12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2452|回复: 10
打印 上一主题 下一主题

javascript基本知识(三)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-15 14:23:00 |只看该作者 |倒序浏览


           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!
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

沙发
发表于 2012-2-11 23:18:48 |只看该作者
好`我顶``顶顶
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-5-3 23:20:00 |只看该作者
俺是新人,这厢有礼了!
回复

使用道具 举报

markq    

511

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
15839
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-5-4 23:21:30 |只看该作者
  谢谢分享



爱生活 爱3D 爱纳金网



www.narkii.com
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-6-12 23:26:19 |只看该作者
我就看看,我不说话
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-7-19 23:18:44 |只看该作者
有意思!学习了!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-7-25 23:20:20 |只看该作者
呵呵,真得不错哦!!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-7-30 23:26:24 |只看该作者
路过、路过、快到鸟,列位请继续...ing
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-8-19 23:42:46 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-12-13 22:49:45 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-24 02:15 , Processed in 0.100945 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部