Unity API常用方法和类(四)

一. Camera
【1】组件介绍
1
Clipping Planes:Camera最近最远视野
Field of View:视角的宽阔程度
Projection:视野类型
Clear Flags:设置背景的,默认为Skybox .选择到Solid Color时,下面的Background可设置背景颜色

2Field of View为48时
3Field 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角色控制器
通过角色控制器控制角色移动,首先添加组件让角色控制的功能,其次编写代码发送命令
4

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指定的物体上控制物体的“皮肤”
5
6
【2】更改物体的Meah(由cube变成sphere)
7
cube的inspector面板
8

 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声音
9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值