- Home /
 
How to store a boolean for each variable in a class
Here's a link to an example of what I'm asking about http://www.anbsoft.com/middleware/ezs/overview/
At around 2:50 it shows a list of variables each with a toggle button. I am wanting to know how you would create the boolean for each of those variables and save them. What I'm trying to do is recreate what EZ Game Saver does since I don't have $99 to just throw on it and I'd want it to be my own anyways.
SaveMe.cs
Make a GameObject and put this script on it
 using UnityEngine; using System.Collections; using System.Collections.Generic;
 
                public class SaveMe : MonoBehaviour { public Component[] components; } 
 
               
               SaveMeEditor.cs
Make a folder in the main Assets folder named Editor and put this script in it
using UnityEngine; using UnityEditor; using System; using System.Reflection;
 
               [CustomEditor(typeof(SaveMe))] public class SaveMeEditor : Editor {
 
                public SaveMe saveMe;
 void Awake() {
     saveMe = target as SaveMe;
 }
 public override void OnInspectorGUI() {
     saveMe.components = saveMe.GetComponents(typeof(Component));
     int i = 0;
     foreach(Component a in saveMe.components) {
         Type type = a.GetType();
         if(saveMe.components.Length > 0) {
             GUI.color = Color.Lerp(new Color(.2f, 1, .2f), Color.white, (float)i / saveMe.components.Length);
         }
         GUILayout.Button(type.Name);
         GUI.color = Color.white;
         if(!(a == saveMe)) {
             foreach(PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
                 if(IsSaveable(p.PropertyType)) {
                     GUILayout.BeginHorizontal();
                     GUILayout.BeginHorizontal();
                     EditorGUILayout.Toggle(false, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(20));
                     GUILayout.Label("." + p.Name, GUILayout.ExpandWidth(false));
                     GUILayout.EndHorizontal();
                     GUILayout.Label(p.PropertyType.Name, GUILayout.ExpandWidth(false));
                     GUILayout.EndHorizontal();
                 }
             }
         }
         i++;
     }
 }
 bool IsSaveable(object variable) {
     object a = variable;
     return
         a == typeof(bool)
         |
         a == typeof(int)
         |
         a == typeof(float)
         |
         a == typeof(double)
         |
         a == typeof(string)
         |
         a == typeof(Vector3)
         |
         a == typeof(Quaternion)
         ;
 }
 
               } 
 
               
               What you will see when you go back to the GameObject with the SaveMe script on it, you should see at least one button named Transform and a few variables listed. On the left side is a toggle button. The toggle button is what I need the booleans for. If the boolean is True then I want to be able to save that variable. If someone can give me some ideas of how I could do that I'd appreciate it.
Answer by GibTreaty · Apr 03, 2011 at 10:19 PM
I got it working.. mostly. It doesn't seem to notice variables that I add into my own scripts but oh well I'll worry about that later.
SaveMe.cs
using UnityEngine; using System.Collections; using System.Collections.Generic;
 
               public class SaveMe : MonoBehaviour { public Component[] components; public List<Foldout> foldout = new List<Foldout>();
 
                public bool cFoldout = false;
 
                public bool Contains(Component item) { foreach(Foldout a in foldout) { if(a.component.Equals(item)) { return true; } }
 
                  return false;
 
                }
 
                [System.Serializable] public class Foldout { public Component component; public bool componentEnabled = false; public bool[] variableSavable = new bool[0];
 
                  public Foldout() { }
   public Foldout(Component component) {
       this.component = component;
   }
 
                } } 
 
               
               SaveMeEditor.cs
