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 Hrethric · Mar 14, 2013 at 02:37 PM · arrayinspectorcustom-inspectortool

Custom Inspector Array Elements Not Updating

I have looked everywhere for this answer, and no one seems to have this particular problem. I wrote a custom inspector for a ScriptableObject that allows me to add, and remove elements of an array. These elements are a custom class that do not extend anything - they're just a container for data (right now, functionality will be added later). The problem is that when I edit fields within an element, it does not update. Immediately upon hitting "Enter" the field will revert back to the default value. I've seen people complain about this happen upon hitting play, but never instantly after attempting to enter new values.

The inspector:

 using UnityEditor;
 using UnityEngine;
 using System;
 
 [CanEditMultipleObjects]
 [CustomEditor(typeof(MoveStateList))]
 public class MoveStateEditor : Editor
 {    
     // A reference to our target movestate list
     private MoveStateList MoveStates;
     private bool showState = false;
     
     private static GUIContent
         insertContent = new GUIContent("+", "Add a MoveState"),
         deleteContent = new GUIContent("-", "Delete this MoveState");
     
     private static GUILayoutOption
         buttonWidth = GUILayout.MaxWidth(20f);
     
     void Awake()
     {
         MoveStates = (MoveStateList)target;
     }
     
     public override void OnInspectorGUI()
     {
         //base.OnInspectorGUI();
         int i = 0;
         
         GUILayout.Label("Move States", EditorStyles.boldLabel);
         EditorGUI.indentLevel++;
         
         foreach(BaseMoveState state in MoveStates.MoveStates)
         {
             EditorGUI.indentLevel++;
             //i++;
             
                         // Add the foldout for elements, and the add/remove buttons for each state
             GUILayout.BeginHorizontal();
                 showState = EditorGUILayout.Foldout(showState, state.Name);
             
                 if(GUILayout.Button(insertContent, EditorStyles.miniButtonLeft, buttonWidth))
                 {
                     MoveStates.AddMoveState(i);
                     break;
                 }
                 
                 GUILayout.BeginVertical();
                     if(GUILayout.Button(deleteContent, EditorStyles.miniButtonRight, buttonWidth))
                     {
                         MoveStates.RemoveMoveState(state);
                         break;
                     }
                 GUILayout.EndVertical();
             GUILayout.EndHorizontal();
             
                         // If the element is expanded, show the properties
                         // Side note - if anyone knows how to get only INDIVIDUAL elements to       
                         // expand I'd love to hear about it
             if(showState)
             {
                 EditorGUILayout.TextField("Name", state.Name);
                 EditorGUILayout.FloatField("Acceleration", state.Acceleration);
                 EditorGUILayout.FloatField("Decceleration", state.Decceleration);
                 EditorGUILayout.FloatField("Turn Speed", state.TurnSpeed);
                 EditorGUILayout.FloatField("Bank Speed", state.BankSpeed);
                 EditorGUILayout.FloatField("Max Bank Angle", state.MaxBankAngle);
                 EditorGUILayout.FloatField("Climb Speed", state.ClimbSpeed);
                 EditorGUILayout.FloatField("Max Climb Angle", state.MaxClimbAngle);
                 EditorGUILayout.FloatField("Dive Speed", state.DiveSpeed);
                 EditorGUILayout.FloatField("Min Dive Angle", state.MinDiveAngle);
             }
             
             EditorGUI.indentLevel--;
             
             if(GUI.changed)
             {
                 EditorUtility.SetDirty(MoveStates);
                 EditorUtility.SetDirty(target);
             }
         }
         
         if(GUILayout.Button("Add")) MoveStates.AddMoveState(MoveStates.GetSize());
         
         i = 0;
     }
 }

The MoveStateList:

 using UnityEngine;
 using UnityEditor;
 using System;
 
 public class MoveStateListAsset
 {
     [MenuItem("Assets/Create/MoveStates")]
     public static void CreateAsset ()
     {
         ScriptableObjectUtility.CreateAsset<MoveStateList> ();
     }
 }

The Container Class:

 using System;
 [System.Serializable]
 public class BaseMoveState
 {    
     private static int CurrentID            = 0;
     private int ID                            = -1;
     public string Name                        = "NewMoveState";
     public float Acceleration                 = 0f;
     public float Decceleration                 = 0f;
     public float TurnSpeed                    = 0f;
     public float BankSpeed                     = 0f;
     public float MaxBankAngle                 = 0f;
     public float ClimbSpeed                 = 0f;
     public float MaxClimbAngle                 = 0f;
     public float DiveSpeed                     = 0f;
     public float MinDiveAngle                 = 0f;
     public BaseMoveState BoostMoveState        = null;
 }
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
2
Best Answer

Answer by whydoidoit · Mar 14, 2013 at 02:39 PM

You need to do this kind of thing:

  state.Name = EditorGUILayout.TextField("Name", state.Name);

The return value is the updated one...

Comment
Add comment · Show 4 · 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 Hrethric · Mar 14, 2013 at 03:03 PM 0
Share

Yup I just figured that out right before this finally posted. Thanks for the response - for some reason I was thinking too much in C++ and that state.Name was a pointer that would get modified directly.

avatar image Fattie · Mar 14, 2013 at 03:03 PM 0
Share

please post comments AS CO$$anonymous$$$$anonymous$$ENTS cheers

avatar image Hrethric · Mar 14, 2013 at 03:17 PM 0
Share

Sorry about that, new to this UnityAnswer stuff. :)

avatar image Bunny83 · Mar 14, 2013 at 03:25 PM 2
Share

btw. You should remove the "CanEdit$$anonymous$$ultipleObjects" attribute because you implemented a single object editor. To edit multiple objects you should use the SerializedObject ins$$anonymous$$d of "target". Editing multiple objects can be a bit tricky, so if you don't need this feature you should just removed the attribute.

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

13 People are following this question.

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

Related Questions

Custom Inspector & Arrays 0 Answers

Cutom Array Inspector in a Custom Inspector 1 Answer

How to show an array of custom objects in Inspector? 2 Answers

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers

Creating enum using a string array 3 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