描述

Unity 场景中所有实体的基类。

静态函数

image.png

创建物体

如果要创建一个可见的物体,比如创建一个球体或者创建一个方块,通过GameObject这个类的一个静态方法可以达到这个目的的public static GameObject CreatPrimitive(PrimitiveType)可以创建五种类型的基础模型。
还可以创建那么几个物体。
image.png

寻找物体并获取组件,改变旋转角度

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameObjectTest : MonoBehaviour
  5. {
  6. public GameObject hand;
  7. void Start()
  8. {
  9. //查找到hand这个物体并旋转
  10. hand = GameObject.Find("hand");
  11. }
  12. void Update()
  13. {
  14. //查找到hand这个物体并旋转
  15. hand.transform.Rotate(100 * Time.deltaTime, 100 * Time.deltaTime, 0);
  16. }
  17. }

遍历寻找模型,但并没有找到模型,需要抛出

返回标签为 tag 的活动 GameObjects 的数组。如果未找到任何 GameObject,则返回空数组。
标签在使用前必须在标签管理器中加以声明。如果此标签不存在,或者传递了空字符串或 null 作为标签,则将抛出 UnityException。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameObjectTest : MonoBehaviour
  5. {
  6. public GameObject respawnPrefab;
  7. public GameObject[] respawns;//复位的意思
  8. void Start()
  9. {
  10. //遍历寻找模型,但并没有找到模型,需要抛出
  11. //在指定位置实例化respawnPrefab
  12. //所有标记为“Respawn”的游戏对象。
  13. if (respawns == null)
  14. respawns = GameObject.FindGameObjectsWithTag("Respawn");
  15. foreach (GameObject respawn in respawns)
  16. {
  17. Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
  18. }
  19. }
  20. void Update()
  21. {
  22. }
  23. }

其他函数

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameObjectTest : MonoBehaviour
  5. {
  6. public GameObject obj;
  7. public GameObject player;
  8. public Camera camera;
  9. void Start()
  10. {
  11. GameObject.Find("Unity");//从scence下开始查找,根据GameObject的名字进行查找,允许使用“/"穿越层次查找
  12. //GameObject.FindWithTag("Player");//根据tag查找一个GameObject
  13. player = GameObject.FindWithTag("Player");//这样就可以在面板看到是否获取到
  14. GameObject.FindGameObjectsWithTag("Player");//根据tag批量去查找GameObject
  15. camera = GameObject.FindObjectOfType<Camera>();//查找带有camera组件的物体而且只找一个
  16. GameObject.Destroy(obj);//删除obj
  17. GameObject.DontDestroyOnLoad(obj);//加载下一个场景的时候不要删除obj
  18. Camera[] allcamera = GameObject.FindObjectOfType<Camera>();//查找所以带有camera组件的物体返回,集合
  19. bool b = GameObject.Equals(obj, obj);//判断两个物体是否一样返回 布尔类型
  20. }
  21. void Update()
  22. {
  23. }
  24. }

汇总

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameObjectTest : MonoBehaviour
  5. {
  6. public GameObject hand;
  7. public GameObject respawnPrefab;
  8. public GameObject[] respawns;//复位的意思
  9. public GameObject obj;
  10. public GameObject player;
  11. public Camera camera;
  12. public bool isStatic;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. new GameObject();//这样运行游戏的时候里面就有一个空物体了
  17. new GameObject("啊这");//这可以给新建的物体取名
  18. new GameObject().name = "啊这2";
  19. //静态类创建物体
  20. GameObject.CreatePrimitive(PrimitiveType.Cube);//创建一个方块
  21. GameObject.CreatePrimitive(PrimitiveType.Capsule);
  22. GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  23. //查找到hand这个物体并旋转
  24. hand = GameObject.Find("hand");
  25. //遍历寻找模型,但并没有找到模型,需要抛出
  26. //在指定位置实例化respawnPrefab
  27. //所有标记为“Respawn”的游戏对象。
  28. if (respawns == null)
  29. respawns = GameObject.FindGameObjectsWithTag("Respawn");
  30. foreach (GameObject respawn in respawns)
  31. {
  32. Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
  33. }
  34. GameObject.Find("Unity");//从scence下开始查找,根据GameObject的名字进行查找,允许使用“/"穿越层次查找
  35. //GameObject.FindWithTag("Player");//根据tag查找一个GameObject
  36. player = GameObject.FindWithTag("Player");//这样就可以在面板看到是否获取到
  37. GameObject.FindGameObjectsWithTag("Player");//根据tag批量去查找GameObject
  38. camera = GameObject.FindObjectOfType<Camera>();//查找带有camera组件的物体而且只找一个
  39. GameObject.Destroy(obj);//删除obj
  40. GameObject.DontDestroyOnLoad(obj);//加载下一个场景的时候不要删除obj
  41. Camera[] allcamera = GameObject.FindObjectsOfType<Camera>();//查找所以带有camera组件的物体返回,集合
  42. bool b = GameObject.Equals(obj, obj);//判断两个物体是否一样返回 布尔类型
  43. ////
  44. //gameObject.isStatic = true;//获取并设置GameObject是否为静态
  45. }
  46. // Update is called once per frame
  47. void Update()
  48. {
  49. //查找到hand这个物体并旋转
  50. hand.transform.Rotate(100 * Time.deltaTime, 100 * Time.deltaTime, 0);
  51. //需要在Updata执行,因为需要每一帧都执行
  52. player.gameObject.isStatic = isStatic;//获取并设置GameObject是否为静态
  53. gameObject.SetActive(true);//设置物体是否启动
  54. gameObject.AddComponent<Camera>();//给物体添加一个组件
  55. gameObject.tag = "Player";//给物体设置标签
  56. gameObject.GetComponentInChildren<Camera>();//寻找子物体是否有Camera组件
  57. gameObject.GetComponentInParent<Camera>();//寻找父物体是否有Camera组件
  58. gameObject.GetComponents<Camera>();//寻找这个物体所有的Camera组件
  59. gameObject.GetComponent<Camera>();//寻找这个物体的Camera组件
  60. }
  61. }