using UnityEngine; using UnityEditor; using System; using System.Reflection; using System.Collections.Generic;
 
               [CustomEditor(typeof(SaveMe))] public class SaveMeEditor : Editor { public SaveMe saveMe;
 
                void Awake() {
     saveMe = target as SaveMe;
 }
 public override void OnInspectorGUI() {
     saveMe.components = saveMe.GetComponents(typeof(Component));
     #region Add Components To List
     string added = "Added - ";
     foreach(Component a in saveMe.components) {
         if(a != saveMe) {
             if(!saveMe.Contains(a)){
                 added += "\n" + a.GetType().Name + " ID - " +  a.GetInstanceID();
                 saveMe.foldout.Insert(saveMe.foldout.Count, new SaveMe.Foldout(a));
             }
         }
     }
     if(added != "Added - ") {
         Debug.Log(added);
     }
     #endregion
     #region Remove Null Components From List
     List<int> remove = new List<int>(saveMe.foldout.Count);
     for(int n = 0; n < saveMe.foldout.Count; n++){
         if(saveMe.foldout[n].component == null) {
             remove.Add(n);
         }
     }
     foreach(int r in remove) {
         saveMe.foldout.RemoveAt(r);
     }
     #endregion
     #region Draw Foldout Buttons
     for(int i = 0; i < saveMe.foldout.Count; i++){
         if(saveMe.foldout.Count > 1) {
             GUI.color = Color.Lerp(new Color(.2f, 1, .2f), Color.white, (float)i / (float)(saveMe.foldout.Count - 1));
         }
         SaveMeGUI(saveMe.foldout[i]);
     }
     GUI.color = Color.white;
     #endregion
     #region Component Foldout
     saveMe.cFoldout = EditorGUILayout.Foldout(saveMe.cFoldout, "Foldout");
     if(saveMe.cFoldout) {
         for(int f = 0; f < saveMe.foldout.Count; f++) {
             EditorGUILayout.BeginHorizontal();
             EditorGUILayout.PrefixLabel(saveMe.foldout[f].component.GetType().Name);
             saveMe.foldout[f].componentEnabled = EditorGUILayout.Toggle(saveMe.foldout[f].componentEnabled);
             EditorGUILayout.EndHorizontal();
         }
     }
     #endregion
 }
 void SaveMeGUI(SaveMe.Foldout foldout) {
     Type type = foldout.component.GetType();
     if(GUILayout.Button(type.Name)) {
         foldout.componentEnabled = !foldout.componentEnabled;
     }
     if(foldout.componentEnabled) {
         GUI.color = Color.white;
         //Get all variables
         PropertyInfo[] propertyInfo = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
         int fields = 0;
         foreach(PropertyInfo b in propertyInfo) {
             if(IsSaveable(b.PropertyType)) {
                 fields++;
             }
         }
         if(foldout.variableSavable.Length != fields) {
             Array.Resize<bool>(ref foldout.variableSavable, fields);
         }
         for(int i = 0; i < fields; i++) {
             if(IsSaveable(propertyInfo[i].PropertyType)) {
                 GUILayout.BeginHorizontal();
                 GUILayout.BeginHorizontal();
                 foldout.variableSavable[i] = EditorGUILayout.Toggle(foldout.variableSavable[i], GUILayout.ExpandWidth(false), GUILayout.MaxWidth(20));
                 GUILayout.Label("." + propertyInfo[i].Name, GUILayout.ExpandWidth(false));
                 GUILayout.EndHorizontal();
                 GUILayout.Label(propertyInfo[i].PropertyType.Name, GUILayout.ExpandWidth(false));
                 GUILayout.EndHorizontal();
             }
         }
     }
 }
 bool IsSaveable(object variable) {
     object a = variable;
     return
         a == typeof(bool)
         |
         a == typeof(int)
         |
         a == typeof(float)
         |
         a == typeof(string)
         |
         a == typeof(System.Enum)
         |
         a == typeof(Color)
         |
         a == typeof(Vector2)
         |
         a == typeof(Vector3)
         |
         a == typeof(Quaternion)
         //|
         //a == typeof(GameObject)
         //|
         //a == typeof(Component)
         ;
 }
 
               } 
 
              Your answer
 
             Follow this Question
Related Questions
Player prefs and bool 6 Answers
How to make a save and load option 2 Answers
Problem Saving game progress 1 Answer