- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
不错的脚本简介文章,对unity3d代码不怎么了解的朋友比较有用!
Unity offers a slew of options to choose from as far as scripting goes. You can use Javascript, C#, Boo script or a combination of the three. In reality though, scripting in Unity is slightly different than other mediums. So let’s just call it Unity script. The scripting language you choose will dictate what syntax you must use in your scripts and what methods and functions will be available to you. For a beginner the easiest medium is Javascript. For more advanced users C# offers more control and better array handling than Javascript. In this tutorial we’re going to cover some of the basic elements of scripting in Javascript with Unity. We’ll go over the most commonly used methods and techniques and the basic syntax of Javascript. Let’s get started.
Javascript Variables in Unity 3
The first and arguably most important topic that we’ll cover is variables and variable scope. You can think of a variable as a storage unit. Each variable is assigned a name and can hold a certain type of data. You can then access the variable later on in your script via the name that you’ve assigned. Let’s look at an example.
var myFirstVariable : String;
function Awake () {
myFirstVariable = "Hello World!";
}
function Start() {
//prints "Hello World!" to the console.
print (myFirstVariable);
}In this simple example, we declare a variable called “myFirstVariable” and tell Unity that we’re going to be using it to store a string. A string can be any form of text. Next, in the Awake function we assign the value of “Hello World!” to our variable. Finally, in the Start function we print out the variable to the Unity console.
There’s a couple things to take not of here. First, notice that we declared the variable *outside* of the functions. We also used the keyword “var” to tell Unity that we’re going to be declaring the variable. Since the variable was assigned outside of the functions, we can access the variable within *all* of the functions. The variable will also be available in the Unity inspector. In other words, the variable is exposed both to the Unity inspector *and* within the script itself. In Unity these are called “member variables” They are essentially a member of the script that they are declared in.
If we don’t want the variable to be exposed in the inspector we can instead declare a “private member” variable. The variable will still be available for use throughout our script, but you won’t see it within the Unity inspector panel. Let’s see how we can accomplish that.
private var myFirstVariable : String;
function Awake () {
myFirstVariable = "This string is private to this script!";
}
function Start() {
//prints "This string is private to this script!" to the console.
print (myFirstVariable);
}
Pretty straightforward, right? This is an example of whats called “variable scope”. Different types of variables have different “scopes”. This means you can reveal or hide variables from other aspects of your game to keep things nicely organized and use less resources.
|
|