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
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, this 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 this 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 this 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 = this.transform;
  
  }
  }
  
  public void changeElementsVisibility (bool visible) {
  
  foreach(Renderer r in GetComponentsInChildren<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 this happens with almost every script where I have a list or an array and I need to attach elements to this array in the Editor.

What can I make to save this 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

Any one?!

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. Anything you want to store needs to be sent to the data model, which is your component script. In other words, you need to write everything 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 something 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 something 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

Thanks for your comment! A strange thing is that I have other script to create an AnimatedSprite, and this script has a Sprite class instance, which is a serialized class. It also contains a array that holds the frames, but seems like the changes are not being saved to this array, even the class being serializable.

*What you mean by horrible code formatting?

avatar image Rafes · Jul 06, 2012 at 10:09 PM 0
Share

Can you see the list in the debug inspector? If you can see it there, then you know you are safe, since that displays all serialized fields that will be saved. If you DO see it there, and it still isn't saved, then you know there is something deeper you are doing to wiping out your list.

Your code indentation makes it hard to follow for me. I have very very lazy eyes, hah.

avatar image GutoThomas · Jul 06, 2012 at 10:16 PM 0
Share

Hahahah! I don't know. It has some better format but it just dissapeared! Well, I could solve the problem following your hints! Thank you!

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

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

A node in a childnode? 1 Answer

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

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