查看: 1633|回复: 8
打印 上一主题 下一主题

javascript基本知识(二)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

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


           Unity & Javascript also allow us to define global variables. These are variables that will be available to *all* scripts within your game. We define global variables by using the “static” keyword. Let’s see an example.
         

           Save this script as “globalExample”.
         

           static var myFirstGlobal : String;
         

           function Awake () {
         

           myFirstGlobal = "This string is available throughout my game!";
         

           }
           

           Create another script and call it whatever you like.
         

           function Start () {
         

           //we use the name of the script where the variable was defined to access the global variable
         

           //prints "This string is available throughout my game!" to the console
         

           print (globalExample.myFirstGlobal);
         

           //assign a new string to the variable
         

           globalExample.myFirstGlobal = "This variable was changed from outside the script it was defined in!";
         

           }
           

           Pretty handy right? We created one script and defined a global variable, then we accessed that variable from another script. When creating games it’s essential that you’re scripts can communicate with each other in one way or another. Taking advantage of variable scope is one way to accomplish this. So when you’re creating variables within your scripts, ask yourself “Am I going to need to access this variable anywhere else?”.
         

           Common Variable Types in Unity 3
         

           When defining a variable, its good practice (and often a requirement) that you define it’s type as well. You’ll find yourself using a few type of variables more often than not. Defining variable types also helps to speed up processing and proper use of variable types helps avoid conflicts and helps keep memory usage in check.
         

           //define a floating point variable (floats can hold integers with or without decimal points)
         

           var firstFloat : Float = 1.0;
         

           //define an integer - an integer is a positive or negative number with no decimal points
         

           var firstInteger : int  = 10;
         

           //define a string - a string can contain both next and numbers but can't be numerically compared without first being processed
         

           var firstString : String;
         

           //define a boolean - booleans hold a value of either ***e or false (1 or 0)
         

           var frstBoolean : Boolean;
         

           //define a Vector3 - a Vector3 holds a set of 3 float values or integers (0,0,0)
         

           var frstVector : Vector3;Javascript Dynamic Variable Typing
         

           when declaring variables in C# you *must* define the type of variable for every variable you create. The same does not hold ***e for Javascript however, which is one reason it’s much easier to pick up than C#. Javascript uses what’s called “dynamic variable typing”. In other words, when you define a variable Unity will *guess* at what the type should be when you assign a value to it. Unity can’t always guess correctly, but it will try it’s best to “dynamically type” the variable. Keep in mind, using dynamic typing is costly in terms of overhead and isn’t very good practice – but it’s still handy to know that the option is there.
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

沙发
发表于 2012-1-25 23:25:18 |只看该作者
快意吟诗句, 乐在得团圆。
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-2-4 23:30:17 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-2-22 23:22:43 |只看该作者
爱咋咋地!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

5#
发表于 2012-3-12 23:24:39 |只看该作者
路过、路过、快到鸟,列位请继续...ing
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-4-27 23:26:48 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

7#
发表于 2012-7-13 23:18:59 |只看该作者
读铁系缘分,顶铁系友情
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

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

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

9#
发表于 2012-10-10 23:19:14 |只看该作者
“再次路过……”我造一个-----特别路过
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-30 19:51 , Processed in 0.090399 second(s), 31 queries .

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

© 2008-2019 Narkii Inc.

回顶部