The use of GameObject in Unity

GameObject game object

1. Creation method

(1). Use the constructor (declaration + instantiation) to create an empty game object

 void Start()
    {
    
    
        GameObject myGo = new GameObject("MyGameObject");
        //运行unity后会在Hierarchy窗口创建一个"MyGameObject"游戏对象
    }

(2). Instantiate based on existing prefab (game object) resources or existing game objects in the game scene, which is equivalent to cloning.

Operation tips: You need to create a GameObject in the Hierarchy window and drag the object to the script.

public class No3_GameObject : MonoBehaviour
{
    
    
    public GameObject gb;
    void Start()
    {
    
    
        GameObject.Instantiate(gb);
        //运行unity后会在Hierarchy窗口创建一个游戏对象(clone)
    }
}

(3). Use special APIs to create some basic game object types (original geometry)

void Start()
    {
    
    
        GameObject.CreatePrimitive(PrimitiveType.Plane);
       //运行unity后会在Hierarchy窗口创建一个Plane物体
    }

2. Acquisition and search of game objects

(1) To yourself (this.gameObject)

    void Start()
    {
    
    
        Debug.Log("当前脚本挂载到的游戏物体名称是:" + gameObject.name);
        //运行unity后会在控制台输出当前脚本所挂载的游戏对象名称
        Debug.Log("当前游戏物体标签是:"+gameObject.tag);
        //运行unity后会在控制台输出当前脚本所挂载的游戏物体标签
        Debug.Log("当前游戏物体层级是:"+gameObject.layer);
        //运行unity后会在控制台输出当前脚本所挂载的游戏物体层级
    }

(2) There are references to other game objects (there are objects referenced by scripts)

public class No3_GameObject : MonoBehaviour
{
    
    
    public GameObject gb;
    void Start()
    {
    
    
        //有引用,对其他游戏物体
        Debug.Log("gris游戏物体的状态是:" + gb.activeSelf);
        //运行unity后会在控制台输出gb游戏物体的状态 
    }
}

(3) Not directly referenced, to other game objects

Attention! Search for other game objects (the game object must be active at this time)
a. Search by name

void Start()
    {
    
    
        GameObject mainCameraGo= GameObject.Find("Main Camera");
        Debug.Log("mainCamera游戏物体的标签是:" + mainCameraGo.tag);
        //运行unity后会在控制台输出MainCamera物体的标签
    }

b. Search by tag

void Start()
    {
    
    
        GameObject mainCameraGo = GameObject.FindGameObjectWithTag("MainCamera");
        Debug.Log("mainCamera游戏物体的名字是:" + mainCameraGo.name);
        //运行unity后会在控制台输出MainCamera物体的名称
    }

c. Search by type

Type refers to the name of the current component, here it is the class name, search by class name

Note: You need to create a script class here. The no2_EventFunction class is created here.

    void Start()
    {
    
    
        No2_EventFunction no2_EventFunction= GameObject.FindObjectOfType<No2_EventFunction>();
        Debug.Log("no2_EventFunction游戏物体的名字是:" + no2_EventFunction.name);
    }

d. Most searches and acquisitions
Note:
First paragraph: Here you need to create a label and set it in the created game object Label, the Enemy label is created here.
Second paragraph: Several game objects need to be selected to add the BoxCollider component

void Start()
    {
    
    
        //将查找到的标签为Enemy的游戏对象放入enemyGos数组
        GameObject[] enemyGos= GameObject.FindGameObjectsWithTag("Enemy");
        for (int i = 0; i < enemyGos.Length; i++)
        {
    
    
            //遍历查找到的游戏物体名称并输出
            Debug.Log("查找到的敌人游戏物体名称是:"+enemyGos[i].name);
        }
        Debug.Log("--------------------------------------------------");
        
        //将查找到的类型为BoxCollider的游戏组件放入colliders数组
        BoxCollider[] colliders= GameObject.FindObjectsOfType<BoxCollider>();
        
        for (int i = 0; i < colliders.Length; i++)
        {
    
    
            Debug.Log("查找到的敌人碰撞器名称是:" + colliders[i].name);
        }
    }

Guess you like

Origin blog.csdn.net/qq_43007056/article/details/130767762