当前位置:   article > 正文

Unity API 解析—— GameObject 类_unity getcomponent无法获取指定组件

unity getcomponent无法获取指定组件

GameObject 类是Unity场景中所有实体的基类。一个GameObject对象通常由多个组件component组成,且至少含有一个transform组件。

activeSelf 属性 —— GameObject的Active标识

activeInHierarchy 属性的功能是返回GameObject实例在程序运行时的激活状态,它只有当GameObect实例的状态被激活时才会返回true。而且它会受父类对象激活状态的影响。如果其父类至最顶层的对象中有一个对象未被激活,activeInHierarchy就会返回false


using UnityEngine;
using System.Collections;

public class ActiveSelf_ts : MonoBehaviour {
    public GameObject cube1, cube2, cube3;
    void Start () {
    // 对cube2设置为false,其他设置为true
    cube1.SetActive(true);
    cube2.SetActive(false);
    cube3.SetActive(true);

    Debug.Log("activeSelf:");
    // 尽管cube2被设置为false,但其子类cube3的activeSelf返回值仍然为true
    Debug.Log("cube1.activeSelf:" + cube1.activeSelf);
    Debug.Log("cube2.activeSelf:" + cube2.activeSelf);
    Debug.Log("cube3.activeSelf:" + cube3.activeSelf);

    Debug.Log("nactiveInHierarchy:");
    // cube2 和 cube3的activeInHierarchy返回值都为false
    Debug.Log("cube1.activeInHierarchy:" + cube1.activeInHierarchy);
    Debug.Log("cube2.activeInHierarchy:" + cube2.activeInHierarchy);
    Debug.Log("cube3.activeInHierarchy:" + cube3.activeInHierarchy);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

GameObject 构造方法

public GameObject();

public GameObject(string name);

public GameObject(string name,params Type[] components)

using UnityEngine;
using System.Collections;

public class Constructors_ts : MonoBehaviour {
    void Start () {
    // 使用构造函数 GameObject (name : String)
    GameObject g1 = new GameObject("G1");
    g1.AddComponent<Rigidbody>();
    // 使用构造函数 GameObject () 
    GameObject g2 = new GameObject();
    g2.AddComponent<FixedJoint>();
    // 使用构造函数 GameObject (name : String, params components : Type[])
    GameObject g3 = new GameObject("G3",typeof(MeshRenderer),typeof(Rigidbody),typeof(SpringJoint));

    Debug.Log("g1 name:" + g1.name + "nPosition:" + g1.transform.position);
    Debug.Log("g2 name:" + g2.name + "nPosition:" + g2.transform.position);
    Debug.Log("g3 name:" + g3.name + "nPosition:" + g3.transform.position);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

GameObject 类实例方法

GetComponent 方法 —— 获取组件

public T GetComponent() where T: Component;

public Component GetComponent(string type);

public Component GetComponent(Type type);

using UnityEngine;
using System.Collections;

public class GetComponent_ts : MonoBehaviour {
    void Start () {
    Debug.Log("以下是GetComponent 的相关使用代码。n GetComponet 方法用来获取当前GameObject中符合Type类型的第一个组件");
    //GetComponent (type : Type)
    Rigidbody rb = GetComponent(typeof(Rigidbody))as Rigidbody;
    Debug.Log("使用 GetComponent (type : Type) 获取 Rigidbody" + rb.GetInstanceID());

    //GetComponent.<T>()
    rb=GetComponent<Rigidbody>();
    Debug.Log("使用 GetComponent.<T>()获取 Rigidbody" + rb.GetInstanceID());

    //GetComponent (type : String)
    rb = GetComponent("Rigidbody") as Rigidbody;
    Debug.Log("使用 GetComponent (type : String)获取 Rigidbody" + rb.GetInstanceID());

    Debug.Log("以下是GetComponentInChildren的相关使用代码。n GetComponentInChildren 方法来获取当前GameObject的所有子类中符合Type类型的第一个组件");

    //GetComponentInChildren (type : Type)
    rb = GetComponentInChildren(typeof(Rigidbody)) as Rigidbody;
    Debug.Log("使用GetComponentInChildren (type : Type)获取Rigidbody " + rb.name);

    //GetComponentInChildren.<T> ()
    rb=GetComponentInChildren<Rigidbody>();
    Debug.Log("使用GetComponentInChildren.<T>()获取Rigidbody " + rb.name);

    //GetComponents (type : Type)
    Component[] cjs = GetComponents(typeof(ConfigurableJoint)) as Component[];
    foreach(ConfigurableJoint cj in cjs){
        Debug.Log("使用GetComponents (type : Type)获取ConfigurableJoint " + cj.GetInstanceID());
    }

    //GetComponents.<T> ()
    cjs = GetComponents<ConfigurableJoint>();
    foreach (ConfigurableJoint cj in cjs)
    {
        Debug.Log("使用GetComponents.<T>()获取ConfigurableJoint " + cj.GetInstanceID());
    }


    //GetComponentsInChildren(type: Type, includeInactive: boolean = false)
    cjs = GetComponentsInChildren(typeof(ConfigurableJoint), false) as Component[];
    foreach (ConfigurableJoint cj in cjs)
    {
        Debug.Log("使用GetComponentsInChildren(type: Type, false)获取ConfigurableJoint " + cj.name);
    }

    cjs = GetComponentsInChildren(typeof(ConfigurableJoint), true) as Component[];
    foreach (ConfigurableJoint cj in cjs)
    {
        Debug.Log("使用GetComponentsInChildren(type: Type, true)获取ConfigurableJoint " + cj.name);
    }

    //GetComponentsInChildren.<T> (includeInactive : boolean)
    cjs = GetComponentsInChildren<ConfigurableJoint>(true);
    foreach (ConfigurableJoint cj in cjs)
    {
        Debug.Log("使用GetComponentsInChildren.<T>(includeInactive : boolean)获取ConfigurableJoint " + cj.name);
    }

    //GetComponentsInChildren.<T> ()
    cjs = GetComponentsInChildren<ConfigurableJoint>();
    foreach (ConfigurableJoint cj in cjs)
    {
        Debug.Log("使用GetComponentsInChildren.<T>()获取ConfigurableJoint " + cj.name);
    }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

SendMessage 方法:发送消息

和自身同级的物体不会收到消息

SendMessageOptions 有两个可选方式:SendMessageOptions.RequireReceiver 和 SendMessageOptions.DontRequireReceiver 前者要求信息的接收方必须有接收信息的方法,否则程序报错,后者无此要求

BroadcastMessage 向自身以及所有子类发送消息,和自身同级的物体不会收到消息

SendMessageUpwards 方法的功能是向GameObject 自身及其所有父类发送消息,和自身同级的物体不会受到消息

using UnityEngine;
using System.Collections;

public class SendMessageUpward_ts : MonoBehaviour {
    void Start () {
        // 向父类及自己发送消息
        gameObject.SendMessageUpwards("GetChildrenMessage", gameObject.name + ":use SendMessageUpwards send!");

        // 向子类及自己发送消息
        gameObject.BroadcastMessage ("GetParentMessage ",gameObject.name +":use BroadcastMessage send");

        // 向自己发送消息
        gameObject.SendMessage ("GetSelfMessage",gameObject.name +" : use SendMessage send");
    }
    private void GetParentMessage(string str)
    {
        Debug.Log(gameObject.name + "收到父类发送的消息" + str);
    }

    private void GetSelfMessage(string str)
    {
        Debug.Log(gameObject.name + "受到自身发送的消息" + str);
    }

    private void GetChildrenMessage(string str)
    {
        Debug.Log(gameObject.name + "受到子类发送的消息" + str);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

CreatePrimitive —— 创建GameObject 对象

静态方法

using UnityEngine;
using System.Collections;

public class CreatePrimitive_ts : MonoBehaviour
{
    void Start()
    {
    // 使用GameObject.CreatePrimitive方法创建 GameObject
    GameObject g1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    g1.name = "G1";
    g1.tag = "sphere_Tag";
    // 使用 AddComponent (className : String)添加组件
    g1.AddComponent("SpringJoint");
    // 使用AddComponent (componentType : Type)添加组件
    g1.AddComponent(typeof(GUITexture));
    g1.transform.position = Vector3.zero;

    GameObject g2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    g2.name = "G2";
    g2.tag = "sphere_Tag";
    // 使用AddComponent.<T>() 添加组件
    g2.AddComponent<Rigidbody>();
    g2.transform.position = 2.0f * Vector3.right;
    g2.GetComponent<Rigidbody>().useGravity = false;

    GameObject g3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    g3.name = "G1";
    g3.tag = "sphere_Tag";
    g3.transform.position = 4.0f * Vector3.right;

    // 使用GameObject.FindÀ类方法获取GameObject,返回符合条件的第一个对象
    Debug.Log("use Find:" + GameObject.Find("G1").transform.position);
    // 使用GameObject.FindGameObjectWithTag类方法获取GameObject,返回符合条件的第一个对象
    Debug.Log("use FindGameObjectWithTag:" + GameObject.FindGameObjectWithTag("sphere_Tag").transform.position);
    // 使用GameObject.FindGameObjectsWithTag类方法获取GameObject,返回符合条件的所有对象
    GameObject[] gos = GameObject.FindGameObjectsWithTag("sphere_Tag");
    foreach (GameObject go in gos)
    {
        Debug.Log("use FindGameObjectsWithTag:" + go.name + ":" + go.transform.position);
    }

            // 更改 g1,g2,和g3的层级关系
    g3.transform.parent = g2.transform;
    g2.transform.parent = g1.transform;

    Debug.Log("use Find again1:" + GameObject.Find("G1").transform.position);
    // 使用“/” 限定条件的方式查找GameObject
    Debug.Log("use Find again2:" + GameObject.Find("/G1/G2/G1").transform.position);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

若要获取当前脚本所在GameObject对象中的某个组件,直接使用GetCompoent方法即可,如

Rigidbody br = GetComponent<Rigidbody>()

若要获取非当前脚本所在GameObject对象中的某个组件,则需要有GameObject作为前置引用,如变量go为指向GameObject对象的引用,则

Rigidbody br = go.GetComponent<Rigidbody>()

using UnityEngine;
using System.Collections;

public class GameObjectAndComponent_ts : MonoBehaviour {
    public GameObject sp;
    void Start () {
    //以下3种表达方式功能相同
    Rigidbody rb1 = rigidbody;
    Rigidbody rb2 = GetComponent<Rigidbody>();
    Rigidbody rb3 = rigidbody.GetComponent<Rigidbody>();
    Debug.Log("rb1µÄInstanceID£º" + rb1.GetInstanceID());
    Debug.Log("rb2µÄInstanceID£º" + rb2.GetInstanceID());
    Debug.Log("rb3µÄInstanceID£º" + rb3.GetInstanceID());

    // 使用前置引用获取引用对象的Rigidbody组件
    Debug.Log("前置引用sp对象中Rigidbody的InstanceID "+sp.GetComponent<Rigidbody>().GetInstanceID());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/93294
推荐阅读
相关标签
  

闽ICP备14008679号