以下の内容はhttps://bravememo.hatenablog.com/entry/2023/10/30/230716より取得しました。


【Unity】Publicな変数を編集できないようにするCustom Attributeを作成する

はじめに

Publicな変数を編集できないようにするReadOnlyなCustom Attributeを作成します

スクリプト

using UnityEngine;
using UnityEditor;

public class ReadOnlyAttribute : PropertyAttribute { }


[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        GUI.enabled = false;
        EditorGUI.PropertyField(position, property, label, true);
    }
}

使い方

using UnityEngine;

public class Test : MonoBehaviour
{
    [ReadOnly]
    public float test;
}

下記のように編集できないようになります




以上の内容はhttps://bravememo.hatenablog.com/entry/2023/10/30/230716より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

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