Unity: transform.GetComponent Vs. GameObject.GetComponent?

I was wondering what the difference was between the transform and gameobject getcomponents.

I looked at this resource here: https://answers.unity.com/questions/1360282/what-is-the-difference-between-gameobjectgetcompon.html
【First of all understand the relationship between GameObject and Component.
A GameObject is a container; you add pieces to the GameObject container to make it into a character, a light, a tree, a sound, or whatever else you would like it to be. Each piece you add is called a component.

Depending on what kind of object you want to create, you add different combinations of components to a GameObject. You can think of a GameObject as an empty cooking pot, and components as different ingredients that make up your recipe of gameplay. Unity has lots of different built-in component types, and you can also make your own components.

GameObjects are all the same type.

Components come in many types.

GameObjects contain (instances of) components.

Every GameObject contains a Transform component.

Every component can access its gameObject.

Every component also get convenience accessors (properties) that retrieve the corresponding properties of its gameObject, including .transform, as you have observed.

Both the GameObject class and the Component class have a number of properties that are “shortcuts” to (other) components.

 myGameObject.transform is a shortcut for myGameObject.GetComponent().
 myComponent.transform is a shortcut for myComponent.gameObject.GetComponent()

the GetComponent function exists on the Component class as well. At the End both are same!!】

It states that: “At the end, both are the same”

Surely there must be a difference between the two?

ANSWER1:
They are equivalent.

Keep in mind Unity’s component-based architecture: almost everything that exists in the scene is a GameObject which usually has one or more Components attached to it. A Component must be attached to a GameObject in order to exist.

The GetComponent method, really an entire family of methods, allows scripts to look up references to specific Components.

If you call GetComponent on a GameObject (such as gameObject.GetComponent), it will find a matching Component attached to that GameObject.

If you call GetComponent on a Component (such as transform.GetComponent), it will find the GameObject that component is attached to, then find a matching Component attached to that GameObject. This is the same result, but it’s a convenient shorthand to save you the effort of putting gameObject references all over your scripts.

Technically these are separate methods, but the results and semantics are essentially identical. In practice you can use whichever form you are more comfortable with.

一,概述
   我们在monoDevelop中书写脚本语言时,GameObject与gameobject, Transform和transform是同时存在的如下图, 那么它们有什么区别和联系呢?
   1>GameObject与gameObject
   
   2>Transform与transform
   
二,GameObject与gameObject
      Gameobject是一个类型,所有的游戏物件都是这个类型的对象。
      gameobject是一个对象, 就跟java里面的this一样, 指的是这个脚本所附着的游戏物件
      示例:     
public class ShowSliderValue : MonoBehaviour
{
private GameObject obje; //定义GameObject类型的指针
void Start(){
Text lal =gameObject.GetComponent (); //通过gameObject获取到Text组件.
Debug.Log (“Text” + lal.text); //打印获取到组件的中的text的属性
}
}
打印结果:
  Text中的值
  
  输出台:

注意:
  Text  lal =gameObject.GetComponent () 中不使用gameObject , 直接通过GetComponent (),也是可以的.

三,Transform与transform
     Transform是一个类,用来描述物体的位置,大小,旋转等等信息。
     transform是Transform类的对象,依附于每一个物体。也是当前游戏对象的一个组件(每个对象都会有这个组件).
四,transform与gameObject
     1>二者的含义
        transform :  当前游戏对象的transform组件
    gameobject :当前游戏对象的实例
     2>两者的联系和区别
        * 在unity中每个游戏对象都是一个gameobject. monodevelop中的gameobject就代表着本脚本所依附的对象. 每个gameobject都包含各种各样的组件,但从这点可以看出transform是gameobject的一个组件,控制着gameobject的位置,缩放,和旋转,而且每个gameobject都有而且必有一个transform组件
        * gameobject.find用来获取场景中那个我们需要查找的对象(object)。而transform.find方法则是获取当前对象的子对象下我们需要获取的目标对象位置信息。
       
        * 注意:  在update()中尽量不使用find()方法,影响性能.
     3>gameobject.transform与transform.gameobject
        *  gameobject.transform,是获取当前游戏对象的transform组件.
            所以在start函数中 gameobject.transform 和this.transform,指向的都是同一个对象。即:gameobject.transform == this.transform == transform
        * transform.gameobject:可以这么理解为:获取当前transform组件所在的gameobect
           所以在start函数中()transform.gameobject == this.gameobject == gameobect
       示例:

public class ShowSliderValue : MonoBehaviour
{
private GameObject obje; //定义GameObject类型的指针
private Transform trans;//定义Transform类型的指针

void Start(){
	Debug.Log ("gameObject.name:" + gameObject.name);
	Debug.Log ("gameObject.transform.gameObject.name:" + gameObject.transform.gameObject.name);
	Debug.Log ("ThisGame.name:" + this.gameObject.name);
}

}
 打印结果:

点赞 8
————————————————
版权声明:本文为CSDN博主「等你左岸右岸」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lxl_815520/article/details/53638481

你可能感兴趣的:(C#,unity)