Search Unity

[Help] Changing custom project settings doesn't update references

Discussion in 'Immediate Mode GUI (IMGUI)' started by MCrafterzz, Apr 17, 2019.

  1. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    I have created a custom project settings that save the data in a scriptable object. The asset always updates as well as the settings gui, the problem is the classes that reference the scriptable object. Code:
    Code (CSharp):
    1. public class RoadCreatorSettings : ScriptableObject
    2. {
    3.  
    4.     public float pointSize = 0.5f;
    5.  
    6.     private static RoadCreatorSettings GetOrCreateSettings()
    7.     {
    8.         RoadCreatorSettings settings = AssetDatabase.LoadAssetAtPath<RoadCreatorSettings>("Assets/Editor/RoadCreatorSettings.asset");
    9.         if (settings == null)
    10.         {
    11.             if (Directory.Exists("Assets/Editor") == false)
    12.             {
    13.                 Directory.CreateDirectory("Assets/Editor");
    14.             }
    15.  
    16.             settings = ScriptableObject.CreateInstance<RoadCreatorSettings>();
    17.             AssetDatabase.CreateAsset(settings, "Assets/Editor/RoadCreatorSettings.asset");
    18.             AssetDatabase.SaveAssets();
    19.         }
    20.         return settings;
    21.     }
    22.  
    23.     public static SerializedObject GetSerializedSettings()
    24.     {
    25.         return new SerializedObject(GetOrCreateSettings());
    26.     }
    27.  
    28. }
    And
    Code (CSharp):
    1. public class RoadCreatorProjectSettings
    2. {
    3.     [SettingsProvider]
    4.     public static SettingsProvider CreateSettingsProvider()
    5.     {
    6.         SettingsProvider settingsProvider = new SettingsProvider("Project/RoadCreator", SettingsScope.Project)
    7.         {
    8.             label = "Road Creator",
    9.  
    10.             guiHandler = (searchContext) =>
    11.             {
    12.                 SerializedObject settings = RoadCreatorSettings.GetSerializedSettings();
    13.  
    14.                 EditorGUI.BeginChangeCheck();
    15.                 settings.FindProperty("pointSize").floatValue = Mathf.Max(0.1f, EditorGUILayout.FloatField("Point Size", settings.FindProperty("pointSize").floatValue));
    16.                 if (EditorGUI.EndChangeCheck() == true)
    17.                 {
    18.                     settings.ApplyModifiedPropertiesWithoutUndo();
    19.                 }
    20.             }
    21.         };
    22.  
    23.         return settingsProvider;
    24.     }
    25.  
    26. }
    27.  
    I have a reference which I get in onenable my custom editor classes:
    Code (CSharp):
    1. if (roadCreator.settings == null)
    2.         {
    3.             roadCreator.settings = RoadCreatorSettings.GetSerializedSettings();
    4.         }
    I then use RoadCreator.settings but the problem is that, that reference doesn't contain the updated values so when changing settings in the gui the objects still use the old values. Any idea how to work around this? I have searched and haven't found a lot about custom project settings in general. Also I'm using 2019.1.

    EDIT: it updates it's value on recompile for some reason
     
    Last edited: Apr 22, 2019
  2. nehvaleem

    nehvaleem

    Joined:
    Dec 13, 2012
    Posts:
    438
    @MCrafterzz did you solve this problem? I am facing a very similar issue.
     
  3. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Wow this was a long time ago. What I ended up with was that when the settings updated I went through all my objects that used it and gave them the new settings object
     
    Last edited: Dec 29, 2021
    nehvaleem likes this.
  4. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Since i just came across this as well. This seemed to fix it for me.
    Code (CSharp):
    1.         public override void OnGUI(string searchContext)
    2.         {
    3.             m_CustomSettings.Update();  //add this
    4.  
    5.             // Use IMGUI to display UI:
    6.             EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("_IncludePaths"), Styles.IncludePaths);
    7.             EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("_ExcludePaths"), Styles.ExcludePaths);
    8.             EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("_LTextSourceAnalyzerPath"), Styles.SourceAnalysizePath);
    9.             EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("_OutputPath"), Styles.OutputPath);
    10.  
    11.             m_CustomSettings.ApplyModifiedProperties(); //add this
    12.         }
    13.  
    This follows the same pattern from other editor stuff. In my case the first two are lists. Which would update ok.
    When i added just some string fields it would not update without adding the extra Update and ApplyModifiedProperties calls.