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 Darkworth · Jul 07, 2016 at 09:08 PM · listeditor-scriptingeditorwindoweditor windowpropertyfield

Trying to do custom editor using propertyfield of a List<> but want to stuff each item with a dropdown picklist from another list.

Let me see if I can explain this appropriately...

I have an editor window for editing items in an inventory. The area I am working on is the portion for building recipes for craftable items. The class for craftable items contains a List for itemID's of each material needed to make the desired item.

 public List<int> materialIDs;

If I show this in the editor window using a propertyfield:

 SerializedProperty prop1 = serObj.FindProperty("craftItem").FindPropertyRelative("materialIDs");            
 EditorGUILayout.PropertyField (prop1, new GUIContent("Required material IDs"), true);

I get a result like this in the editor window:

alt text

What I am trying to do... is replace the textboxes shown in the default rendering of this list, with a dropdown selection of all of the items currently existing in the item database so that instead of having to go look up itemIDs for each material, the user can simply select the material item from the dropdown list and behind the scenes in the editor window it will assign the selected items itemID as that element in the materialIDs list.

ie this is the intended result: alt text

I have the initial rendered materialID list... I have a list pre-populated with all of the items currently existing in the database, is it possible for me to "stuff" the values of the existing items list into/in place of, the textboxes for the materialID list elements so that the user can select the desired item, instead of having to go look up its assigned ID?

Thanks

propertyfield-list-choices.png (21.8 kB)
propertyfield-list.png (4.3 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
Best Answer

Answer by Arkaid · Jul 08, 2016 at 12:55 AM

Yes, but it's not straightforward. You need to make your own drop down control. Here's some sample code that does what you need.

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(Example))]
 public class ExampleEditor : Editor
 {
     // ---------------------------------------------------
     // this is just a placeholder, you need to change this to fit your project
     // by taking the data from your database
     class ItemData
     {
         public int id;
         public string name;
     }
 
     List<ItemData> database = new List<ItemData>()
     {
         new ItemData() { id = -1, name = "(Unset)" },   // might want to keep an invalid id for unset items
         new ItemData() { id = 1, name = "Small Healing Potion" },
         new ItemData() { id = 2, name = "Medium Healing Potion" },
         new ItemData() { id = 3, name = "Large Healing Potion" },
     };
 
 
     // ---------------------------------------------------
     // prepare a list of item names to use with Popup();
     // Might be more efficient to do this once somewhere than
     // rebuild it each time the component gets selected
     string[] itemNames;
     void OnEnable()
     {
         itemNames = new string[database.Count];
         for (int i = 0; i < database.Count; i++)
             itemNames[i] = database[i].name;
     }
 
 
     // ---------------------------------------------------
     // Do the actual drawing
     bool listExpanded = true;
     public override void OnInspectorGUI()
     {
 
         SerializedProperty materialIDs = serializedObject.FindProperty("materialIDs");
 
         listExpanded = EditorGUILayout.Foldout(listExpanded, "Required material IDs");
         if (listExpanded)
         {
             materialIDs.arraySize = EditorGUILayout.IntField("Size", materialIDs.arraySize);
             for (int i = 0; i < materialIDs.arraySize; i++)
             {
                 // get serialized item id from component
                 int itemID = materialIDs.GetArrayElementAtIndex(i).intValue;
 
                 // assuming the array itemNames is in the same order as database, we can do this
                 int index = database.FindIndex(item => item.id == itemID);
 
                 // draw the combo box / popup / pulldown / whatchamacallit
                 index = EditorGUILayout.Popup("Element " + i, index, itemNames);
 
                 // now work backwards to update the property
                 itemID = database[index].id;
                 materialIDs.GetArrayElementAtIndex(i).intValue = itemID;
             }
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 }
 

Good Luck :D

Comment
Add comment · Show 1 · 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 Darkworth · Jul 08, 2016 at 03:18 PM 0
Share

@Arkaid Works perfectly, thank you! :D

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

50 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

Related Questions

A node in a childnode? 1 Answer

Editing a List Propertyfield in Editor Extension overwrites previously saved entries. How to fix? 1 Answer

Weird bug when dragging object onto EditorGUILayout.ObjectField 0 Answers

Text in custom editor is displayed/rendered with boxes around characters... 2 Answers

custom fogColor editorWindow undo problem 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