Search Unity

Question ScriptableSingleton<T> derived class showing gui as disabled in settings provider

Discussion in 'Immediate Mode GUI (IMGUI)' started by EnamulIslamJisan, Dec 29, 2023.

  1. EnamulIslamJisan

    EnamulIslamJisan

    Joined:
    Oct 11, 2018
    Posts:
    93
    ScriptableSingleton<T> derived class showing GUI as disabled when showing its properties in the settings provider. Works ok with normal scriptable objects. What is wrong with my implementation?
    Unity Version: 2021.3.2f1

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEditor;
    5. using UnityEditor.Graphs;
    6. using UnityEditor.UIElements;
    7. using UnityEngine;
    8. using UnityEngine.UIElements;
    9.  
    10. namespace RSS.Editor.Tools.Clipper
    11. {
    12.     [FilePath("ProjectSettings/Clipper.asset", FilePathAttribute.Location.ProjectFolder)]
    13.     internal class ClipperSettings : ScriptableSingleton<ClipperSettings>
    14.     {
    15.         [SerializeField]
    16.         private ClipDuplication clipDuplication;
    17.         public ClipDuplication ClipDuplication
    18.         {
    19.             get { return clipDuplication; }
    20.             set {
    21.                 clipDuplication = value;
    22.                 Save(true);
    23.             }
    24.         }
    25.         [SerializeField]
    26.         private bool autoDeletion;
    27.         public bool AutoDeletion
    28.         {
    29.             get { return autoDeletion; }
    30.             set
    31.             {
    32.                 autoDeletion = value;
    33.                 Save(true);
    34.             }
    35.         }
    36.         [SerializeField]
    37.         private bool includeFavourites;
    38.         public bool IncludeFavourites
    39.         {
    40.             get { return includeFavourites; }
    41.             set
    42.             {
    43.                 includeFavourites = value;
    44.                 Save(true);
    45.             }
    46.         }
    47.         [SerializeField]
    48.         private int daysBeforeDelete;
    49.         public int DaysBeforeDelete
    50.         {
    51.             get { return daysBeforeDelete; }
    52.             set
    53.             {
    54.                 DaysBeforeDelete = value;
    55.                 Save(true);
    56.             }
    57.         }
    58.         [SerializeField]
    59.         private List<Clip> clips;
    60.         public List<Clip> Clips
    61.         {
    62.             get { return clips; }
    63.             set
    64.             {
    65.                 clips = value;
    66.                 Save(true);
    67.             }
    68.         }
    69.         public const string VERSION = "1.0.0";
    70.  
    71.  
    72.         [InitializeOnLoadMethod]
    73.         private static void OnCompile()
    74.         {
    75.             if(instance.AutoDeletion)
    76.             {
    77.                 var clipsToDelete = instance.Clips.Where(c => DateTime.Now.Subtract(DateTime.FromBinary(c.createdAt)).TotalDays >= instance.DaysBeforeDelete);
    78.                 foreach (var clip in clipsToDelete)
    79.                 {
    80.                     if (!instance.IncludeFavourites && clip.IsFavourite) continue;
    81.                     instance.Clips.Remove(clip);
    82.                 }
    83.             };
    84.  
    85.             Save();
    86.         }
    87.  
    88.         public static void Save()
    89.         {
    90.             instance.Save(true);
    91.         }
    92.  
    93.         internal static SerializedObject GetSerializedSettings()
    94.         {
    95.             return new SerializedObject(instance);
    96.         }
    97.     }
    98.     internal class ClipperSettingsProvider : SettingsProvider
    99.     {
    100.         private const string SettingsPath = "Project/Rocketshift Studio/Clipper";
    101.         SerializedObject serializedObject;
    102.         SerializedProperty clipDuplication;
    103.         SerializedProperty autoDeletion;
    104.         SerializedProperty includeFavourites;
    105.         SerializedProperty daysBeforeDelete;
    106.         SerializedProperty clips;
    107.  
    108.         public ClipperSettingsProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null) : base(path, scopes, keywords)
    109.         {
    110.         }
    111.  
    112.         public static bool IsSettingsAvailable()
    113.         {
    114.             return ClipperSettings.instance;
    115.         }
    116.  
    117.         public override void OnActivate(string searchContext, VisualElement rootElement)
    118.         {
    119.             serializedObject = ClipperSettings.GetSerializedSettings();
    120.             clipDuplication = serializedObject.FindProperty("clipDuplication");
    121.             autoDeletion = serializedObject.FindProperty("autoDeletion");
    122.             includeFavourites = serializedObject.FindProperty("includeFavourites");
    123.             daysBeforeDelete = serializedObject.FindProperty("daysBeforeDelete");
    124.             clips = serializedObject.FindProperty("clips");
    125.         }
    126.  
    127.         public override void OnGUI(string searchContext)
    128.         {
    129.             base.OnGUI(searchContext);
    130.  
    131.             EditorGUI.BeginChangeCheck();
    132.             serializedObject.Update();
    133.             EditorGUILayout.PropertyField(clipDuplication, new GUIContent("Clip Duplication"));
    134.             EditorGUILayout.PropertyField(autoDeletion, new GUIContent("Auto Deletion"));
    135.             if (autoDeletion.boolValue)
    136.             {
    137.                 EditorGUI.indentLevel++;
    138.                 EditorGUILayout.PropertyField(includeFavourites, new GUIContent("Include Favourites"));
    139.                 EditorGUILayout.PropertyField(daysBeforeDelete, new GUIContent("Days Before Delete"));
    140.                 EditorGUI.indentLevel--;
    141.             }
    142.             Debug.Log(GUI.enabled);
    143.             GUI.enabled = false;
    144.             EditorGUILayout.PropertyField(clips, new GUIContent("Clips"));
    145.             GUI.enabled = true;
    146.             serializedObject.ApplyModifiedProperties();
    147.             if (EditorGUI.EndChangeCheck())
    148.             {
    149.                 ClipperSettings.Save();
    150.             }
    151.         }
    152.  
    153.         public override void OnFooterBarGUI()
    154.         {
    155.             base.OnFooterBarGUI();
    156.             EditorGUILayout.LabelField($"Version: {ClipperSettings.VERSION}");
    157.         }
    158.         [MenuItem("Tools/Clipper/Settings")]
    159.         private static void SelectSettings()
    160.         {
    161.             SettingsService.OpenProjectSettings(SettingsPath);
    162.         }
    163.  
    164.         [SettingsProvider]
    165.         private static SettingsProvider CreateSettingsProvider()
    166.         {
    167.             // Register the SettingsProvider
    168.             if (IsSettingsAvailable())
    169.             {
    170.                 var provider = new ClipperSettingsProvider(SettingsPath, SettingsScope.Project);
    171.                 // Automatically extract all keywords from the Styles.
    172.                 var keywords = GetSearchKeywordsFromGUIContentProperties<Styles>().ToList();
    173.                 keywords.AddRange(new string[] { "Copy, Clip, Paste, Cut" });
    174.                 provider.keywords = keywords;
    175.                 return provider;
    176.             }
    177.  
    178.             return null;
    179.         }
    180.     }
    181. }
    182.