Unity官方案例精讲_2015_优化

1.将公共变量从Inspector视图中隐藏:    [HideInInspector]

    [HideInInspector]
    public GameObject player;

2.限定Inspector面板上属性值的范围:    [Range(min,max)]

    [Range(0f,255f)]
    public int num;

3.在C++中参数的传递方式有:传值,传址,传引用,在C#中只存在在传值,传引用,其中传引用可由关键字ref和out完成,两者之间的区别在于若用ref方式来传递参数,则该实际参数必须先初始化,而out方式则不需要初始化,out参数也称为输出参数。

    void Start () {
        string str1;
        OutFun(out str1);//参数可以不用初始化
        string str = "hello word";
        RefFun(ref str);//参数必须初始化public void OutFun(out string str) {
            str = "我是OUT参数";
        }
        public void RefFun(ref string str) {
            str = "我是ref参数";
        }
}

4.协程(Coroutine):中断当前执行的代码,直到终端指令结束接着执行下面代码。

 5.在Update函数中,需要通过Time.deltaTime来抵消帧率带来的影响,但在FixedUpdate函数中,由于其更新帧频率固定,所以不需要使用Time.deltaTime。

控制游戏对象移动的方法:

    void FixedUpdate() {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //方法一
        //Vector3 dir = new Vector3(h, 0, v);
        //GetComponent<Rigidbody>().velocity = dir;

        //方法二
        //GetComponent<Rigidbody>().AddForce(new Vector3(h, 0, v));
    }

    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //方法三
        //transform.Translate(new Vector3(h, 0, v));
    }
PlayerMove
原文地址:https://www.cnblogs.com/shirln/p/9289017.html