[Unity 3D] Study Notes 27: Unity game script (7)

Source: Internet
Author: User

Use C # To write game scripts


As mentioned above, unity supports writing scripts in three languages: JS, C #, and Boo. We recommend that you only use JavaScript when getting started, because Javascript is easy to understand and the syntax is not very strict. However, C # is recommended later in the promotion, because it is more in line with the unity programming philosophy, higher execution efficiency. The following describes how to use C # To write scripts.


Inherit the monobehaviour class

In unity, any script, including the above three languages, must inherit the class monobehaviour. Why didn't we inherit JavaScript code before? When creating JS Code, the system hides the class name and inheritance relationship.

Create a JS script in the project view. The following code is displayed:

#pragma strictfunction Start () {}function Update () {}
The system automatically generates the update method for us. In JS, there is no void method, and function must be used as the keyword.


Create a C # code. After opening the Code:

using UnityEngine;using System.Collections;public class NewBehaviourScript : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}
In the C # script, the system has helped us generate the corresponding inheritance relationship public class newbehaviourscript: monobehaviour

When binding a C # script, make sure that its name corresponds to the script name in the project resource view. Otherwise, it cannot be bound.


Declare Variables

In JS, the VaR keyword must be used to declare variables, and ":" and the specific object type and value must be added to the variable. C # adds the variable name to the object type.

JS:

Public var I: Int = 0;

VaR name: String [] = ["test", "test1", "Test2"];

VaR OBJ: gameobject;

C #:

Public int I = 0;

VaR string [] Name = ["test", "test1", "Test2"];

VaR gameobject OBJ;



Call Method

It is very common to call methods when writing code, because it is impossible to write all the code in the same method. Such code is unreasonable and should not. JS and C # have different calling methods:

JS

Text01.js:

// Integer var I: int; // float var F: float; // Boolean var B: bool; // string var STR: string; // set the integer function setint (INT temp: INT) {I = temp;} // set the floating point function setfloat (temp: Float) {f = temp ;} // set the Boolean function setboolean (temp: bool) {B = temp;} // set the string function setstring (temp: string) {STR = temp ;} // obtain the integer function getint () {return I;} // obtain the floating point function getfloat () {return F ;}// obtain the Boolean function getboolean () {return B ;} // obtain the string function getstring () {return STR ;}

Main, JS:

var obj : GameObject;function Start(){obj = GameObject.find("cube");var script : text0 = obj.GetComponent("text0");script.setInt(100);script.setFloat(10.0f);script.setBoolean(true);script.setString("test");Debug.Log(script.getInt());Debug.Log(script.getFloat());Debug.Log(script.getBoolean());Debug.Log(script.getString());}

C #:

Text01, CS.

Using unityengine; using system. collections; public class test1: monobehaviour {// integer int I; // float F; // Boolean bool B; // string STR; // set public void setint (INT temp) {I = temp;} // set Floating Point Public void setfloat (float temp) {f = temp ;} // set the Boolean public void setboolean (bool temp) {B = temp;} // set the string public void setstring (string temp) {STR = temp ;} // obtain the public int getint () {return I;} // obtain the floating point public float getfloat () {return F;} // obtain the Boolean public bool getboolean () {return B;} // obtain the Public String getstring () {return STR ;}}


Main, CS:

Using unityengine; using system. collections; public class main: monobehaviour {// cube object gameobject OBJ; // use this for initializationvoid start () {// obtain cube object OBJ = gameobject. find ("cube"); // obtain the test1 script test1 script = obj. getcomponent <test1> (); // sets the integer script. setint (100); // set floating point script. setfloat (10.0f); <PRE name = "code" class = "JavaScript"> function ongui () {If (GUI. button (rect (200,100, 50,), "javascript calls C #") {// obtain the C # script object var cs = This. getcomponent ("cs_test"); // method CS in script C. callme ("I'm from JavaScript") ;}} function callme (test: string) {debug. log (TEST );}

// Set the Boolean script. setboolean (true); // set the string script. setstring ("test"); // obtain information and print debug. log (script. getint (); debug. log (script. getfloat (); debug. log (script. getboolean (); debug. log (script. getstring ());}}

 

In fact, the JS and C # Code have not changed much, but the related keywords are different from the format. I believe that it is not difficult to learn C # With Java learning experience. I remember the teacher once said: since the development of computer programming languages, most languages are actually used for mutual reference, so they are very similar. As long as you have learned one of them, other languages are easy to use.


Communication between JS and C # scripts

In the official unity documents, the JavaScript or C # script functions are completely equal. This function does not exist and is better than C. However, in practice, you will inevitably encounter two types of scripts to write a game. In this case, communication is required between them.

Js_test.js

Function ongui () {If (GUI. button (rect (200,100, 50,), "javascript calls C #") {// obtain the C # script object var cs = This. getcomponent ("cs_test"); // method CS in script C. callme ("I'm from JavaScript") ;}} function callme (test: string) {debug. log (TEST );}

Cs_test.cs

Using unityengine; using system. collections; public class cs_test: monobehaviour {void ongui () {If (GUI. button (New rect (100,170,200,100), "C # Call JavaScript") {// get the Javascript script object js_test jsscript = (js_test) getcomponent ("js_test "); // call the jsscript method in the Javascript script. callme ("I'm from C #") ;}} public void callme (string test) {debug. log (TEST );}}

Run:


After clicking:


In this example, you must bind both js_test and cs_test to the camera, and place the JS script in the newly created standard assets folder. Otherwise, the file cannot be compiled.


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.