JS script in Unity and C # script call each other
Test1.js
- function Ongui ()
- {
- if (GUI. Button (Rect (25,25,100,30),"JS call CS"))
- {
- var c = gameobject.getcomponent ("test2");
- C.printtest ();
- }
- }
- function Testprint ()
- {
- Print ("CS call JS");
- }
Test2.cs
- Using Unityengine;
- Using System.Collections;
- Public class Test2:monobehaviour {
- void Ongui ()
- {
- if (GUI. button (new Rect (25,70,100,30), "CS call JS"))
- {
- Test1 C = (test1) gameobject.getcomponent ("test1");
- C.testprint ();
- }
- }
- void Printtest ()
- {
- Print ("JS call CS");
- }
- }
It must be noted here that the JS file must be in the "Standardassets", "Pro standardassets" and "Plugins" in any one of the three directories, and the CS file cannot be in a directory with the JS file. The reason is that the scripts in these three directories are compiled first, compiled later in the "editor" directory, and the other scripts are compiled at the end. If the CS file cannot read the JS method in a directory, it will not compile and pass. JS calls the CS method without this limitation.
JS script in Unity and C # script call each other