• 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 Kergal · Feb 13, 2013 at 04:08 PM · c#editorvariableinspectoreditorguilayout

Custom Inspector - How to add functionality?

Hello community,

I apologize in advance for not always using proper terms.

For the last 2 days I have been learning about how to modify the UI of the Inspector in Unity. I am quite happy with the result I have ac$$anonymous$$eved so far. Today, I finished the basic structure and wanted to implement functionality. So far, when the user makes adjustments to somet$$anonymous$$ng wit$$anonymous$$n the Inspector - those changes only affect the inspector and not$$anonymous$$ng else.

Basic Setup : I want to make my work easier. Instead of modifying tons of values each time I create a new "enemy" ,"player" or "npc" object - i just want to use my custom inspector. Here is some of the code I used :

 using UnityEditor;
 using System.Collections;
 using UnityEngine;
 
 [CustomEditor(typeof(HealthScript))]
 public class ControlInterface : Editor {
 
 //    ---------------------- START VARIABLES -----------------------------------    //
     SerializedObject serObj;   // i have no idea what that is 
     
     private bool isActiveOnCharacter = true;
     private bool useHealthTex =false;
     private bool monoHealthBar = true;
     private bool canRegenHealth = false;
     private bool canAttack = false;
 
     
     private string tagForObject="";
     private string tagForEnemy ="";
     
     private int characterTypeIndex;
     private string[] characterTypeOptions = new string[4] {"not Defined", "Player","Mob (hostile)","NPC (neutral)"};
     private string characterName="";
     
     SerializedProperty myMaxHealth; // again not much of an idea how that works
     private float myCurHealth = 0f;
 
     private float regenRate = 0f;
     private string[] RegenRateSamples = new string[4] {"slow","normal","fast","custom" };
     private int regenIndex =0;
     
     public Color green;
     
     public Transform myEnemy;
     private float baseDamage;
     private float $$anonymous$$tChance;
     private float coolDown;
 //    ---------------------- End VARIABLES -------------------------------------    //
     
     public void OnEnable(){            // got the idea for the following 2 code lines from an existing unity script ( antialiasing)
 
         serObj = new SerializedObject(target);    
         
         myMaxHealth = serObj.FindProperty("myMaxHealth");
         
     }
     
     public override void OnInspectorGUI() {
         
         EditorGUIUtility.LookLikeInspector();
     
         isActiveOnCharacter = EditorGUILayout.Foldout(isActiveOnCharacter,"Character Setup:");
         
         EditorGUILayout.Separator();        
         
         if(isActiveOnCharacter){    
         
             characterTypeIndex = EditorGUILayout.Popup("Character Type: ",characterTypeIndex, characterTypeOptions);
         
             switch(characterTypeIndex){
             
             //    NOTHING CHOSEN
             case 0:
                 
                 //    THis function resets some of the specific values previously entered
                 //    and moreover is the default setting of "Character Type: ".
                 NotSpecified();
                 
                 break;
             
         
             
             //    PLAYER CHOSEN
             case 1:
                 
                 //    T$$anonymous$$s function allows specifications about the Player object
                 //    and contains several functions that are being shared by the
                 //    different object types. 
                 SpecifiedPlayerObject();
                 
                 break;
                             
 
             //    MOB CHOSEN
             case 2:
                 
                 //    T$$anonymous$$s function allows specifications about the Mob object
                 //    and contains several functions that are being shared by the
                 //    different object types.
                 SpecifiedMobObject();
                 
                 break;
             
             //    NPC CHOSEN
             case 3:
                 
                 //    T$$anonymous$$s function allows specifications about the NPC object
                 //    and contains several functions that are being shared by the
                 //    different object types.                
                 SpecifiedNPCObject();
                 
                 break;
             //    Catch loopholes    
             default:
                 
                 //    Throw out a LogWarning - t$$anonymous$$s message should never appear
                 //    if it does scream at developer!
                 Debug.LogWarning("Somet$$anonymous$$ng went wrong ...!");
                 
                 break;
             }
         }    
     }
     
     
 //    ----------------------------Chosen Character------------------------------    //
     
     private void NotSpecified(){
         
         EditorGUILayout.BeginHorizontal("Button");
         
             EditorGUILayout.LabelField( "Please specify character type!");
         
         EditorGUILayout.EndHorizontal();
         //    ---------------Reset some Variables---------------    //         
         
         tagForObject="";
                 
         myMaxHealth = 0;
         
         myCurHealth = 0;
         
         regenIndex  = 0;
         
         regenRate   = 0;
         
         tagForEnemy ="";
         
         //    ---------------Reset some Variables---------------    //
     }
     
     private void SpecifiedPlayerObject(){
         
         EditorGUILayout.BeginVertical();
         
         SetupHealth ();
         
         GUILayout.Box("", new GUILayoutOption[]{GUILayout.ExpandWidth(true), GUILayout.Height(1)});
         
         PlayerOnlyHealthBarDisplay();
     
         GUILayout.Box("", new GUILayoutOption[]{GUILayout.ExpandWidth(true), GUILayout.Height(3)});
     
         SetupBasicAttack();
         
         EditorGUILayout.EndVertical();
         
     }


I have been trying to understand the basic principals using the Image Effect scripts from Unity. and in there it appeared as if they would simply say - every variable in my editor class is of type SerializedProperty and i would just assign the value inputed via

SerializedObject serObj;

some variable in my editor class =serObj.FindProperty("some variable I have in my "true" class);

i don't t$$anonymous$$nk that would work quite as I would want it to work.

Example :

I created a variable myMaxHealth in the Editor Class. (assume it was an int) . Then somewhere in the inspector I am storing a value to t$$anonymous$$s variable :

 myMaxHealth = EditorGUILayout.IntSlider(....);

I would like to have t$$anonymous$$s value been the new value of my true myMaxHealth variable in the real HealthScript class (not the editor).

Can you help me ? . I really tried finding a solution, but I am struggling. I know, I am quite far off track with my solutions... - at least I t$$anonymous$$nk I am .

Thank you,

Daniel

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 vbbartlett · Feb 13, 2013 at 08:31 PM

Basics: target is the instance of the class that the editor is displaying.

Thus if you have

 HealthScript hs = (HealthScript)target;
 

then if you actually want the gui controls to directly effect the instance you would do somet$$anonymous$$ng like

 hs.m_maxHealth = EditorGuiLayout.IntSlider(hs.m_maxHealth,...)
  

T$$anonymous$$s sets the slider to the value of the instance and then when it changes applies that change back to the instance.

Comment
Add comment · Show 5 · 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 Kergal · Feb 14, 2013 at 10:10 AM 0
Share
avatar image vbbartlett · Feb 14, 2013 at 04:00 PM 0
Share
avatar image Wiking_Division · Jun 13, 2014 at 06:29 PM 0
Share
avatar image Wiking_Division · Jun 13, 2014 at 06:31 PM 0
Share
avatar image Wiking_Division · Jun 13, 2014 at 06:34 PM 0
Share

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Unity Editor - Class variable value contrain 4 Answers

Public Scene Variables not appearing in the inspector 2 Answers

Is there a Function Callback for when a vairable is altered in the Inspector? 1 Answer

How to optimize a custom editor ? 3 Answers

Edit child in parent list [Custom Inspector] 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