[Unity] API learning --> Three ways to create GameObject (object)

GameObjectIt is the most critical object in Unity

How to create objects:

1 Created through the constructor

  • You can directly new a GameObject
  • Created in the start function, the game object can be created anywhere, and the test is created once
  • By default only Transform
  • You can pass parameters when creating a new object to create a specified object
    void Start() {
    
    
        new GameObject();
        new GameObject("Cube");//创建一个正方体
    }

2 Instance

  • Instantiate is a static method that can be GameObject.Instantiate();called by and needs to pass a prefab (prefab).
  • The Transform property of the created object can be assigned directly
  • It can be found that there is a Clone behind the newly created game object, indicating that it was cloned through a prefab. He can also be cloned based on game objects.
    void Start() {
    
    
        Instantiate(gameObject,transform);
    }

3 CreatePrimitive

  • Create basic geometry
  • CreatePrimitive (PrimitiveType type);
  • Pass in a geometry type
  • The Transform property is the default property
	void Start() {
    
    
        GameObject.CreatePrimitive(PrimitiveType.Cube);
    }

Summarize

  • The construction method is generally used to create an empty game object
  • InstanceUsed to instantiate various things like special effects, game characters, etc.
  • CreatePrimitiveCan create some basic geometry

Guess you like

Origin blog.csdn.net/ainklg/article/details/129767442