Unity3D 脚本及变量 -- 基本概念

来源:互联网 发布:高德地图数据导入 编辑:程序博客网 时间:2024/05/21 04:38

1. 简介

    在Unity中,脚本是实现游戏功能的主要方式之一,而变量是脚本中的重要元素,通过变量的使用,可以完成数据的引用,数学模型的定义及完成各种计算等。

    • 主要支持以下三种脚本:

    1)JavaScript

    2)C#

    3)Boo Script

    前两种较常用。

    • 脚本:本质上也是一种资源,所以在项目视窗中创建。

    • 在使用脚本时,需要把脚本链接到一个物体中

    • 可通过变量的使用来提高脚本的重用性


2. 访问游戏物体组件

2.1 访问游戏物体成员组件(标准的成员组件)

    •  游戏对象成员组件的访问:是指在脚本中对自身成员组件变量的访问,可通过成员变量的方式进行直接访问。

        举例如下:

        1)transform:引用Transform组件,与gameObject.transfrom、this.gameObject.transform、this.transform相同,this表示当前脚本,gameObject表示当前脚本链接的物体

        2)rigidbody:引用刚体组件

        3)...

2.2 访问自定义的组件(如脚本)

    •  如在ScriptA中需要访问ScriptB,需在ScriptA中定义一个新的变量(ScriptB),然后使用组件获取语句GetComponent来获取ScriptB,举例如下:

      ScriptB.js

#pragma strictpublic var speed = 5.0;function Start () {}function Update () {}

     ScriptA.js

#pragma strictvar other:ScriptB;function Start () {other = GetComponent(ScriptB); //仅引用脚本名即可Debug.Log(other.speed);}function Update () {transform.Rotate(0,other.speed*Time.deltaTime,0);}

3. 游戏物体间相互访问

    • 创建游戏的过程,就是定义游戏物体与环境之间,以及游戏物体间相互交互的过程。

    • 一个游戏物体可访问其它游戏物体,或访问其它游戏物体的标准成员组件或脚本。

3.1 使用Transform公共变量直接引用物体

    在一个脚本中通过定义Transform公共变量,然后把其它物体拖到此公共变量,从而实现对此物体的引用。

    在Cube中访问Sphere的例子如下:

      SphereScript.js

#pragma strictpublic var radius = 6.0;function Start () {}function Update () {Debug.Log("Hello");}

     CubeScript.js

#pragma strictpublic var target:Transform; //引用Cube物体function Start () {}function Update () {target.renderer.material.color = Color.red;}


3.2 引用其它物体的脚本

       除了直接引用物体类型,用户也可以通过定义变量的方式来引用或访问其它物体中的脚本。

         SphereScript.js

#pragma strictpublic var radius = 6.0;function Start () {}function Update () {}function DoSomething(){Debug.Log("Hello");}

   CubeScript.js

#pragma strictpublic var target:SphereScript;function Start () {target.DoSomething();}function Update () {target.renderer.material.color = Color.red;}

     直接把SphereScript.js链接的物体拖到变量targer即可(因为球体对象中包含了SphereScript脚本)。

3.3 使用Find()函数引用物体(根据物体名称或路径)

    • 通过GameObject.Find()函数来访问其它物体,通过使用物体的名称、或路径来查找或定位场景中的其它物体。

    • 对于Find函数,尽量放在Start函数中,避免在每一帧中都对其进行执行, 以提高程序的运行效率。  

      SphereScript.js

#pragma strictpublic var radius = 6.0;function Start () {}function Update () {}function DoSomething(){Debug.Log("Hello");}

     CubeScript.js

#pragma strictpublic var target:GameObject; //也可以不要Find,直接把物体拖到这儿var other:SphereScript;function Start () {target = GameObject.Find("MySphere");other = target.GetComponent(SphereScript);  //根据组件名获取物体的组件other.DoSomething();}function Update () {target.renderer.material.color = Color.red;}

3.4 使用标签的方式引用物体(根据物体标签名)

       SphereScript.js

#pragma strictpublic var radius = 6.0;function Start () {}function Update () {}function DoSomething(){Debug.Log("Hello");}

     CubeScript.js

#pragma strictpublic var target:GameObject;var other:SphereScript;function Start () {//target = GameObject.Find("MySphere");target = GameObject.FindGameObjectWithTag("SphereTag");other = target.GetComponent(SphereScript);other.DoSomething();}function Update () {target.renderer.material.color = Color.red;}

4. 脚本时间控制

4.1 Time.deltaTime

       以秒计算,完成最后一帧的时间(只读)。

4.2 Time.time

       从游戏开始到到现在所用的时间(只读)。 

4.3 Time.frameCount

       已经显示的总的帧数

4.4 Time.timeScale

       时间在以此比例流逝,可用于减慢运动效果。

4.5 时间控制Yield

       • Yield:是一种特殊类型的返回语句(即中断指令),其后的指令执行完之后,再接着向下执行。Yield指令常与WaitForSeconds,WaitForFixedUpdate,Coroutine,MonoBehaviour.StartCoroutine配合使用。     

#pragma strict//yield.jsfunction Start () {Do();print("This is printed immediately, Time=" + Time.time);}function Update () {}function Do(){print("Do Now, Time=" + Time.time );yield WaitForSeconds(10); // 等待10秒之后,再执行后面的语句,即中断处理print("Do 10 seconds later, Time=" + Time.time);}


     其执行结果如下所示:
Do Now, Time=0UnityEngine.MonoBehaviour:print(Object)This is printed immediately, Time=0UnityEngine.MonoBehaviour:print(Object)Do 10 seconds later, Time=10.00013UnityEngine.MonoBehaviour:print(Object)

5. 随机数(Random)

    该类用于产生随机数据。

5.1 Class Variables类变量

  • seed
    Sets the seed for the random number generator.
    设置用于随机数生成器的种子。
  • value
    Returns a random number between 0.0 [inclusive] and 1.0 [inclusive] (Read Only).
    返回一个随机数,在0.0(包括)~1.0(包括)之间。(只读)
  • insideUnitSphere
    Returns a random point inside a sphere with radius 1 (Read Only).
    返回半径为1的球体内的一个随机点。(只读)
  • insideUnitCircle
    Returns a random point inside a circle with radius 1 (Read Only).
    返回半径为1的圆内的一个随机点。(只读)
  • onUnitSphere
    Returns a random point on the surface of a sphere with radius 1 (Read Only).
    返回半径为1的球体在表面上的一个随机点。(只读)
  • rotation
    Returns a random rotation (Read Only).
    返回一个随机旋转角度。(只读)

5.2 Class Functions类函数

  • Range
    Returns a random  
         返回一个随机浮点数,在min(包含)和max(包含)之间。(只读)

#pragma strictfunction Start () {var a:float;var b:int;var c:float;a = Random.value;Debug.Log("a=" + a);b = Random.Range(1,100);Debug.Log("[1,100) b=" + b);c = Random.Range(0.0,10.0);Debug.Log("[0.0,10.0] c=" + c);}function Update () {}

      Random.Range产生的浮点随机数,可以包含最小最大范围,而产生的整型随机数,只包含最小范围,而不包含指定的最大范围。

6. C#脚本编写

    在Unity中,所有的脚本(JavaScript、C#和Boo Script)都继承自MonoBehaviour这个类。在JavaScript脚本中,系统自动继承MonoBehaviour这个类,而在C#和Boo脚本中,我们必须精确的声明类之间的继承关系。






 •

 








0 0
原创粉丝点击