AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  1. Packages
  2. Library
  3. Attributes

ReadOnly Field

Protecting serialized field with visibility

Last updated 2 months ago

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
}

With attribute , it will be more flexible on editing to non-editable fields.

InspectorButton