• 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 gameplay4all · Jun 26, 2013 at 06:10 PM · inspectorvariablesclassesenumtype

if statement inside variables of a class

Hi all

I have made an inventory system but i would like to organize them some more. Now im using a class with variables like damage, strenghtreq, healthadd, potion effects etc. i also have a variable that declares the type of the item(it's an enum) now i have it setup in my script that it only uses variables like ''health add'' when it has a type like ''food''.

my question: how could.i make it that when i select e.g. 'weapon' that only then the variables in the inspector show up.

hope that my question is clear :)

-Gijs

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
1
Best Answer

Answer by equalsequals · Jun 26, 2013 at 06:49 PM

You will need to do some custom editor scripting for that.

What you want is a CustomEditor.

[Edit - Better explanation and example code]

Custom Editors are how you customize how the specified MonoBehaviour is inspected, you do this by using the CustomEditor attribute just before declaring the class, which binds that Editor class to your run-time MonoBehaviour, and then implementing the OnInspectorGUI method, which behaves basically identically to the OnGUI method of a MonoBehaviour.

One key difference between the OnGUI and OnInspectorGUI methods is that you have access to the EditorGUI and EditorGUILayout utility classes, as well as some others which are specific to Editor classes. For more info, just check the Scripting docs for Editor classes.

Here are some code examples, which I have tested and do work. Note that in order for any Editor class to work it must be in a folder called "Editor". (Crazy, I know.)

Example run-time class: (JS)

 #pragma strict
 public class Foo extends MonoBehaviour
 {
     var itemType : ItemType = ItemType.Sword;
     var strengthReq : int;
     var damage : int;
     var healthAdd : int;
     var manaAdd : int;
 }
 
 //our public enum
 public enum ItemType{Sword,Food}

Example Editor class: (JS)

 #pragma strict
 @CustomEditor(Foo) //This binds this Editor to the MonoBehaviour of type Foo.
 class FooEditor extends Editor
 {
     function OnInspectorGUI()
     {
         //we need to cast the target of this inspector as the class that it is to gain access to its members.
         var script : Foo = target as Foo; 
         
         script.itemType = EditorGUILayout.EnumPopup(script.itemType);
          
         switch(script.itemType)
         {
             case ItemType.Sword:
                    script.strengthReq = EditorGUILayout.IntField("Strength Req", script.strengthReq);
                    script.damage = EditorGUILayout.IntField("Damage", script.damage);
             break;
             
             case ItemType.Food:
                 script.healthAdd = EditorGUILayout.IntField("Health Add", script.healthAdd);
                    script.manaAdd = EditorGUILayout.IntField("Mana Add", script.manaAdd);
             break;
         }
     }
 }

The idea here is that you define what itemType is first, and then use Unity's immediate-mode GUI to your advantage by simply only drawing what you want to the Inspector, based on the value of itemType.

Hopefully that clears it up for you.

==

Comment
Add comment · Show 14 · 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 gameplay4all · Jun 26, 2013 at 09:09 PM 0
Share

Hi, thanks for your answer although i dont really understand it because i use javascript. it would be perfect if i could achieve something like this:

 var type : TypeEnum;
 if(type == TypeEnum.sword){
 var strengthReq : int;
 var damage : int;
 }
 else if(type == TypeEnum.food){
 var healthAdd : int;
 var manaAdd : int;
 }
 
 enum TypeEnum {sword, food}
avatar image equalsequals · Jun 26, 2013 at 09:55 PM 0
Share

That is only possible with a custom inspector. Is it just the language barrier you are struggling with or do you need a better explanation of editor extensions?

avatar image Eric5h5 · Jun 26, 2013 at 10:45 PM 0
Share

Actually you can use a PropertyDrawer in Unity 4.0+, which is nice because then you can use your custom type everywhere automatically, ins$$anonymous$$d of having to make a custom inspector for every script that uses it.

avatar image gameplay4all · Jun 27, 2013 at 06:33 AM 0
Share

It's the language barrier because i actually don't know what stands there or what it does. I'm pretty new to scripting so i still don't understand case statements and break statements. as for eric5h5: i read an article about that and i didn't saw the feature i wanted i only want to show type-specific variables whenever i select the appropriate type.

sorry for being such a time-taking question :)

avatar image gameplay4all · Jun 27, 2013 at 06:35 AM 0
Share

P.S i do need more explanation aboyt editor extensions.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 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

Related Questions

Change Inspector Variables Depending On Enum 2 Answers

Classes and type casting? 2 Answers

Array with multiple variables in Inspector 1 Answer

Accessible collection of Type 1 Answer

Getting the selected enum value set in the Inspector 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges