以下の内容はhttps://baba-s.hatenablog.com/entry/2020/07/28/090000より取得しました。


【Unity】条件を満たしていない場合は Inspector から Add Component できないようにするエディタ拡張の例

概要

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
internal static class Example
{
    static Example()
    {
        ObjectFactory.componentWasAdded += OnComponentWasAdded;
    }

    private static void OnComponentWasAdded( Component component )
    {
        // 条件を満たしていない場合
        if ( ... )
        {
            // Add Component しようとしたコンポーネントを削除する
            // 1 フレーム遅らせないと下記のエラーが発生する
            // Assertion failed on expression: 'm_InstanceID != InstanceID_None'
            EditorApplication.delayCall += () => Object.DestroyImmediate( component );
        }
    }
}
  • 例えば自作のコンポーネントを意図しないゲームオブジェクトに
    アタッチできないようにすることができる



以上の内容はhttps://baba-s.hatenablog.com/entry/2020/07/28/090000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14