纳金网

标题: javascript基本知识(一) [打印本页]

作者: 会飞的鱼    时间: 2011-11-15 14:20
标题: javascript基本知识(一)


           不错的脚本简介文章,对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.
         

作者: 菜刀吻电线    时间: 2012-2-1 23:22
提醒猪猪,千万不能让你看见

作者: tc    时间: 2012-5-7 23:25
都闪开,介个帖子,偶来顶

作者: 奇    时间: 2012-5-10 23:24
楼主收集的可真全哦

作者: 奇    时间: 2012-5-20 23:19
跑着去顶朋友滴铁

作者: 奇    时间: 2012-5-22 23:25
都闪开,介个帖子,偶来顶

作者: 菜刀吻电线    时间: 2012-9-19 10:00
响应天帅号召,顶

作者: 菜刀吻电线    时间: 2012-12-3 23:24
佩服,好多阿 ,哈哈

作者: 晃晃    时间: 2013-2-19 23:39
你们都躲开,我来顶

作者: 晃晃    时间: 2013-2-27 23:18
很有心,部分已收录自用,谢谢

作者: boylhw    时间: 2013-3-6 10:49
看着还不错  多谢分享
作者: nts    时间: 2013-10-17 11:00
还是C#比较好学的




欢迎光临 纳金网 (http://wwww.narkii.com/club/) Powered by Discuz! X2.5