Search Unity

Setting and Loading configuration

Discussion in 'Scripting' started by deraggi, Sep 20, 2017.

  1. deraggi

    deraggi

    Joined:
    Apr 29, 2016
    Posts:
    88
    Dear Unity community,

    I have a requirement which I was not able to resolve by googeling.

    I have a couple scripts on gameobjects that define settings for my game, e.g. shoult it connect to Steam or not, am I in developer cheat mode or not, do I use VR or not, etc. I'm doing the setup currently in Editor.

    I would like to create different configurations for my game and set them via script, so I can link the setting to a menu entry or button and just set it before building the game.

    However, I fail to understand how I can modify editor values of gameobjects.

    Does anyone have an idea how this can be done?

    Thank you!
     
  2. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    You could have a Settings ScriptableObject, and a SettingsManager that contains the current settings. Every different configuration would be a new asset, so a SteamSettings etc.
     
  4. deraggi

    deraggi

    Joined:
    Apr 29, 2016
    Posts:
    88
    Hmm, I had thought about that as well. I was hoping I could hard-Code settings somehow - but this might do as well. Thank you!
     
  5. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Why would you want that?

    You could always just have some Struct in your code and assign these to a Current property.
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    You could add a simple button to the inspector which can apply those ScriptableObject values in the editor.

    Make an Editor script for your main script, then use DrawDefaultInspector to draw it as normal, then add a button underneath to apply all the settings, then you can build your game.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(MyScriptName))]
    6. public class MyScriptNameEditor : Editor {
    7.  
    8.     public override void OnInspectorGUI()
    9.     {
    10.         DrawDefaultInspector();
    11.  
    12.         if (GUILayout.Button("Apply", GUILayout.Height(20f)))
    13.         {
    14.             ...
    15.         }
    16.     }
    17.  
    18. }
    Or just use a menu drop down to apply different settings.
     
    deraggi likes this.