It is important to note that the script being called must be placed in the"Standard Assets","Pro standard Assets"and the"Plugins"any one of these three folders, and the script that calls it cannot be in a directory folder, otherwise it cannot be compiled, becauseUnitywill compile these three folders first,"Editor"the script in the directory is compiled later, and the other scripts are finally compiled (bothCScalledJS,CSThe script is placed in the other file home,JSthe script should be placed in the above three folders first compiled;JScalledCS,JSThe script is placed in a different folder,CSscript to be placed in the above three folders first compiled)
First, CS Call JS script
Jsmm.js
Jsmm Script method function spoken () { print ("JS js JavaScript js js "); }
Test.cs Script
public class Test:monobehaviour { void Ongui () { if (GUI. button (new Rect ((), Diaoyong js)) { //Get JSMM script jsmm js = (JSMM) getcomponent<jsmm> (); //Call the method in the JS script js.spoken () ; }}
Hang the jsmm.js and Test.cs on one object and run
Second,JS Call CS
CsMM.cs Script
Using unityengine;using System.collections;public class Csmm:monobehaviour {public void AAA () { Debug.Log ("Aaaaaaa");} }
Jscode.js
#pragma strictfunction Ongui () { if (GUI. button (new Rect (a), "JS DIAO YONG cs") { var csmmscript = gameobject.getcomponent ("csmm"); CSMMSCRIPT.AAA (); }}
Can be run
Note the comments in the JS script //#pragma strict or it will be an error. method in CS script not found
The CS script in unity interacts with the JS script