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 ");
- }
- }
Note that the JS file must be in either of the "standardassets", "Pro standardassets", and "plugins" directories, the CS file cannot be in the same directory as the JS file. The reason is that the scripts in these three directories are first compiled, the scripts in the "Editor" directory are compiled later, and other scripts are finally compiled. If the CS file is in a directory, the method in JS cannot be read, and compilation will fail. JS does not have this restriction when calling the Cs method.