您的位置:首页 > 移动开发 > Unity3D

Unity3D探索之路-C#与js脚本间的相互调用

2015-05-26 07:40 696 查看
之前自己也碰到过,正值从js到C#的艰难转型期,当时痛苦死了。昨晚上群里有朋友提出来C#与js相互调用的问题,鉴于网上大部分解释不全或者不方便大家理解,在这里写一个完整版给大家解决一下。

 

 一、先是编译顺序,脚本的编译过程分四步:

        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

一定尽力为大家解决问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity js c#