一,在脚本B中调用脚本A的函数。
脚本A:
//myFuncs.cs
using UnityEngine;using System.Collections;namespace myFuncs{ public class CmyFuncs{ public static void printHi(){ Debug.Log ("Hi"); } }}
脚本B:
// test.cs
using UnityEngine;using System.Collections;using myFuncs;public class test : MonoBehaviour { void Start () { CmyFuncs.printHi (); }}
二,GameObjecA调用GameObjectB的脚本中的函数。
//scriptA.cs
using UnityEngine;
using System.Collections;
public class scriptA: MonoBehaviour {
public scriptB _spriptB;
void Start(){
_scriptB.helloScriptB();
}
}
//scriptB.cs
using UnityEngine;
using System.Collisions;
public class scriptB: MonoBehaviour {
public void helloScriptB(){
print("HELLO, YES, I AM SHOUTING and I AM SCRIPT B!");
}
}
参考:https://www.youtube.com/watch?v=PrXLobhkNqU