[Unity Basics] What is a GameObject

environment:

  • UnityHub3.3.0-c8
  • Unity2021.3.20f1c1 LTS
  • Visual Studio 2022

What is a GameObject

In Unity, GameObjectis the most basic entity in the game. It can be seen as a container for all other objects in the game scene. GameObjectCan contain many different components, such as Transform、Renderer、Collider``等。通过添加这些组件,GameObjectcan have different properties and functions, such as moving, rotating, displaying and interacting with other objects.

In Unity, by creating, instantiating and managing GameObjectobjects, a complete game scene can be constructed.

create

First create a 2D project by yourself

find a Hierarchycolumn named

insert image description here

右键

insert image description here

It will automatically create a new GameObjectsubject named . To view it, you need to select MoveTooland click on the subject.

insert image description here

Choosing an Icon

We can perform and select operations Inspectoron it in the bar . Note that for empty objects, the previously selected content will be automatically displayed for the project that has just been started .renameselect iconselect icon

But Hierarchyonce the empty object is clicked and moved, this will not be displayed select icon, and replaced with other objects, such as cubethe like, can be select icondisplayed. I don’t know if this is a problem with the latest version.

Please add a picture description

Alternatively, we can Gameview it in the

insert image description here

Of course, iconwe can also choose our own set of pictures in

insert image description here

GameObject Components

点击空对象->看右侧Inspector栏->Add Component->Sprite Renderer

You can Spriteclick on our own sprites. The content that makes us excited or happy to hear and see all starts with a set of pictures.

insert image description here

If you want to add the picture we want, you only need to put the picture Projectyou Assetswant in , and pull the picture into Spritethe position of the picture below, and it defaults toBackground

insert image description here

GameObject Code

Project\AssetsCreate a C# Scriptscript in the right click ->rename自己想要的名字

insert image description here

After that, throw the C# script you created into Hierarchythe object you are creating, and you can perform debugging operations. The initial code content is as follows:

insert image description here

important point:

  • The name of the class must be consistent with the name of your script. If you don’t start , the content in VSrename will not change after the creation is completed rename, and you can’t enter it at this time.addComponentGameObject

Dynamically Create GameObject

If you want to be able to create by code GameObject, you can write like this:

void Start(){
    
    
    new GameObject("MyNewGameObject");
}

Afterwards, run Add Componentany object of this script in , and a new column named " will be added , and it will disappear after running.unityHierarchyMyNewGameObjectGameObject

If you want this new addition GameObjectto have addComponentother content, for example Sprite Renderer, the implementation method is as follows:

void Start(){
    
    
    GameObject myGo = new GameObject("MyNewGameObject");
    myGo.AddComponent<SpriteRenderer>();
}

or write like this

GameObject myGo = new GameObject("MyNewGameObject",typeof(SpriteRenderer));

Please add a picture description

Guess you like

Origin blog.csdn.net/kokool/article/details/129863945