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

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

 

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

        . 编译所有 ”Standard Assets”, “Pro Standard Assets” or “Plugins” 目录下的代码

        . 编译所有“Standard Assets/Editor”, “Pro Standard Assets/Editor” or         “Plugins/Editor” 目录下的代码

        . 编译除了上面列出来的目录和Editor目录之外的所有代码

        . 编译Editor目录下的代码

    需要被调用的脚本放在第1类目录下(没有就新建);而使用它的脚本放在第三类目录下就好了。

二、调用方法

     注:test5test7脚本挂在了名为"C# to js"的物体上

            test6test8脚本挂在了名为"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#jsC#

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交流qq1083565131

一定尽力为大家解决问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值