The C # And JS methods in unity3d are mutually used

Source: Internet
Author: User

Some of unity3d's compile-based methods can only be used in JS, And it is ineffective in C #, while C # can communicate with the server side, and JS itself won't work. Therefore, we will encounter mutual use of these two methods in the plain text. The following is an example.

Two files: test1.js and test2.cs

Test1.js

Function Ongui ()
{
If (GUI. Button (rect ( 25 , 25 , 100 , 30 ), "JS call CS " ))
{
VaRC=Gameobject. getcomponent ("Test2");
C. printtest ();
}
}

Function Testprint ()
{
Print ("CS callJS");
}

 

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 JS files must be in either of the three categories: "Standard assets", "Pro standard assets", and "plugins, the CS file cannot be in the same project as the JS file. The reason is that the original books in these three categories are first written, later written in the "Editor" category, and other historical books are finally written. Currently, the unity3d version 2.5 does not support C #3.0, so you cannot use the VaR keyword. In this way, only the struct type is supported, therefore, if the CS file cannot retrieve the methods in JS in a single object, it cannot be used. The Cs method does not have this restriction for JS runtime.

the software-based entrance exam http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

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.