Version: 2022.3
言語: 日本語

MonoBehaviour

class in UnityEngine

/

継承:Behaviour

マニュアルに切り替える

説明

MonoBehaviour is a base class that many Unity scripts derive from.

MonoBehaviour offers life cycle functions that make it easier to develop with Unity.

MonoBehaviours always exist as a Component of a GameObject, and can be instantiated with GameObject.AddComponent. Objects that need to exist independently of a GameObject should derive from ScriptableObject instead.

A MonoBehaviour can be deleted with Object.Destroy or Object.DestroyImmediate. When the parent GameObject is destroyed all components are automatically deleted, including MonoBehaviours.

After the underlying component is destroyed, the C# object for the MonoBehaviour remains in memory until garbage is collected. A MonoBehaviour in this state acts as if it is null. For example, it returns true for a "obj == null" check. However, this class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??).

When a MonoBehaviour is serialized, the value of C# fields are included according to Unity's Serialization rules. See Script Serialization for details. The serialized data also includes internal properties, such as the reference to the MonoScript that tracks the implementation class for the object.

For code samples, see the individual MonoBehaviour methods.

Note: There is a checkbox for enabling or disabling MonoBehaviour in the Unity Editor. It disables functions when unticked. If none of these functions are present in the script, the Unity Editor does not display the checkbox. The functions are:

Start()
Update()
FixedUpdate()
LateUpdate()
OnGUI()
OnDisable()
OnEnable()

See Also: The Deactivating GameObjects page in the manual.

変数

destroyCancellationTokenCancellation token raised when the MonoBehaviour is destroyed (Read Only).
runInEditModeAllow a specific instance of a MonoBehaviour to run in edit mode (only available in the editor).
useGUILayoutこれを無効にすると、GUI のレイアウトフェーズをスキップすることができます

Public 関数

CancelInvokeすべての Invoke をキャンセルします
Invoke設定した時間(単位は秒)にメソッドを呼び出します
InvokeRepeating設定した時間(単位は秒)にメソッドを呼び出し、repeatRate 秒ごとにリピートします
IsInvokingメソッドの呼出が保留中かどうか
StartCoroutineStarts a Coroutine.
StopAllCoroutinesBehaviour 上で実行されているコルーチンをすべて停止します
StopCoroutineこの Behaviour 上で実行されている methodName という名のコルーチン、または routine として保持されているコルーチンをすべて停止します

Static 関数

printUnity コンソールにログを出力します (Debug.Log と同じです)

メッセージ

