기술 자료

Unity Engine

Download Unity

Unity Engine

MonoBehaviour components

Understand how to use MonoBehaviour to create script components that control the behavior of GameObjects and respond to events.
읽는 시간 4분최근 업데이트: 7일 전

The
MonoBehaviour
class provides the framework that allows you to attach your script to a GameObject in the Unity Editor. It also provides hooks into useful Events such as
Start
and
Update
.
You can create new MonoBehaviour scripts in the Editor as described in Create scripts.
For a complete reference of every member of the
MonoBehaviour
class, and its technical details, refer to the MonoBehaviour script reference.

Content of a MonoBehaviour script file

Double-click a script asset in Unity to open it in a text editor. By default, Unity uses Visual Studio, but you can select any editor you like from the External Tools Preferences window. To change your default editor, navigate to Edit > Preferences > External Tools (macOS: Unity > Settings > External Tools) and change the External Script Editor.
A new
MonoBehaviour
script file contains the following:
using UnityEngine;public class NewMonoBehaviourScript : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void Update() { }}
This script connects to low-level Unity Engine functionality by implementing a class derived from the built-in class called
MonoBehaviour
. Any
Monobehaviour
-derived class can be attached to GameObjects as a component type. Each time you attach a script component to a GameObject, Unity creates a new instance of the object defined by the script. Unity names the class after the file name you supply on creation of the script asset. It's best practice to keep the class name and file name in sync. For more information about script file names, refer to Naming considerations.
The main things to note are the two functions defined inside the class:
  • Start
    : Unity calls this function before gameplay begins and before calling the
    Update
    function for the first time. This function is the most appropriate place for initialization work.
  • Update
    : Handles the frame update for the GameObject. This might include movement, triggering actions and responding to user input, or anything that needs to be handled over time during gameplay. To enable the
    Update
    function to do its work, it's often useful to be able to set up variables, read preferences, and make connections with other GameObjects before any game action takes place.
참고
Don't use a constructor to initialize a
MonoBehaviour
. The Unity Editor handles the construction of objects, which doesn't take place at the start of gameplay as you might expect. If you define a constructor for a
MonoBehaviour
, then this interferes with the normal operation of Unity and can cause major problems with the project.

Attach a script to a GameObject

A script only defines a blueprint for a component, so none of its code will be active until an instance of the script is attached to a GameObject. You can attach a script by dragging the script asset to a GameObject in the Hierarchy window or in the
Inspector

?

window for the GameObject that's currently selected. In the Editor, you can navigate to Component >
Scripts

?

to view all the scripts in the project, including those you've created yourself. The script instance looks much like any other component in the Inspector:

A script component attached to a GameObject in the Inspector.

Once attached, the script runs when you enter Play mode. You can check this by adding the following code in the
Start
function:
// Use this for initializationvoid Start(){ Debug.Log("Hello world!");}
The
Debug.Log
command prints a message to Unity's Console window. When you press Play, the
Hello world!
message appears at the bottom of the main Editor window and in the Console window (menu: Window > General > Console).

Coroutines

The
MonoBehaviour
class allows you to start, stop, and manage coroutines.
For more information on coroutines, refer to Coroutines and the StartCoroutine method script reference.

Events

The
MonoBehaviour
class provides access to a large collection of event functions, which allow you to perform work at different phases of the game update loop and in response to game events. For a list of all event functions, refer to the MonoBehaviour script reference page.

Additional resources