【Unity】OdinのColorフィールド拡張を無効化する

自分の環境だけかもしれませんが、Unity に Odin という Editor 機能を拡張するアセットを導入すると自作のスクリプトの Color フィールドが Odin の拡張表示に置き換わります。

これによってフィールドの値をコピーするときに形式をいくつか選べるようになるのですが、この機能が有効化されると自分のスクリプト内で宣言した Color フィールドから値をコピーして Material や SpriteRenderer などの Unity 組み込みの Color プロパティから値を Copy/Paste が出来なくなります。

あと、メニューをコピーするたびに以下のメッセージが Console に表示されたりします。

Cannot replace menu items in GenericMenu, as private Unity members were missing.

Odin はかなり便利ですが、この挙動だけが地味に不便なので Editor 拡張で動作を無効化したいと思います。

確認環境

  • Unity 2022.3.5f1
  • Odin Inspector 3.1.14.2 (2023/7/25版)

使い方

使い方は以下の通り。無効化したい Color フィールドに属性を付与します。

// MyScript,cs

public class MyScript : MonoBehaviour
{
    // 元に戻したいフィールドに属性を追加
    [SerializeField, OdinIgnore] Color _color;
}

実装コード

OdinIgnoreAttributeクラス

使用しないことを明示するための属性です。

// OdinIgnoreAttribute.cs

using UnityEngine;

public class OdinIgnoreAttribute : PropertyAttribute { }

OdinIgnoreAttributeクラス

Unity 標準の表示になるように Editor 拡張を実装します。とはいえ実装は ColorField を表示するだけです。

// OdinColorIgnoreEditor.cs

#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(OdinIgnoreAttribute))]
public class OdinColorIgnoreEditor : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
    {
        property.colorValue = 
            EditorGUI.ColorField(position, property.displayName, property.colorValue);
    }
}
#endif

以上です。