AwakeAwake is called when an enabled script instance is being loaded.
FixedUpdateFrame-rate independent MonoBehaviour.FixedUpdate message for physics calculations.
LateUpdate Behaviour が有効の場合、LateUpdate は毎フレーム呼びだされます
OnAnimatorIKアニメーション IK (インバースキネマティクス)をセットアップするときのコールバック
OnAnimatorMoveルートモーションを修正するアニメーション動作を処理するコールバック
OnApplicationFocusプレイヤーがフォーカスを取得、または、失ったときに、すべてのゲームオブジェクトに送信されます。
OnApplicationPauseプレイヤーが一時停止したときにすべてのゲームオブジェクトに送信されます
OnApplicationQuitSent to all GameObjects before the application quits.
OnAudioFilterReadOnAudioFilterRead が実装されている場合、Unity は DSP チェーンにカスタムフィルターを挿入します。
OnBecameInvisibleOnBecameInvisible はレンダラーがカメラから見えなくなったときに呼び出されます
OnBecameVisibleOnBecameVisible はレンダラーが任意のカメラから見えるようになると呼び出されます
OnCollisionEnterこの collider/rigidbody は他の collider/rigidbody に触れたときに OnCollisionEnter は呼び出されます。
OnCollisionEnter2Dオブジェクトのコライダーが別のコライダーに衝突したときに呼び出されます(2D 物理挙動のみ)
OnCollisionExitこの collider/rigidbody が他の collider/rigidbody と触れ合うのをやめたときに OnCollisionExit は呼び出されます。
OnCollisionExit2Dオブジェクトのコライダーと別のオブジェクトコライダーが衝突から離れた瞬間に呼び出されます(2D 物理挙動のみ)
OnCollisionStayOnCollisionStay is called once per frame for every Collider or Rigidbody that touches another Collider or Rigidbody.
OnCollisionStay2Dオブジェクトのコライダーと別のオブジェクトのコライダーが衝突している間、毎フレーム呼び出され続けます(2D 物理挙動のみ)
OnConnectedToServerサーバーとの接続に成功したときにクライアント上で呼び出されます
OnControllerColliderHitOnControllerColliderHit はキャラクターコントローラーが移動中にコライダーに衝突した際に、呼び出されます。
OnDestroyDestroying the attached Behaviour will result in the game or Scene receiving OnDestroy.
OnDisableThis function is called when the behaviour becomes disabled.
OnDisconnectedFromServerサーバーとの接続が失われたか切断されたときにクライアント上で呼び出されます
OnDrawGizmos選択可能にしたり、常に描画したいギズモを描画するには OnDrawGizmos を使用します
OnDrawGizmosSelectedオブジェクトが選択されている場合は、ギズモを描画するために OnDrawGizmosSelected を実装します。
OnEnableこの関数はオブジェクトが有効/アクティブになったときに呼び出されます
OnFailedToConnect接続試行がなんらかの理由で失敗したときにクライアント上で呼び出されます
OnFailedToConnectToMasterServerMasterServer への接続に問題がある場合に、クライアントまたはサーバーで呼び出されます
OnGUIOnGUI はレンダリングと GUI イベントのハンドリングのために呼び出されます
OnJointBreakゲームオブジェクトに対するジョイントが外れたとき呼び出されます
OnJointBreak2Dゲームオブジェクトにアタッチした Joint2D が壊れたときに呼び出されます。
OnMasterServerEventMasterServer からイベントを報告してくるときにクライアントやサーバー上で呼び出されます。
OnMouseDown OnMouseDown is called when the user has pressed the mouse button while over the Collider.
OnMouseDragOnMouseDrag is called when the user has clicked on a Collider and is still holding down the mouse.
OnMouseEnterCalled when the mouse enters the Collider.
OnMouseExitCalled when the mouse is not any longer over the Collider.
OnMouseOverCalled every frame while the mouse is over the Collider.
OnMouseUpOnMouseUp はユーザーがマウスボタンを離したときに呼び出されます
OnMouseUpAsButtonOnMouseUpAsButton is only called when the mouse is released over the same Collider as it was pressed.
OnNetworkInstantiateNetwork.Instantiate でインスタンス化されたオブジェクトに対して呼び出されます
OnParticleCollisionパーティクルがコライダーにヒットしたときに OnParticleCollision が呼び出されます
OnParticleSystemStoppedOnParticleSystemStopped is called when all particles in the system have died, and no new particles will be born. New particles cease to be created either after Stop is called, or when the duration property of a non-looping system has been exceeded.
OnParticleTriggerOnParticleTrigger is called when any particles in a Particle System meet the conditions in the trigger module.
OnParticleUpdateJobScheduledOnParticleUpdateJobScheduled is called when a Particle System's built-in update job has been scheduled.
OnPlayerConnected新しいプレイヤーが接続に成功したときにサーバー上で呼び出されます
OnPlayerDisconnectedプレイヤーがサーバーから接続が切断されるたびにサーバー上で呼び出されます
OnPostRender Event function that Unity calls after a Camera renders the scene.
OnPreCull Event function that Unity calls before a Camera culls the scene.
OnPreRender Event function that Unity calls before a Camera renders the scene.
OnRenderImage Event function that Unity calls after a Camera has finished rendering, that allows you to modify the Camera's final image.
OnRenderObjectOnRenderObject is called after camera has rendered the Scene.
OnSerializeNetworkViewネットワークビューによって監視されるスクリプトの変数の同期をカスタマイズするために使用します
OnServerInitializedNetwork.InitializeServer が実行され完了したときにサーバー上で呼び出されます
OnTransformChildrenChangedこの関数は GameObject の Transform のすべての子の中で変更があったときに呼び出されます
OnTransformParentChangedThis function is called when a direct or indirect parent of the transform of the GameObject has changed.
OnTriggerEnterWhen a GameObject collides with another GameObject, Unity calls OnTriggerEnter.
OnTriggerEnter2Dオブジェクトにアタッチしたトリガーの中に別のオブジェクトが入ったときに呼び出されます。(2D 物理挙動のみ)
OnTriggerExit Collider が other のトリガーに触れるのをやめたときに OnTriggerExit は呼び出されます。
OnTriggerExit2Dトリガー状態のオブジェクトのコライダーと別のオブジェクトのコライダーが衝突から離れた瞬間に、呼び出されます。(2D 物理挙動のみ)
OnTriggerStayOnTriggerStay is called once per physics update for every Collider other that is touching the trigger.
OnTriggerStay2Dトリガー状態のオブジェクトのコライダーと別のオブジェクトのコライダー衝突している間、毎フレーム呼び出され続けます。(2D 物理挙動のみ)
OnValidateEditor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
OnWillRenderObjectOnWillRenderObject is called for each camera if the object is visible and not a UI element.
Resetデフォルト値にリセットします
StartStart is called on the frame when a script is enabled just before any of the Update methods are called the first time.
UpdateUpdate は MonoBehaviour が有効の場合に、毎フレーム呼び出されます

継承メンバー

変数

enabled有効であれば更新され、無効であれば更新されません。
isActiveAndEnabledReports whether a GameObject and its associated Behaviour is active and enabled.
gameObjectこのコンポーネントはゲームオブジェクトにアタッチされます。コンポーネントはいつもゲームオブジェクトにアタッチされています。
tagゲームオブジェクトのタグ
transformThe Transform attached to this GameObject.
hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameオブジェクト名

Public 関数

BroadcastMessageゲームオブジェクトまたは子オブジェクトにあるすべての MonoBehaviour を継承したクラスにある methodName 名のメソッドを呼び出します。
CompareTagChecks the GameObject's tag against the defined tag.
GetComponentGets a reference to a component of type T on the same GameObject as the component specified.
GetComponentInChildrenGets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject.
GetComponentInParentGets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject.
GetComponentsGets references to all components of type T on the same GameObject as the component specified.
GetComponentsInChildrenGets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject.
GetComponentsInParentGets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject.
SendMessageゲームオブジェクトにアタッチされているすべての MonoBehaviour にある methodName と名付けたメソッドを呼び出します
SendMessageUpwardsゲームオブジェクトと親(の親、さらに親 ... )にアタッチされているすべての MonoBehaviour にある methodName と名付けたメソッドを呼び出します
TryGetComponentGets the component of the specified type, if it exists.
GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static 関数

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfTypeタイプ type から最初に見つけたアクティブのオブジェクトを返します
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
Instantiateoriginal のオブジェクトをクローンします

Operator

boolオブジェクトが存在するかどうか
operator !=二つのオブジェクトが異なるオブジェクトを参照しているか比較します
operator ==2つのオブジェクト参照が同じオブジェクトを参照しているか比較します。