Several ways to get game objects in Unity

When learning how to get objects and components, first explain what objects, components and objects are.

Object: Everything displayed in the hierarchy in unity can be called an object

Components: Unity provides a large number of components that have been written, such as rigid body, collision body, etc., and the script written by yourself is also a component class

Object: The script mounted on the object is an instantiated component, that is, an object

A game object is an object; a script that is not mounted on the object is an uninstantiated class, and when the script is mounted on the object, it becomes a real object

method to get the component

(1)GetComponent()

Rigidbody2D M_Rigidbody2D; 
void Start()
{
    //获取到组件后,将它的 引用!! 保存在M_Rigidbody2D字段中,方便下次使用
    M_Rigidbody2D = gameobject.GetComponent<Rigidbody2D>();
   //这是很常见的获取组件的代码,gameobject是从当前脚本组件获取到游戏物体,GetComponent就是从游戏物体中获取到指定的组件
}

(2) GetComponents(): This method is similar to the above method, but its return value is an array, which is used to get multiple components of the same type on a game object and get all components of the same type

method to get the object

(1) GameObject.Find(): Get the object by the name of the object

There are two disadvantages. First, GameObject.Find() cannot find inactive objects. Second, GameObject.Find() needs to traverse all objects in the scene

(2) GameObject.FindGameObjectWithTag(): Find objects through tags, this method returns the game object

There is also a CompareTag() method, which returns true or false, which is the same as GameObject.tag==""

(3) Parent-child relationship to obtain object objects

transform.Find("the name of the child object") finds the child object

transform.GetChild() Find child objects by serial number

Get public variables to get objects and components

When obtaining the objects and components described above, they are all assigned in the script. In fact, they can also be obtained in the unity compiler.

(1) Get objects (most commonly used)

(2) Get components

The acquisition of components is also similar to the acquisition of objects, but there is also an assignment bar for components in the inspector window, but the difference is that instead of dragging the component to assign it, and dragging the game object with the component to assign it, after all The purpose of the obtained component is to obtain a certain component on the game object

Summarize

获取游戏对象和组件方法其实有很多,上面的方法也只是列举的比较常用的,在获取物体时,脚本在物体上时--直接用GameObject就能获取,脚本不在物体上--用public公共变量获取;在获取组件时,脚本(其实脚本也算是组件)如果和组件在同一个对象上--用gameobject.GetComponent;如果脚本和组件不在同一个对象上--用public公共变量获取

现在我才发现,在使用public方法对变量或者组件,对象进行赋值时,当游戏物体被制作成预制件之后,通过预制件的方式用到场景中去,之前使用public赋值的方法需要重新再拖曳赋值,比较麻烦,因此再这里再详细介绍下通过

代码来查找物体的方法:

(1)GameObject.Find(string name)//按名字寻找对象,1.只能找到激活对象,2 尽量保证名字是唯一的,不唯一可以用路径 3.效率低会遍历场景中的所有对象

(2)GameObject.FindWithTag(string tag);//按标签寻找对象,1.只能找到激活对象

(3)GameObject.FindGameObjectWithTag(string tag)//按标签寻找对象,1.只能找到激活对象

//注意FindGameObjectWithTag和FindWithTag的作用是一样的,只是名字不一样

(4)GameObject.FindGameObjectsWithTag(string tag)//按标签寻找对象数组,1.只能找到激活对象

(5)transform.Find()//按名字寻找子物体的对象,1.可用于未激活物体,2.多重子物体时需写全路径

(6)transform.GetChild()//按索引寻找子物体的对象,1.可用于未激活物体2.多重子物体可以多次使用GetChild()

绝大部分寻找到物体就是为了获取该物体身上的组件,有时候可以直接用代码获取

gameObject.GetComponent//通过以上方法获取对象后,再结合这个方法就能获取到想要的组件

//当想要获取游戏子对象上的组件时,可以先不用获取到子对象再获取组件,可以直接用下面的方法获取组件

(1)transform.parent//得到父对象的transform组件

(2)transform.GetComponentInChildren<T>()//获取子物体对应的组件,1.可用于未激活物体,不过需要参数里面写true,注意:这里不能直接获取<GameObject>,具体原因我也不是太清楚

(3)transform.GetComponentsInChildren<T>()//获取子物体对应的组件,返回值是数组

(4)transform.GetChildCount()//被弃用了,现在是通过transform.childCount来获取子对象的个数

Guess you like

Origin blog.csdn.net/qq_62947569/article/details/128588952