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 erap129 · Aug 13, 2017 at 08:07 AM · editor-scriptingeditorwindowreflectioneditor scriptingeditorscript

Populating object fields in a loop - editor scripting

Hello, I am creating a custom editor window in which I want the user to be able to populate. The window currently looks like this (messy I know): alt text

I want the user to be able to change to monoscript field from "PositionChange" to any other monoscript. The problem is that these object fields are clearly created in a loop - and if I put the object field with this code for example:

 MonoScript transformationFunc = EditorGUILayout.ObjectField(transformationFunc, typeof(MonoScript), true) as MonoScript;

it won't let the user change the field because every time the window is drawn in OnGUI() the field is instantiated again. I tried using a dictionary and create "empty" monoscript objects for every field I know will appear in the window, but the only way to retrieve a monoscript from that dictionary was via the method "TryGetValue", and I couldn't figure out how to make the monoscript field affect the dictionary field using that function.

Any suggestions on how to let the user succesfully change this field? Thanks

p.s - the quantity and names of these fields of course are not known in advance because they depend on the prefab entered by the user (in the screenshot-Cylinder)

window.png (159.0 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by YoucefB · Sep 26, 2017 at 09:45 AM

@erap129 A way to do this would be to create a List of MonoScript then populate it in a for loop. and to get a Script just use its index.
Here is a simple example:

 List<MonoScript> monoscripts = new List<MonoScript> (); //List of scripts
 int capacity = 50; //how many fields


     public void myMonoScriptsLoader(){
         capacity = EditorGUILayout.IntField ("Capacity",capacity);
         capacity = Mathf.Max (0, capacity);
 
         //add any new fields
         for (int m = monoscripts.Count; m < capacity; m++)
             monoscripts.Add (new MonoScript());
 
         //remove extra fields
         for (int m = capacity; m <  monoscripts.Count; m++)
             monoscripts.RemoveAt (monoscripts.Count-1);
 
         for (int m = 0; m < monoscripts.Count; m++) {
             Rect r = EditorGUILayout.BeginHorizontal ();
             // delete a field
             if (GUI.Button (new Rect(r.width,r.y,r.height,20),EditorGUIUtility.Load("icons/vcs_delete.png") as Texture2D)) {
                 monoscripts.RemoveAt (m);
                 m--;
                 capacity--;
                 continue;
             }
             //display the field
             monoscripts[m] = (EditorGUILayout.ObjectField ("monoscript #"+(m+1),monoscripts[m], typeof(MonoScript), true,GUILayout.Width(200)) ) as MonoScript;
 
             EditorGUILayout.EndHorizontal ();
         }
     }
 

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

78 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

Related Questions

Is there a way to get all editable fields of a component in a script? 2 Answers

Dynamic content in custom EditorWindow 1 Answer

Acces monobehaviour script from editor script 2 Answers

MethodInfo.Invoke() throwing ArgumentException: failed to convert parameters 1 Answer

Text in custom editor is displayed/rendered with boxes around characters... 2 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