且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-23 23:31:09

在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; 	    } 	     	} } 

















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366369,如需转载请自行联系原作者