Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question String Array Property Field Not Applying Modified Values

Discussion in 'Immediate Mode GUI (IMGUI)' started by Maraudical, Sep 18, 2023.

  1. Maraudical

    Maraudical

    Joined:
    Feb 8, 2021
    Posts:
    3
    I am trying to setup a custom Project Settings tab and have an editable array of strings. Every thing displays correctly and I can add, remove, and reorder the array perfectly fine. The problem is that when I try to edit the value of the string in the array it doesn't actually update the value.

    Here is the code (for the most part it is really just copied from the SettingsProvider docs):

    Code (CSharp):
    1. // Create a new type of Settings Asset.
    2. public class StatusEffectSettings : ScriptableObject
    3. {
    4.     public const string k_MyCustomSettingsPath = "Assets/Plugins/StatusEffectsFramework/Runtime/Settings/Resources/StatusEffectsFramework/StatusEffectSettings.asset";
    5.    
    6.     public bool allowEffectStacking;
    7. #if LOCALIZATION_SUPPORT
    8.     public bool disableUnityLocalizationSupport;
    9. #endif
    10.     [Space]
    11.     public string[] groups;
    12.     [Space]
    13.     public string[] statuses;
    14.  
    15.     internal static StatusEffectSettings GetOrCreateSettings()
    16.     {
    17.         var settings = Resources.Load<StatusEffectSettings>("StatusEffectsFramework/StatusEffectSettings");
    18.         if (settings == null)
    19.         {
    20.             settings = CreateInstance<StatusEffectSettings>();
    21.             settings.allowEffectStacking = false;
    22.             settings.disableUnityLocalizationSupport = false;
    23.             settings.groups = new string[] { "Static", "Negative", "Positive" };
    24.             settings.statuses = new string[] { "Max Health", "Speed", "Damage" };
    25.             AssetDatabase.CreateAsset(settings, k_MyCustomSettingsPath);
    26.             AssetDatabase.SaveAssets();
    27.         }
    28.         return settings;
    29.     }
    30.  
    31.     internal static SerializedObject GetSerializedSettings()
    32.     {
    33.         return new SerializedObject(GetOrCreateSettings());
    34.     }
    35. }
    36.  
    37. // Register a SettingsProvider using IMGUI for the drawing framework:
    38. static class StatusEffectSettingsIMGUIRegister
    39. {
    40.     [SettingsProvider]
    41.     public static SettingsProvider CreateStatusEffectSettingsProvider()
    42.     {
    43.         // First parameter is the path in the Settings window.
    44.         // Second parameter is the scope of this setting: it only appears in the Project Settings window.
    45.         var provider = new SettingsProvider("Project/StatusEffectSettings", SettingsScope.Project)
    46.         {
    47.             // By default the last token of the path is used as display name if no label is provided.
    48.             label = "Status Effect Settings",
    49.             // Create the SettingsProvider and initialize its drawing (IMGUI) function in place:
    50.             guiHandler = (searchContext) =>
    51.             {
    52.                 var settings = StatusEffectSettings.GetSerializedSettings();
    53.                 EditorGUIUtility.labelWidth = 215;
    54.                 SerializedProperty property = settings.FindProperty("allowEffectStacking");
    55.                 EditorGUILayout.PropertyField(property, new GUIContent(property.displayName));
    56. #if LOCALIZATION_SUPPORT
    57.                 property = settings.FindProperty("disableUnityLocalizationSupport");
    58.                 EditorGUILayout.PropertyField(property, new GUIContent(property.displayName));
    59. #endif
    60.                 EditorGUIUtility.labelWidth = 0;
    61.                 property = settings.FindProperty("groups");
    62.                 EditorGUILayout.PropertyField(property, new GUIContent(property.displayName));
    63.                 property = settings.FindProperty("statuses");
    64.                 EditorGUILayout.PropertyField(property, new GUIContent(property.displayName));
    65.                 settings.ApplyModifiedPropertiesWithoutUndo();
    66.             },
    67.  
    68.             // Populate the search keywords to enable smart search filtering and label highlighting:
    69.             keywords = new HashSet<string>(new[] { "Status", "Effect", "Group", "Stacking", "Localization" })
    70.         };
    71.  
    72.         return provider;
    73.     }
    74. }
    I also included a short gif of the array editing not updating.
     

    Attached Files: