Unity中C#与JavaScript相互通信
2013-08-03 09:29
288 查看
本文代码例子 http://vdisk.weibo.com/s/BDn59yfnBVHW-
首先给大家介绍一个名词 #pragma strict
#pragma strict
严谨编译模式
性能优化:JS中强制使用静态类型,脚本顶部添加#pragma strict。然后,unity将在脚本中禁用动态类型,强制使用静态类型,如果一个类型未知。Unity将报告编译错误。
可能会报这样的一个错误:(方法名) is not a member of 'UnityEngine.Component'. 解决这个错误的方法就是将 #pragma
strict 删掉就好了。
其中JS的文件必须在 这三个文件中的一个"Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 没有的话自己创建就好。
下面是相互通信的代码
c#
JS
欢迎留言相互讨论问题
首先给大家介绍一个名词 #pragma strict
#pragma strict
严谨编译模式
性能优化:JS中强制使用静态类型,脚本顶部添加#pragma strict。然后,unity将在脚本中禁用动态类型,强制使用静态类型,如果一个类型未知。Unity将报告编译错误。
可能会报这样的一个错误:(方法名) is not a member of 'UnityEngine.Component'. 解决这个错误的方法就是将 #pragma
strict 删掉就好了。
其中JS的文件必须在 这三个文件中的一个"Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 没有的话自己创建就好。
下面是相互通信的代码
c#
using UnityEngine; using System.Collections; public class CSMain : MonoBehaviour { void OnGUI() { if(GUILayout.Button("JS")) { JSMain jsScript = (JSMain)GetComponent("JSMain"); jsScript.Test("shenqi"); } } void CSTest(string test) { Debug.Log("CS" + test); } }
JS
function OnGUI() { if(GUI.Button( Rect(100,100,100,50),"CS")) { var csTest = this.GetComponent("CSMain"); csTest.CSTest("fff"); } } function Test(test:String) { Debug.Log("JS " + test); }
欢迎留言相互讨论问题
相关文章推荐
- net中前台javascript与后台c#函数如何相互通信
- javascript与后台c#函数相互通信
- NanUI文档 - 如何实现C#与Javascript的相互通信
- NanUI文档 - 如何实现C#与Javascript的相互通信
- unity中c#、js 相互通信
- unity JavaScript 与 c# 脚本之间的通信
- net中前台javascript与后台c#函数相互调用
- 转载:net中前台javascript与后台c#函数相互调用问题
- .net中前台javascript与后台c#函数相互调用问题
- C# client 与java netty 服务端的简单通信,客户端采用Unity。
- Javascript与C#函数的相互调用
- Unity和Android相互通信
- net中前台javascript与后台c#函数相互调用问题
- C#、Unity网络通信中基于字节码的自定义协议解码,C#版ByteBuffer
- JavaScript、Flex、Air之间的相互通信的研究
- net中前台javascript与后台c#函数相互调用问题
- [转]C# winform与Javascript的相互调用
- asp.net中前台javascript与c#函数相互调方法(http://hi.baidu.com/tracyjay/blog/item/fac2063700b0ea45241f14c2.html)
- Asp.Net中前台Javascript与C#函数相互调方法
- .Net中前台javascript与后台c#函数相互调用问题