一、先是编译顺序,脚本的编译过程分四步:
1. 编译所有 ”Standard Assets”, “Pro Standard Assets” or “Plugins” 目录下的代码
2. 编译所有“Standard Assets/Editor”, “Pro Standard Assets/Editor” or “Plugins/Editor” 目录下的代码
3. 编译除了上面列出来的目录和Editor目录之外的所有代码
4. 编译Editor目录下的代码
需要被调用的脚本放在第1类目录下(没有就新建);而使用它的脚本放在第三类目录下就好了。
二、调用方法
注:test5和test7脚本挂在了名为"C# to js"的物体上
test6和test8脚本挂在了名为"js to C#"的物体上
test7:
#pragma strict
function Start () {
}
function Update () {
}
function js()
{
Debug.Log("js");
}
test8:
using UnityEngine;
using System.Collections;
public class test8 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void cs()
{
Debug.Log("cs8");
}
}
1.js调用C#和js示例:
test6:
#pragma strict
private var cs : test8;
private var js : test7;
function Awake()
{
//cs = this.GetComponent("test8");
js = GameObject.Find("C# to js").GetComponent("test7");
cs = GameObject.Find("js to C#").GetComponent("test8");
}
function Start () {
cs.cs();
js.js();
}
function Update () {
}
调用结果:
2.C#调js和C#:
test5:
using UnityEngine;
using System.Collections;
public class test5 : MonoBehaviour {
private test7 js;
private test8 cs;
void Awake()
{
js = GameObject.Find("C# to js").GetComponent<test7>();
cs = GameObject.Find("js to C#").GetComponent<test8>();
}
// Use this for initialization
void Start () {
js.js();
cs.cs ();
}
// Update is called once per frame
void Update () {
}
}
调用结果:
目录截图:
场景对象截图:
Unity交流qq:1083565131
一定尽力为大家解决问题