Search Unity

Using an External Dependency to handle Config Values alongside [ExecuteInEditMode]

Discussion in 'Scripting' started by danrayson, Nov 4, 2018.

  1. danrayson

    danrayson

    Joined:
    Mar 3, 2013
    Posts:
    10
    I have a setup where I'm using a DLL to handle my configuration values, such that in code I write something like this;

    Code (CSharp):
    1. using Rayson.Configuration.Json;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class ConfigSystem : MonoBehaviour {
    6.  
    7.     public TextAsset JsonConfigFile;
    8.  
    9.     void Start ()
    10.     {
    11.         JsonConfig.AddJsonProviderWith(JsonConfigFile.text);
    12.     }
    13. }
    14.  
    ... and then elsewhere in my scene I'll have a GameObject with this kind of thing;

    Code (CSharp):
    1. using Rayson.Configuration;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class SomeOtherSystem : MonoBehaviour {
    6.  
    7.     void Update()
    8.     {
    9.         int myValue = Config.Get<int>("IKnowThisWorksOutsideOfEditMode");
    10.     }
    11. }
    12.  
    The problem I have is not knowing where this external "Rayson" dependency is working or not - I know that it does - The problem is that it doesn't work properly in Unity's EditMode.

    The exact error I get is related to the fact that the first
    AddJsonProviderWith
    call doesn't seem to affect the second call to
    Get<int>
    . Normally it does, but I think the editor isn't maintaining the memory during edit mode??

    Can someone try to explain why this doesn't work? I know I could use a ScriptableObject and load that instead, but I'd really like to get my configuration to be in a json file... Having an editable text file to provide config values seems nicer to me than an object you can only access via the Unity editor. Am I missing the point?
     
    Last edited: Nov 4, 2018
  2. danrayson

    danrayson

    Joined:
    Mar 3, 2013
    Posts:
    10
    For those interested, I've resorted to using a ScriptableObject and creating an Asset from it.

    Like this;

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "Config", menuName = "Config/Create New")]
    2. public class ConfigData : ScriptableObject
    3. {
    4.     public string MyConfigValue;
    5. }
    6.  
    Then I put that Asset onto a MonoBehaviour on a GameObject in the scene. I called the MonoBehaviour "ConfigProvider".

    Code (CSharp):
    1. public class ConfigProvider : MonoBehaviour
    2. {
    3.     public ConfigData CurrentConfig;
    4. }
    I then load that asset into other GameObjects using this pattern;

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         CurrentConfig = FindObjectOfType<ConfigProvider>().CurrentConfig;
    4.     }
    Unfortunately I still have to set the properties manually by dragging the asset into the properties editor, for the game objects that have ExecuteInEditMode on them. At runtime this shown pattern works fine, but edit mode still teething issues.

    As a note, I'll be passing instances into set methods for game objects created at runtime, FindObjectOfType is apparently rather slow.
     
    Last edited: Nov 5, 2018