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 d34thst4lker · Mar 28, 2014 at 10:31 PM · c#editorinspectorclassescustom

How can I create a list of preset class/objects to be used in the editor

Sorry I wasn't really sure how to word the question and not exactly sure how to ask this but basically I want to know how exactly can I get this goal done;

After creating a class , Lets say "Equipment", I create an array of that Equipment class and preset all the variables in the inspector drop down. Now lets say I have 10 items in this Equipment array... How can I make that a permanent list for the game where I can now call "Equipment item" from a script and when I try to initialize the object in the inspector it will give me a drop down of the set list of equipment where I can now choose one of those to be that item?

I'm sorry in advanced if this is a bit confusing. If you have other questions that will clear things up a bit please let me know. I would really like to know how I can get this done.

Thanks!

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 frarees · Mar 29, 2014 at 12:28 PM

Ok so if I understood correctly, you want to define an array with some Equipment instances, and then a drop-down where you choose just one of them. Right?

If so, you'd have:

 public class MyScript : MonoBehaviour {
     public Equipment[] equipments;
     public Equipment currentEquipment;
 }

And editor related code (quick custom editor for MyScript, not optimized, so it won't be as fast as it should):

 [SerializeField]
 int index = 0;
 
 [SerializeField]
 MyScript myScript;
 
 void OnEnable () {
     if (myScript == null)
         myScript = target as MyScript;
 }
 
 public override void OnInspectorGUI () {
     string[] equipmentStrings = new string[myScript.equipments.Length];
     for (int i = 0; i < equipmentStrings.Length; i++) {
         equipmentStrings[i] = myScript.equipments[i].equipmentName; // choose here the string you want to show in the drop-down
     }
     serializedObject.Update ();
     EditorGUILayout.PropertyField (serializedObject.FindProperty ("equipments"));
     EditorGUI.BeginChangeCheck ();
     index = EditorGUILayout.Popup (index, equipmentStrings);
     if (EditorGUI.EndChangeCheck ()) {
         serializedObject.FindProperty ("currentEquipment").objectReferenceValue = myScript.equipments[index];
     }
     serializedObject.ApplyModifiedProperties ();
 }

Note that the example above doesn't support multi-edit out of the box (as we're accessing target, things will probably mess up if you enable multi-edit). It would require some tweaking.

Check the code yourself, as I've typed it straight in the browser and may contain typos.

Comment
Add comment · Show 11 · 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 d34thst4lker · Mar 30, 2014 at 12:53 AM 0
Share

Not sure if this is it but even when trying this all I'm getting in the Inspector is "$$anonymous$$ulti-object editing is not supported"

avatar image d34thst4lker · Mar 30, 2014 at 12:58 AM 0
Share

And also, maybe this would be a better explanation of what I want to achieve... Do you know how in the inspector if you have an enum variable, there will be a drop down to choose from the set of enum? Well picture that, a drop down of 'enum' (in this case custom class), that will display a list of 'name' s and then when one is chosen then that part of the inspector will populate with the associated attributes that was predefined with that element from the predefined array

avatar image d34thst4lker · Mar 30, 2014 at 01:01 AM 0
Share

Also, I don't want to actually create a custom editor for that script alone. I want that any script that will call an instance of my custom class to have this set feeling to it. That allows to do the above action.

avatar image d34thst4lker · Mar 30, 2014 at 01:43 AM 0
Share

Dam... Ok, I'm sorry. So I tested it again and I think it sort of worked but not fully... I'm getting an error saying Cannot Implicity convert type Equipment to Unity.Object for the line that says "serializedObject.FindProperty ("currentEquipment").objectReferenceValue = myScript.equipments[index];"

avatar image iwaldrop · Mar 30, 2014 at 03:17 AM 0
Share

This is a very contrived example which demonstrates the changing of enum value to draw different views in the inspector. There are better ways to actually do the drawing, but this way proves that different code can be executed via switching the enum value.

Test.cs

 using UnityEngine;
 using System.Collections;
 
 public enum Type
 {
     One,
     Two,
     Three,
 }
 
 public class Test : $$anonymous$$onoBehaviour
 {
     public Type type;
 }
 

TestInspector.cs

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 [CustomEditor(typeof(Test))]
 public class TestInspector : Editor
 {
     private Test instance;
 
     void OnEnable()
     {
         instance = (Test)target;
     }
 
     public override void OnInspectorGUI ()
     {
         instance.type = (Type)EditorGUILayout.EnumPopup("Type", instance.type);
 
         switch(instance.type)
         {
         case Type.One:
             DrawTypeOne();
             break;
         case Type.Two:
             DrawTypeTwo();
             break;
         case Type.Three:
             DrawTypeThree();
             break;
         }
     }
 
     void DrawTypeOne()
     {
         GUILayout.Label("Type one selected!");
     }
     
     void DrawTypeTwo()
     {
         GUILayout.Label("Type two selected!");
     }
     
     void DrawTypeThree()
     {
         GUILayout.Label("Type three selected!");
     }
 }
 
Show more comments

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

22 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

Related Questions

How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers

Get if inspector field labels are on separate line 1 Answer

Unity editor and inspector header 0 Answers

Can Editor Windows function like the inspector ? 2 Answers

How To Draw a PropertyDrawer within a PropertyDrawer 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