ReadOnly Field

Protecting serialized field with visibility

ReadOnlyField

Make the field non-editable in Inspector window.

using System;
using AceLand.Library.Attribute;
using AceLand.Library.Extensions;
using UnityEngine;

[CreateAssetMenu(fileName = "MyItem", menuName = "Profiles/MyItem")]
public class ItemProfile : ScriptableObject
{
    // id will be a non-editable field in Inspector window
    [ReadOnlyField] public string id;
    public Texture2D preview;
    public int effectValue;

    // value on create
    private void OnEnable()
    {
        id ??= Guid.NewGuid().ToString();
    }

    // initial readonly field
    // this can apply change on existing ScriptableObject
    private void OnValidate()
    {
        if (id.IsNullOrEmptyOrWhiteSpace())
            id = Guid.NewGuid().ToString();
    }
    
#if UNITY_EDITOR

    // add a button to renew the non-editable field
    [InspectorButton]
    private void RenewId() =>
        id = Guid.NewGuid().ToString();

#endif
}

Last updated