Unity3D中C#和js方法相互调用
通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets"、 "Pro Standard Assets" 和 "Plugins"这三个文件夹中的任意一个。
在Scripts文件夹新建一个C#脚本 csTest :
public class csTest : MonoBehaviour {
void Start () {
jsTest js1 = gameObject.GetComponent<jsTest>();
js1.PrintJS("C#类中调用 ");
}
public void PrintCS (string str) {
print(str+":C#方法被调用");
}
}
在Standard Assets新建一个js脚本jsTest:
function Start(){
var cs1=gameObject.GetComponent("csTest");
cs1.PrintCS("从js中调用 ");
}
function PrintJS (str) {
print(str+": js方法被调用");
}
然将脚本都挂载到主摄像机上

C#和js脚本都是通过主摄像机这个游戏对象以组件的方式来获取的,也可以放在其他游戏对象上或者新建一个空的游戏对象。
运行游戏,查看控制台的打印信息。

相互调用成功!
js脚本为什么要放到这三个文件夹中?因为javascript脚本和C#脚本是不会编译到一起的。
查看官网的文档关于脚本编译顺序:http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
The phases of compilation are as follows:-
Phase 1: Runtime scripts in folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 2: Editor scripts in folders called Standard Assets/Editor, Pro Standard Assets/Editor and Plugins/Editor.
Phase 3: All other scripts that are not inside a folder called Editor.
Phase 4: All remaining scripts (ie, the ones that are inside a folder called Editor).
Unity3D中C#和js方法相互调用的更多相关文章
- gwt中java与js的相互调用
1. java通过jsni调用内部js Button button = new Button("java调用内部jsni的js方法"); button.addClickHandle ...
- 转载 OS js oc相互调用(JavaScriptCore) ---js调用iOS ---js里面直接调用方法
OS js oc相互调用(JavaScriptCore) 接着上节我们讲到的iOS调用js 下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用 ...
- JS和C#方法相互调用
JS和C#方法相互调用 1.JS调用C#后台方法 方法一: 1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中;2.在前台写一个js函数,内容为document.getEle ...
- Android中通过WebView控件实现与JavaScript方法相互调用的地图应用
在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用andro ...
- js和vue方法的相互调用(iframe父子页面的方法相互调用)。
项目是前后端不分离的,模板引擎使用的JSP. 但是使用了Vue+ElementUI,这里列举一些常用的调用方式,有时候可能. 在js里调用vue方法 我们需要把方法注册到vue对象之外的页面,所以对与 ...
- 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js
iOS js oc相互调用(JavaScriptCore) 从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这 ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- 10、一个action中处理多个方法的调用第二种方法method的方式
在实际的项目中,经常采用现在的第二种方式在struct.xml中采用清单文件的方式 我们首先来看action package com.bjpowernode.struts2; import com.o ...
随机推荐
- 【POI word】使用POI实现对Word的读取以及生成
项目结构如下: 那第一部分:先是读取Word文档 package com.it.WordTest; import java.io.FileInputStream; import java.io.Fil ...
- SpringMyBatis解析1-使用示例
MyBatis使用介绍 MyBatis的详细使用介绍 http://www.cnblogs.com/xrq730/category/796495.html 建立PO public class Per ...
- [工作中的设计模式]观察者模式observer
一.模式解析 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 观察者模式又叫订阅发布模式, ...
- Spring Boot 集成MyBatis
http://blog.csdn.net/isea533/article/details/50359390
- spring MVC 的MultipartFile转File??
MultipartFile file = xxx; CommonsMultipartFile cf= (CommonsMultipartFile)file; DiskF ...
- git 学习笔记1--config & help
1. config 配置用户名和邮箱: git config --global user.name 'pzdn2009' git config --global user.email 10502441 ...
- JQuery EasyUI window 用法
var $win; $win = $('#test-window').window({ title: '添加课程设置信息', width: 820, height: 450, top: ($(wind ...
- Java解析文本
import java.io.*; import java.util.*; public class FileManager { public List<Dishes> parseFile ...
- wamp 2.5 开放访问权限和设置虚拟域名
开放访问权限 D:\wamp\bin\apache\apache2.4.9\conf 里的 httpd.conf 搜索www 把 Require local 改为 Require all gra ...
- The design of a distributed variant of Plato framework to support collaborated editing
A first thought system architecture (pulling mode) is one that the server doesn't keep client inform ...