Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by GibTreaty · Apr 03, 2011 at 12:15 AM · gamesaveloadboolean

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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&lt;int&gt; remove = new List&lt;int&gt;(saveMe.foldout.Count);
     for(int n = 0; n &lt; 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 &lt; saveMe.foldout.Count; i++){
         if(saveMe.foldout.Count &gt; 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 &lt; 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&lt;bool&gt;(ref foldout.variableSavable, fields);
         }

         for(int i = 0; i &lt; 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)
         ;
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Player prefs and bool 6 Answers

How to make a save and load option 2 Answers

Problem Saving game progress 1 Answer

What are the basics of saving and loading game? 1 Answer

Unity load last level (save and load game) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges