Unityscript Quick Start

Source: Internet
Author: User

UnityscriptOriginally known as JavaScript For U3D, but some newer documents have been called unityscript. Although JavaScript syntax can be used almost all in unityscript, however, unityscript is a JS with static type check and more Oop, so you can use unityscript as a new language. Of course, if you have learned JS, so it is quite fast to learn unityscript. Compared with JS running on a browser, unityscript is far better than the former in terms of language design and running ability.


This quick start tutorial will focus on unityscript, which is not the same as JS, so it is more suitable for programmers who switch from JS and have a little programming basics, if you do not have the JS Foundation, we recommend that you first learn Js. If you do not have the programming Foundation, we recommend that you do not touch any game engine first ~ (^ )~


Chapter-1 strict Mode

Generally, the first line of the unityscript source code file must contain

#pragma strict
In fact, you can also leave it alone. In addition, it will enter the strict mode, which will limit the dynamic features of Js.

Chapter-2 specifies the type for static variables

Unityscript declares variables in the same way as Js. They are all declared in the 'var variable name' format, but unityscript can use'VaR variable name: Type'To specify a clear type for declared variables. The following are some examples:

VaR num: Int = 1; // integer variable var str: String = "Hello unityscript"; // string variable var player: gameobject; // game object variable </span>


Chapter-3 Function

Because unityscript can specify the data type for variablesSpecify a type for the parameterAnd can alsoSpecify a return value:

function Add(x :int, y :int) :int {    return x + y;}
If no value is returned, you do not need to add any code to the parameter list.


Chapter-4 Access Control
Unityscript allows you to add PrivateOr PublicKeyword to set access control permissions for variables, classes, or functions (also known as external visibility), for example:

Private var self: String = "Myself"; // Private variable public function getname () {// return "nonull ";}

Chapter-5 object-oriented and interfaces

The orientation in unityscript is very similar to that in Java, including the keywords used for inheritance and interface implementation. Take a look at the following example to understand:

Public class person {private var name: string; private var age: int; Public Function person (Name: String, age: INT) {// constructor this. name = Name; this. age = age;} public function introduce (): String {return "My name is:" + this. name + "and I am" + this. age. tostring () + "years old. ";}} public class student extends person {public function student (Name: string) {super (name );}//...}

Next is the interface:

public interface People {    function Speak();     function Study();    function Walk();}public class Child implements People {    public function Speak() {...}    public function Study() {...}    public function Walk() {...}}

Chapter-6 event Functions

Some specific functions are used to deal with specific events in unity, such as what operations should be performed during game loading and what operations should be performed during frame update, these event functions determine the approximate execution structure of the script file. Below are some common event functions ~

Function awake () {} function start () {} function Update () {// frame update} function fixedupdate () {// fixed interval update}

Chapter-7 concurrency
Coroutine is used in unityscript for concurrency. Not all operations must be completed within one frame. You may want to keep a piece of code running within a certain period without affecting the refresh of the frame, coroutine will be used at this time.

To run a code in coroutine mode, add a keyword to the proper position of the Code.YieldThe proper position is determined by yourself. If you set the yield keyword, the code will terminate the operation at the yield keyword, return to the yield keyword and continue running at the appropriate time. The following code defines coroutine:

Public Function coroutinetest () {While (true) {yield; // Add the yield keyword to execute the function in coroutine mode }}
To run a coroutine code segment, call it just like calling a common function:

Function start () {coroutinetest (); // automatically runs in coroutine mode and does not block the update of image frames}

Chapter-8 Network Programming

Unityscript does not have its own socket, but it can use the mono socket because it runs as a Mono operation. The following defines a socket ~

public var sock :System.Net.Sockets.Socket;


If reprinted please indicate the source: http://blog.csdn.net/gophers



Unityscript Quick Start

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.