Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Darinth · Oct 09, 2019 at 05:53 PM · guilayouteditorguilayout

Field creation order using (Editor)GUILayout creating odd behavior

So I'm writing a CustomEditor for a class and running into an issue (actually 2, but I'm just focusing on the one here). It looks like GUILayout (and EditorGUILayout) seem to cache the value of the active field and it assumes that the fields will always be in the same order and reapplies that cached value to the field in the same spot, which is often the wrong field. Take the code below.

 [CustomEditor(typeof(DebugRunner))]
 public class DebugRunnerEditor : Editor
 {
     LinkedList<int> Fields = new LinkedList<int>();
     int newField;
 
     public override void OnInspectorGUI()
     {
         for(LinkedListNode<int> node = Fields.First;node != null;node = node.Next)
         {
             node.Value = EditorGUILayout.IntField("Field ", node.Value);
         }
 
         newField = EditorGUILayout.IntField("New Field", newField);
         if (GUILayout.Button("Add"))
         {
             Fields.AddFirst(newField);
         }
 
     }
 }

I've just created a quick mockup CustomEditor for a debug class that I use to illustrate the problem. When the inspector first opens, it looks like figure A below. If I type 10 and click add, I get figure B. If you look closely, you can see the issue just start to show up already, but it's not very visible. Rather than my 'New Field' field being highlighted, the field I just added is highlighted. It looks like unity remembers that I'd previously selected the first field, and kept the first field selected even though the first field changed. If I add a 5 next I get figure C, which is where things really start to become problematic. When the script adds new elements, it adds them to the start of the list. So the first 5 is correct... but it looks like it replaced my 10 that I already had there with a 5 and if I'm not careful it will actually replace it. If I unselect the value without doing anything else, the 5 goes away and the 10 shows back up, at least most of the time. I did have at least one instance of the change being real, but was never able to replicate it while I was testing again.

alt text

What I'm actually doing is creating a custom editor for a class that includes a Dictionary<string,Stat> member, where Stat is an abstract class that can be a CalculatedStat (that uses a formula), a PureNumericStat (which just contains a number), or a ModifiableStat (which is designed to have a base value, which modifiers can be applied to). I use the CustomEditor to add those objects to the dictionary, and it's moving values around depending on how many new fields show up when I add a new stat. How do I get the unity editor to react properly to new fields that are being added between calls to OnInspectorGUI for a CustomEditor

captures.png (21.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

111 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer? 2 Answers

Best way to offset GUI element from scrollbar in custom inspector? 0 Answers

Setting height via GUILayout.Height() has no effect to BeginVertical in Inspector 0 Answers

Odd GUILayout Placement 0 Answers

What is the difference between editorguilayout and guilayout? 1 Answer


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