一. Camera
【1】组件介绍
Clipping Planes:Camera最近最远视野
Field of View:视角的宽阔程度
Projection:视野类型
Clear Flags:设置背景的,默认为Skybox .选择到Solid Color时,下面的Background可设置背景颜色
Field of View为48时
Field of View为100时
【2】静态变量main和公有函数ScreenPointToRay
通过射线检测的方式检测鼠标在哪个游戏物体身上:
private Camera camera;
void Start () {
camera = Camera.main;
}
void Update () {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
//Debug.DrawRay(ray.origin, ray.direction);//显示射线
//Debug.DrawLine(ray.origin, ray.origin + ray.direction * 100);//设置射线长度
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
Debug.Log(hit.collider);
}
二. CharacterController角色控制器
通过角色控制器控制角色移动,首先添加组件让角色控制的功能,其次编写代码发送命令
public float speed = 3;
private CharacterController cc;//得到组件
void Start () {
cc = GetComponent<CharacterController>();//获取组件
}
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
cc.SimpleMove(new Vector3(h, 0, v) * speed);//Simplemove有重力的影响
//cc.Move(new Vector3(h, 0, v) * speed * Time.deltaTime);//move没有重力的影响
Debug.Log(cc.isGrounded);//判断是否在地面上
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log(hit.collider);
}
三.Mesh和Material控制物体的显示
【1】
Mesh:是一个网格 控制物体外观
Maerial:材质 贴到Mesh指定的物体上控制物体的“皮肤”
【2】更改物体的Meah(由cube变成sphere)
cube的inspector面板
public Mesh mesh;
void Start () {
GetComponent<MeshFilter>().mesh = mesh;//复制了一个新的给mesh
Debug.Log(GetComponent<MeshFilter>().mesh == mesh);//访问一下两个mesh是否相等,结果为false
//GetComponent<MeshFilter>().sharedMesh = mesh;//直接用原有的
//Debug.Log(GetComponent<MeshFilter>().sharedMesh == mesh);//结果为true
}
【3】更改物体的材质
public Mesh mesh;
private Material mat;
void Start () {
mat = GetComponent<MeshRenderer>().material;
}
void Update () {
mat.color = Color.Lerp(mat.color, Color.red, Time.deltaTime);//由当前颜色渐变成红色
}
四.Unity API方法变更
【1】unity 4.x 5.x 2017
GetComponent() 代替 rigidbody2D
GetComponent() 代替 rigidbody
GetComponent() 代替 audio
private Rigidbody rgd;
void Start () {
rgd = GetComponent<Rigidbody>();
}
void Update () {
rigidbody.AddForce(Vector3.one);//弃用的
rgd.AddForce(Vector3.one);
audio.Play();//弃用的
GetComponent<AudioSource>().Play();
GetComponent<Rigidbody2D>();
GetComponent<MeshRenderer>();
}
【2】Unity 5.3:
ParticleSystem main = smokePuff.GetComponent();
main.startColor
Unity 5.5+:
ParticleSystem.MainModule main = smokePuff.GetComponent().main;
main.startColor
【3】SceneManagement 代替 Application
using UnityEngine.SceneManagement;
void Update () {
//Application.LoadLevel("Level2");//弃用的
SceneManager.LoadScene("Scene2");
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
【4】OnLevelWasLoaded() 在 Unity 5中被弃用了。
// private void OnLevelWasLoaded(int level) //弃用的
//{
//}
void Start () {
SceneManager.sceneLoaded += this.OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
}
【5】2D/3D sound 如何设置2D/3D声音