[Unity3d]脚本相互调用以及控制

在unity中,我们时常碰到要调用另外一个脚本中的方法,或者通过代码来控制该脚本是否启动执行,下面就贴上这段脚本。

using UnityEngine;
using System.Collections;

public class scriptChange : MonoBehaviour
{
    int i = 0;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        //鼠标右击
        if (Input.GetMouseButtonDown(1))
	    {
            print("mousedown");
            MouseLook obj = (MouseLook)gameObject.GetComponent("MouseLook");
            //C#调用另外一个脚本的方法
            //if (obj == null)
            //{
            //    print("null");
            //}
            ////print(obj);
            //else
            //{
            //    print("OK");
            //    print(obj);
            //    obj.active = true;
            //    obj.test();
            //}

            //鼠标右击开始和关闭
            if (i == 0)
            {
                //开启脚本
                transform.GetComponent<MouseLook>().enabled = true;
            }
            else
            {
                transform.GetComponent<MouseLook>().enabled = false;
            }
            i++;
            i = i % 2;
	    }
	    
	}
}


上一篇:《程序设计解题策略》——1.6 利用左偏树实现优先队列的合并


下一篇:如何区分数据科学家,数据工程师与数据分析师