JS scripts and C # scripts in Unity call each other

Source: Internet
Author: User

Test1.js

  1. Function ongui ()
  2. {
  3. If (GUI. Button (rect (25,25, 100,30), "JS call CS "))
  4. {
  5. VaR c = gameobject. getcomponent ("Test2 ");
  6. C. printtest ();
  7. }
  8. }
  9. Function testprint ()
  10. {
  11. Print ("CS call JS ");
  12. }

 

Test2.cs

  1. Using unityengine;
  2. Using system. collections;
  3. Public class Test2: monobehaviour {
  4. Void ongui ()
  5. {
  6. If (GUI. Button (New rect (25,70, 100,30), "CS call JS "))
  7. {
  8. Test1 c = (test1) gameobject. getcomponent ("test1 ");
  9. C. testprint ();
  10. }
  11. }
  12. Void printtest ()
  13. {
  14. Print ("JS call CS ");
  15. }
  16. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.