- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
Let’s look at an example.
var speed = 5.0;
var distance = 10;
var moving = false;
var myString = "This string has been dynamically typed!";
function Start() {
//movement is now equal to "50.0"
movement = speed * distance;
//set moving to true
moving = true;
print (myString);
}Notice that we’ve assigned 4 different variables, but we didn’t actually declare the variable type. Unity in this case is able to infer what the variable types are. The first variable “speed” is a float. The second variable “distance” is an integer. The third variable “moving” is a boolean and the fourth variable “myString” is a string.
Javascript Arrays in Unity
Arrays are a type of variable that can hold more than one piece of information. Once you start exploring the world of scripting you will find yourself using arrays a lot! Arrays are very handy and are a necessity for anything more than the simplest scripts.
Let’s look at a simple example of an array.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray.Add("T-Shirt");
myArray.Add("ants");
myArray.Add("Shoes");
}
function Start() {
//prints out "We have 3 items in our array" to the console.
print ("We have " + myArray.length + " items in our array");
}
In this example, we declared an array called “myArray”, then we used the “Add” method to assign 3 values to our array. In the Start function we printed out the length of our array using “myArray.length” which tells us how many items are in our array.
Instead of using the Add method we can also add values to our array as follows.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray = Array("T-Shirt","ants","Shoes");
}
function Start() {
//prints out "We have 3 items in our array" to the console.
print ("We have " + myArray.length + " items in our array");
}Ok, so we know how to assign values to our array but how do we access the values that are contained in our array? Each value in our array is assigned an integer value as an array key. We can access the array value by referencing its “place” within the array.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray = Array("T-Shirt","ants","Shoes");
}
function Start() {
//prints out "T-Shirt"
print (myArray[0]);
//prints out "ants"
print (myArray[1]);
//prints out "Shoes"
print (myArray[2]);
}
The first value in the array will always be assigned a key of “0&rime;, each key after that will be incremented by 1 as you can see in the example above. Note that the key is contained within square brackets.
You can also “iterate” through an Array and do something with each value as you move through the array.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray = Array("T-Shirt","ants","Shoes");
}
function Start() {
for (i = 0; i < myArray.length; i++) {
if (myArray == "Shoes") {
print ("We have shoes in our array!");
}
}
}In the above example, we loop through the array and increment the temporary variable “i” by 1 each time. We stop looping through when i is equal to the length of the array. If we find an element in our array that is equal to “Shoes” we print out “We have shoes in our array!” to the console.
Let’s take a look at another example that should show you exactly how this is working behind the scenes.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray = Array("T-Shirt","ants","Shoes");
}
function Start() {
for (i = 0; i < myArray.length; i++) {
print ("We are currently on loop #" + i + " and the value is currently " + myArray);
}
}
The above example will output the following lines to the console:
We are currently on loop #0 and the value is currently T-Shirt
We are currently on loop #1 and the value is currently Pants
We are currently on loop #2 and the value is currently Shoes
We can also iterate through Javascript arrays using another method. Let’s see how it works.
var myArray = new Array();
function Awake() {
//add some values to the array, let's make a list of clothes our character is wearing
myArray = Array("T-Shirt","ants","Shoes");
}
function Start() {
//loop through the array and print out each value
for (var arrayValue : String in myArray) {
print (arrayValue);
}
}Experiment with Javascript arrays until you are comfortable with the basics. In the next Javascript tutorial I will cover intermediate array handling and manipulation using the available Javascript array functions.
Javsascript Built In Arrays in Unity 3
Unity also allows us to use “built-in” arrays. These are arrays that are built in to the core of the Unity engine and are lightning fast when it comes to processing and use minimal overhead. Built in arrays come in handy in a wide variety of situations and they also have the advantage of being exposed in the Unity editor inspector panel. This means you can drag and drop game objects or elements into your defined built in arrays. There is one downfall however and that’s the fact that built in arrays can *not* be resized on the fly. Let’s see how this works.
var myArray : String[];
function Start() {
for (var arrayValue : String in myArray) {
print (arrayValue);
}
}In the script above, we declare a new built in array called “myArray” but we don’t assign any values to the array. Instead we assign the values in the Unity editor inspector panel.
Built in arrays can hold any type of component as well. This can come in very handy when organizing your game as you can simple drag and drop game components into your array in the inspector.Let’s pretend for a minute that we have a script that’s used to Instantiate enemies in our level. We have 5 different enemy types and we want to hold a reference to them in a built in array. Here’s the code.
var enemyTypes : GameObject[];
function Start() {
for (var enemy : GameObject in enemyTypes) {
Instantiate(enemy,transform.position, Quaternion.identity);
}
}
First we create a new built in array that will hold our Game Objects. Then in the inspector we set the size of the array to 5 and drag and drop our enemy prefabs into the open slots. When we run the script, all 5 of our enemies will be placed into the scene at the same position as the object our script is attached too. Not very practical, but it shows us how built in array functions work.
If you’re creating games for mobile devices using built in arrays can have significant performance advantages over regular Javascript arrays. So I suggest you play with them until you are comfortable using them in your own projects.
Putting it All Together and Using Javascript in Unity
This pretty much concludes your basic introduction to Javascript in Unity. There is a lot more to cover of course and it only get’s more interesting and fun from here. I will be covering intermediate and advanced scripting in future tutorials. In the meantime, I have a challenge for you to test the skills you have just learned.
Javascript Practice Challenge
Create a project in Unity that utilizes all you have learned. Use global variables, private variables, Javascript arrays and built in arrays to put it all together.
Here’s the setup for the project.
Your scene should start out with 4 or more objects. Each object should have a property of “Hot”, “Cold”, “Warm” or “Luke Warm” and have a material associated with those properties. Every time one of the objects is clicked, a new object should be created in your scene. The new object that is created should change it’s material depending on what the property is on the object that was clicked.
Here’s some hints to get you started.
OnMouseDown()
Instantiate()
Material
Have fun!
|
|