Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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 achieved so far. Today, I finished the basic structure and wanted to implement functionality. So far, when the user makes adjustments to something within the Inspector - those changes only affect the inspector and nothing 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 hitChance;
     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:
                 
                 //    This 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:
                 
                 //    This 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:
                 
                 //    This 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 - this message should never appear
                 //    if it does scream at developer!
                 Debug.LogWarning("Something 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 think 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 this variable :

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

I would like to have this 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 think 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 something like

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

This 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

thanks buddy

avatar image vbbartlett · Feb 14, 2013 at 04:00 PM 0
Share

Glad I could help

avatar image Wiking_Division · Jun 13, 2014 at 06:29 PM 0
Share

I have a very simillar question, and I always end up with NullReferenceException when I'm trying to get access to a list from target in editor

this one is equal to HealthScript:

 public class EventScript : $$anonymous$$onoBehaviour {
 
     public List<Action> actionList;
 
     public class Action
     {
         float time;
         FieldInfo[] fieldInfos;
         object[] fieldValues;
 
         public Action(float time, FieldInfo[] fieldInfos, string eventName)
         {
             this.time = time;
             this.fieldInfos = fieldInfos;
             this.fieldValues = new object[3];
         }
     }
 
     void Awake()
     {
         actionList = new List<Action> ();
         actionList.Add(new Action(5.56f, new FieldInfo[3], "name"));
         actionList.Add(new Action(5.56f, new FieldInfo[3], "name"));
         actionList.Add(new Action(5.56f, new FieldInfo[3], "name"));
     }
 
     // Use this for initialization
     void CreateEvent()
     {
         // Call constructor of Event via reflection
     }
 }

and here I,m trying to get access to fields:

         [CustomEditor(typeof(EventScript))]
         public class EventEditor : Editor {
         
             private EventScript the_script;
             public List<EventScript.Action> actions;
         
             void OnEnable()
             {
                 the_script = (EventScript)target;
                 // How to get access to List<Action> "events" from currently editing instance's of EventScript?
                 for(int i = 0; i<the_script.actionList.Count; i++)
                 {
                     Debug.Log("WOW");
                 }
             }
         
             public override void OnInspectorGUI()
             {
         
             }
         }
 
 

avatar image Wiking_Division · Jun 13, 2014 at 06:31 PM 0
Share

this causes error: NullReferenceException: Object reference not set to an instance of an object EventEditor.OnEnable () (at Assets/Editor/EventEditor.cs:16)

avatar image Wiking_Division · Jun 13, 2014 at 06:34 PM 0
Share

Unholy crap, I've found the problem! This works pretty fine in play mode!

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

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

Public Scene Variables not appearing in the inspector 2 Answers

Unity Editor - Class variable value contrain 4 Answers

ReImport in c# of GameObjects only for Scene Objects, not assets? 1 Answer

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Editor Script, Index Out of Range Exception after Play 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