(:3[kanのメモ帳]

個人ゲーム開発者kan.kikuchiのメモ的技術ブログ。月木更新でUnity関連がメイン。

(:3[kanのメモ帳]


本ブログの運営者kan.kikuchiが個人で開発したゲームです!

    

ExecuteInEditModeを使うとPrefabモードで再生した時に「Risk of unwanted modifications」と警告が出るので後継のExecuteAlwaysを使おうという話【Unity】【エディタ拡張】【属性】


このエントリーをはてなブックマークに追加



この記事でのバージョン
Unity 2021.3.11f1


はじめに

UnityではExecuteInEditModeという属性をMonoBehaviourを継承したクラスに付ける事で、

プレイモード(Unityエディタ再生中)でなくともStartやUpdateを実行する事が出来ます。


using UnityEngine;

[ExecuteInEditMode]//プレイモードじゃなくてもStartやUpdateが実行されるように
public class NewBehaviourScript : MonoBehaviour {
    
    void Start(){
        
    }

    void Update(){
        
    }
    
}


しかし、この属性を付けたスクリプトをPrefabに付けると、

Prefabを開いてる状態で再生した時に以下のような警告が出るようになります。


Risk of unwanted modifications


The following scripts on the Prefab open in Prefab Mode use the [ExecuteInEditMode] attribute which means they may accidentally affect or be affected by Play Mode:

NewBehaviourScript, NewBehaviourScript

See the documentation for [ExecuteInEditMode] and [ExecuteAlways] for info on how to make scripts compatible with Prefab Mode during Play Mode.

不要な変更のリスク


Prefab Modeで開くPrefab上の以下のスクリプトは、[ExecuteInEditMode]属性を使用しているので、誤ってPlay Modeに影響を与えたり、影響を受けたりする可能性があります。

NewBehaviourScript, NewBehaviourScript。

プレイモード中にプレハブモードと互換性のあるスクリプトを作成する方法については、[ExecuteInEditMode] と [ExecuteAlways] のドキュメントを参照してください。
DeepL翻訳


これはExecuteInEditModeがPrefab Modeが生まれる以前の属性のためで、

Prefab Modeを考慮していないのが原因です。

なので、今後はExecuteInEditModeの後継(?)であるExecuteAlwaysを使うと良さそうです。



using UnityEngine;

[ExecuteAlways]//プレイモードじゃなくてもStartやUpdateが実行されるように
public class NewBehaviourScript : MonoBehaviour {
    
    void Start(){
        
    }

    void Update(){
        
    }
    
}


なおちょっと試した感じだと、ExecuteInEditModeと全く同じ感覚で使えました。