在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;
}
}
}
本文介绍如何在Unity中实现脚本之间的交互与控制,包括通过代码调用另一个脚本的方法,以及如何通过鼠标点击事件来启用或禁用特定脚本的功能。
3471

被折叠的 条评论
为什么被折叠?



