• 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
0
Question by GutoThomas · Jul 06, 2012 at 05:48 PM · guieditorarraylistsave

Changes not being saved in the Inspector Editor

I'm developing a custom GUI system for Unity and I have a couple of programmed elements for now. One of then is a panel. Basically, t$$anonymous$$s allow me to drag a couple of GUIZone (class that every item of my own GUI derives from) in some slots. The number of slots can be set by a int variable that basically create an array of n elements and start a loop that go from 0 to n creating an ObjectField for each of t$$anonymous$$s elements.

The script I'm using is the following:

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(GUIZonePanel))]
 
 public class GUIZonePanelEditor : Editor {
 
  private GUIZonePanel _target;
  private int _elementsInPanel;
  
  public override void OnInspectorGUI () {
  
  GUILayout.Label("Insert the elements you want to be in t$$anonymous$$s panel");
  _target = target as GUIZonePanel;
  _elementsInPanel = EditorGUILayout.IntField("Number of elements", _elementsInPanel);
  
  if(GUI.changed) {
  
  if(_elementsInPanel > _target.panelElements.Count) {
  
  for(int i = _target.panelElements.Count; i < _elementsInPanel; i++) {
  
  _target.panelElements.Add( null );
  
  }
  
  } else {
  
  for(int i = _target.panelElements.Count; i > _elementsInPanel; i--) {
  
  _target.panelElements.RemoveAt(i - 1);
  
  }
  }
  }
  
  for(int i = 0; i < _elementsInPanel; i++) {
  
  _target.panelElements[i] = (GUIZone)EditorGUILayout.ObjectField("Element " + i.ToString() ,_target.panelElements[i], typeof(GUIZone));
  
  }
  
  bool apply = GUILayout.Button( "Put elements in the panel");
  
  if(apply) {
  
  _target.applyElementsToThePanel();
  
  }
  
  bool _allign = GUILayout.Button("Allign with camera");
  
  if(_allign) {
  
  Vector3 cameraRotation = Camera.main.transform.eulerAngles;
  Vector3 cameraPosition = Camera.main.transform.position;
  Camera.main.transform.eulerAngles = new Vector3(90, 0, 0);
  Camera.main.transform.position = new Vector3(0, 10, 0);
  _target.transform.position = Vector3.zero;
  
  _target.transform.parent = Camera.main.transform;
  
  Camera.main.transform.eulerAngles = cameraRotation;
  Camera.main.transform.position = cameraPosition;
 
  }
  
  if(GUI.changed) {
  
  EditorUtility.SetDirty( _target );
  
  }
  
  }
 }


And here's the GUIZonePanel script:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class GUIZonePanel : GUIZone {
  
  public List<GUIZone> panelElements;
  
  public void applyElementsToThePanel () {
  
  Vector3 resultantPosition = Vector3.zero;
  
  for(int i = 0; i < panelElements.Count; i++) {
  
  resultantPosition += panelElements[i].transform.position;
  
  }
  
  transform.position = resultantPosition/panelElements.Count;
  
  foreach(GUIZone gz in panelElements) {
  
  gz.transform.parent = t$$anonymous$$s.transform;
  
  }
  }
  
  public void changeElementsVisibility (bool visible) {
  
  foreach(Renderer r in GetComponentsInC$$anonymous$$ldren<Renderer>()) {
  
  r.enabled = visible;
  
  }
  
  }
  
 }

The problem is that when I deselect the object that contains the GUIPanel and select it again, every change is losted. My element number get back to 0 and t$$anonymous$$s happens with almost every script where I have a list or an array and I need to attach elements to t$$anonymous$$s array in the Editor.

What can I make to save t$$anonymous$$s changes when the GO gets deselected?

Thanks from now!

Comment
Add comment · Show 1
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
avatar image GutoThomas · Jul 06, 2012 at 09:18 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Rafes · Jul 06, 2012 at 09:45 PM

The GUI's job is to display information and allow the user to interact with it. Anyt$$anonymous$$ng you want to store needs to be sent to the data model, w$$anonymous$$ch is your component script. In other words, you need to write everyt$$anonymous$$ng you want kept to a serializable script field, such as an int, float, string, list<>, an custom serializable class you make, etc.

Regarding a naming convention, I suggest calling it somet$$anonymous$$ng like "_uiPanelElementsBacker". The underscore is a general convention that denotes it is for internal use. A "backer" or "backing field" is a field (class member) that exists only to hold information for somet$$anonymous$$ng more publicly accessible.

[Edit: Your code formatting could be improved. I might have missed the actual cause. Please fix it and I won't be so lazy.]

Comment
Add comment · Show 3 · 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
avatar image GutoThomas · Jul 06, 2012 at 09:55 PM 0
Share
avatar image Rafes · Jul 06, 2012 at 10:09 PM 0
Share
avatar image GutoThomas · Jul 06, 2012 at 10:16 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Show and Edit Attributes of my list of structs in EditorWindow. 0 Answers

Is it possible to drag/drop/increment into editor array? 1 Answer

How can I make an array fill the contents of my GUI List? 1 Answer

Mission editor map save 1 Answer

How to add a reorderable list on CUSTOM EDITOR WINDOW? 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