描述
Unity 场景中所有实体的基类。
静态函数
创建物体
如果要创建一个可见的物体,比如创建一个球体或者创建一个方块,通过GameObject这个类的一个静态方法可以达到这个目的的public static GameObject CreatPrimitive(PrimitiveType)可以创建五种类型的基础模型。
还可以创建那么几个物体。
寻找物体并获取组件,改变旋转角度
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameObjectTest : MonoBehaviour{public GameObject hand;void Start(){//查找到hand这个物体并旋转hand = GameObject.Find("hand");}void Update(){//查找到hand这个物体并旋转hand.transform.Rotate(100 * Time.deltaTime, 100 * Time.deltaTime, 0);}}
遍历寻找模型,但并没有找到模型,需要抛出
返回标签为 tag 的活动 GameObjects 的数组。如果未找到任何 GameObject,则返回空数组。
标签在使用前必须在标签管理器中加以声明。如果此标签不存在,或者传递了空字符串或 null 作为标签,则将抛出 UnityException。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameObjectTest : MonoBehaviour{public GameObject respawnPrefab;public GameObject[] respawns;//复位的意思void Start(){//遍历寻找模型,但并没有找到模型,需要抛出//在指定位置实例化respawnPrefab//所有标记为“Respawn”的游戏对象。if (respawns == null)respawns = GameObject.FindGameObjectsWithTag("Respawn");foreach (GameObject respawn in respawns){Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);}}void Update(){}}
其他函数
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameObjectTest : MonoBehaviour{public GameObject obj;public GameObject player;public Camera camera;void Start(){GameObject.Find("Unity");//从scence下开始查找,根据GameObject的名字进行查找,允许使用“/"穿越层次查找//GameObject.FindWithTag("Player");//根据tag查找一个GameObjectplayer = GameObject.FindWithTag("Player");//这样就可以在面板看到是否获取到GameObject.FindGameObjectsWithTag("Player");//根据tag批量去查找GameObjectcamera = GameObject.FindObjectOfType<Camera>();//查找带有camera组件的物体而且只找一个GameObject.Destroy(obj);//删除objGameObject.DontDestroyOnLoad(obj);//加载下一个场景的时候不要删除objCamera[] allcamera = GameObject.FindObjectOfType<Camera>();//查找所以带有camera组件的物体返回,集合bool b = GameObject.Equals(obj, obj);//判断两个物体是否一样返回 布尔类型}void Update(){}}
汇总
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GameObjectTest : MonoBehaviour{public GameObject hand;public GameObject respawnPrefab;public GameObject[] respawns;//复位的意思public GameObject obj;public GameObject player;public Camera camera;public bool isStatic;// Start is called before the first frame updatevoid Start(){new GameObject();//这样运行游戏的时候里面就有一个空物体了new GameObject("啊这");//这可以给新建的物体取名new GameObject().name = "啊这2";//静态类创建物体GameObject.CreatePrimitive(PrimitiveType.Cube);//创建一个方块GameObject.CreatePrimitive(PrimitiveType.Capsule);GameObject.CreatePrimitive(PrimitiveType.Cylinder);//查找到hand这个物体并旋转hand = GameObject.Find("hand");//遍历寻找模型,但并没有找到模型,需要抛出//在指定位置实例化respawnPrefab//所有标记为“Respawn”的游戏对象。if (respawns == null)respawns = GameObject.FindGameObjectsWithTag("Respawn");foreach (GameObject respawn in respawns){Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);}GameObject.Find("Unity");//从scence下开始查找,根据GameObject的名字进行查找,允许使用“/"穿越层次查找//GameObject.FindWithTag("Player");//根据tag查找一个GameObjectplayer = GameObject.FindWithTag("Player");//这样就可以在面板看到是否获取到GameObject.FindGameObjectsWithTag("Player");//根据tag批量去查找GameObjectcamera = GameObject.FindObjectOfType<Camera>();//查找带有camera组件的物体而且只找一个GameObject.Destroy(obj);//删除objGameObject.DontDestroyOnLoad(obj);//加载下一个场景的时候不要删除objCamera[] allcamera = GameObject.FindObjectsOfType<Camera>();//查找所以带有camera组件的物体返回,集合bool b = GameObject.Equals(obj, obj);//判断两个物体是否一样返回 布尔类型//////gameObject.isStatic = true;//获取并设置GameObject是否为静态}// Update is called once per framevoid Update(){//查找到hand这个物体并旋转hand.transform.Rotate(100 * Time.deltaTime, 100 * Time.deltaTime, 0);//需要在Updata执行,因为需要每一帧都执行player.gameObject.isStatic = isStatic;//获取并设置GameObject是否为静态gameObject.SetActive(true);//设置物体是否启动gameObject.AddComponent<Camera>();//给物体添加一个组件gameObject.tag = "Player";//给物体设置标签gameObject.GetComponentInChildren<Camera>();//寻找子物体是否有Camera组件gameObject.GetComponentInParent<Camera>();//寻找父物体是否有Camera组件gameObject.GetComponents<Camera>();//寻找这个物体所有的Camera组件gameObject.GetComponent<Camera>();//寻找这个物体的Camera组件}}
