U3D GameObject 解读

  GameObject自己没有功能,是Unity场景里全部组件的基类,但不少时候咱们须要在脚本中操做GameObject。先讲一下GameObject类包含哪些内容,其中经常使用的用红色标出了html

 

Variables 变量

activeInHierarchy Is the GameObject active in the scene?
场景中的游戏对象是否激活?
activeSelf The local active state of this GameObject. (Read Only)
该游戏对象的局部激活状态。(只读)
isStatic Editor only API that specifies if a game object is static.
若是一个游戏对象是静态仅在编辑器API指定。
layer The layer the game object is in. A layer is in the range [0…31].
游戏对象所在的层,层的范围是在[0…31]之间。
scene Scene that the GameObject is part of.
场景物体。
tag The tag of this game object.
这个游戏对象的标签。
transform The Transform attached to this GameObject. (null if there is none attached).
附加于这个游戏对象上的变换。(若是没有则为空)

Constructors 构造器

GameObject Creates a new game object, named name.
建立一个新的游戏物体,命名为name。

Functions 函数

AddComponent Adds a component class named /className/ to the game object.
添加一个名称为className的组件到游戏对象。
BroadcastMessage Calls the method named /methodName/ on every MonoBehaviour in this game object or any of its children.
对此游戏对象及其子对象的全部MonoBehaviour中调用名称为methodName的方法。
CompareTag Is this game object tagged with /tag/?
此游戏对象是否被标记为tag标签?
GetComponent Returns the component of Type /type/ if the game object has one attached, null if it doesn't.
若是这个游戏对象附件了一个类型为type的组件,则返回该组件,不然为空。
GetComponentInChildren Returns the component of Type /type/ in the GameObject or any of its children using depth first search.
返回此游戏对象或者它的全部子对象上(深度优先)的类型为type的组件。
GetComponentInParent Finds component in the parent.
从父对象查找组件。
GetComponents Returns all components of Type /type/ in the GameObject.
返回该游戏对象全部type类型的组件列表。
GetComponentsInChildren Returns all components of Type /type/ in the GameObject or any of its children.
返回此游戏对象与其子对象全部type类型的组件。
GetComponentsInParent Returns all components of Type /type/ in the GameObject or any of its parents.
返回此游戏对象与其父对象全部type类型的组件。
SampleAnimation Samples an animation at a given time for any animated properties.
用于任何动画剪辑在给定的时间采样动画。
SendMessage Calls the method named /methodName/ on every MonoBehaviour in this game object.
在这个游戏物体上的全部MonoBehaviour上调用名称为methodName的方法。
SendMessageUpwards Calls the method named /methodName/ on every MonoBehaviour in this game object and on every ancestor of the behaviour.
在这个游戏物体及其祖先物体的全部MonoBehaviour中调用名称为methodName的方法。
SetActive Activates/Deactivates the GameObject.
激活/停用此游戏对象。

Static Functions 静态函数

CreatePrimitive Creates a game object with a primitive mesh renderer and appropriate collider.
建立一个带有原型网格渲染器和适当的碰撞器的游戏对象。
Find Finds a game object by /name/ and returns it. 
找到并返回一个名字为name的游戏物体。
FindGameObjectsWithTag Returns a list of active GameObjects tagged /tag/. Returns empty array if no GameObject was found. 
返回具体tag标签的激活的游戏对象列表,若是没有找到则为空。
FindWithTag Returns one active GameObject tagged /tag/. Returns null if no GameObject was found. 
返回标记为tag的一个游戏对象,若是没有找到对象则为空。

 
下面举几个例子
 
   public static GameObject Find(string name)
    Find找到并返回一个名字为name的游戏物体,若是以name为名字的游戏物体没有被找到,则返回空.。  若是name中包含'/'字符,这个名称将被视做一个hierarchy中的路径名,好比 GameObject.Find("animals/cat")就只会查到父物体名字为animals的cat物体。. 这个函数只返回活动的游戏物体。  
   除非无可奈何,建议不要在每一帧中使用这个函数。由于其效率比较低,通常的作法是,在Awake或Start方法中经过Find找到,而后保存到成员变量中。
   注:Find查找时,只要是当前场景下的物体默认均可以查找到,并且无论是将脚本挂在那个对象上均可以查找到
    若是有多个同名对象,只能查找到一个
 
  那么如何查找active为false的物体呢?
    一、不要使用Find,直接在代码中经过public的变量关联物体。
    二、不要设置物体的active为false,先在游戏最开始时经过代码找到物体,而后经过代码设为false
    三、经过transform.Find
 
  下面例子演示怎么查找到名为Hand的GameObject物体
using UnityEngine;

using System.Collections;

 

public class ExampleClass : MonoBehaviour {

    public GameObject hand;

    void Example() {

        hand = GameObject.Find("Hand");

        hand = GameObject.Find("/Hand");

        hand = GameObject.Find("/Monster/Arm/Hand");

        hand = GameObject.Find("Monster/Arm/Hand");

    }

}

  FindWithTag用法与此相似,这里就不举例了数组

 

  public Component GetComponent(Type type)
app

  type 表示检索数组的类型编辑器

  若是这个游戏对象附件了一个类型为type的组件,则返回该组件,不然为空。ide

using UnityEngine;

public class GetComponentExample : MonoBehaviour
{
    void Start()
    {
        HingeJoint hinge = gameObject.GetComponent(typeof(HingeJoint)) as HingeJoint;

        if (hinge != null)
            hinge.useSpring = false;
    }
}

  

  public T GetComponent()   函数

using UnityEngine;

public class GetComponentGenericExample : MonoBehaviour
{
    void Start()
    {
        HingeJoint hinge = gameObject.GetComponent<HingeJoint>();

        if (hinge != null)
            hinge.useSpring = false;
    }
}

 

  public Component GetComponent(string type)动画

     type 表示检索数组的类型

       若是这个游戏对象附件了一个类型为type的组件,则返回该组件,不然为空。this

using UnityEngine;

public class GetComponentNonPerformantExample : MonoBehaviour
{
    void Start()
    {
        HingeJoint hinge = gameObject.GetComponent("HingeJoint") as HingeJoint;

        if (hinge != null)
            hinge.useSpring = false;
    }
}

   

  当你把脚本挂在一个物体上以后,脚本中的gameObject表明该物体,例如能够经过gameObject.name来获取脚本挂载对象的名字spa

相关文章
相关标签/搜索