Unity はエディター モードでスクリプト ライフサイクルを実装します

Unity の MonoBehaviour は、デフォルトでは再生モードでのみ実行できます。編集モードで実行する場合は、[ExecuteInEditMode] または [ExecuteAlways] の 2 つの機能を使用できます (コードは次のとおりです)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class TestExecuteInEditMode : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log("[ExecuteInEditMode] Awake");
    }

    private void OnEnable()
    {
        Debug.Log("[ExecuteInEditMode] OnEnable");
    }

    private void Start()
    {
        Debug.Log("[ExecuteInEditMode] Start");
    }

    private void Update()
    {
        Debug.Log("[ExecuteInEditMode] Update");
    }

    private void OnDisable()
    {
        Debug.Log("[ExecuteInEditMode] OnDisable");
    }

    private void OnDestroy()
    {
        Debug.Log("[ExecuteInEditMode] OnDestroy");
    }
}


[ExecuteAlways]
public class TestExecuteAlways : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log("[ExecuteAlways] Awake");
    }

    private void OnEnable()
    {
        Debug.Log("[ExecuteAlways] OnEnable");
    }

    private void Start()
    {
        Debug.Log("[ExecuteAlways] Start");
    }

    private void Update()
    {
        Debug.Log("[ExecuteAlways] Update");
    }

    private void OnDisable()
    {
        Debug.Log("[ExecuteAlways] OnDisable");
    }

    private void OnDestroy()
    {
        Debug.Log("[ExecuteAlways] OnDestroy");
    }

}

ExecuteInEditMode

[ExecuteInEditMode] 機能を追加し、Play Mode と Edit Mode でスクリプトを実行できるようにしました ( Prefab Mode では実行できません)。ただし、更新機能は常に編集モードで実行されるわけではなく、マウスを動かし続けたときにのみ実行されます。マウスの動きが止まると、実行も停止します。編集モードでは、次の共通機能がサポートされています。

  • 起きた
  • 有効にする
  • 始める
  • アップデート
  • 無効にする
  • OnDestroy

常時実行

[ExecuteInEditMode] のアップグレード版である [ExecuteInEditMode] 機能を追加し、再生モード、編集モード、およびプレハブ モードでスクリプトを実行できます。あとは基本的に【ExecuteInEditMode】と同じです。

再生モードと同じように、編集モードでも更新機能を常に実行する方法は?

  • 方法1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class TestExecuteInEditMode : MonoBehaviour
{
    private void Update()
    {
        Debug.Log("[ExecuteInEditMode] Update");
    }

    private void OnDrawGizmos()
    {

#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
            UnityEditor.SceneView.RepaintAll();
        }
#endif
    }

}


[ExecuteAlways]
public class TestExecuteAlways : MonoBehaviour
{
    private void Update()
    {
        Debug.Log("[ExecuteAlways] Update");
    }

    private void OnDrawGizmos()
    {

#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            UnityEditor.EditorApplication.QueuePlayerLoopUpdate();
            UnityEditor.SceneView.RepaintAll();
        }
#endif
    }

}
  •  方法 2
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
 
[InitializeOnLoad]
public class TestUpdate
{

    static TestUpdate()
    {
        EditorApplication.update += Update;
    }

    static void Update()
    {
        Debug.Log("[InitializeOnLoad] Update");
    }
}

おすすめ

転載: blog.csdn.net/a451319296/article/details/129642668