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 Essential · Nov 22, 2012 at 10:55 AM · guieditorinspectorarraysclasses

Customize arrays and classes in inspector window

 var levels : Levels[];
 
 class Levels
 {
     var areas : Areas[];
 }
 
 class Areas
 {
     var map : mapType;
     var game : gameType;
     var rounds : EachRound[];
 }
 
 enum mapType { Park, Street, Graveyard, House }
 enum gameType { Escape, Survival, Graverobber, Lockdown }
 
 class EachRound
 {
     var time : int;
     var enemiesToKill : int;
     var houseIntegrityMin : int;
     var bats : batType;
     var werewolves : werewolfType;
     var waves : int;
     var reward : int;
 }
 
 enum batType { None, Bats, PoisonBats, Bats_PoisonBats }
 enum werewolfType { None, Werewolves, StrongWerewolves, Werewolves_StrongWerewolves }

The level parameters in my game are arranged in hierarchies of classes and arrays, as in the code above. I'd like to access these and tweak via the inspector window but currently this displays in the inspector in a bit of a confusing way, with a variety of "Element 0, Element 1, etc." items. I want to put in a little effort to make it more readable and user friendly. Even just replacing those "Element" names with their true identities would be incredibly helpful. I've attached an image of how it looks now, and a mockup of how I'd ideally like it to appear.

I spent the last two days getting my head around custom editor scripting but it doesn't seem like an easy task at all. From what I've learned, I can't just simply make a few modifications to how the information is displayed, I'd need to re-structure reading and writing of arrays and classes and… it just seems like a huge task that is beyond my knowledge. Is there hopefully some other solution that I've missed to editing this data?

Current layout

alt text

Preferred custom layout

alt text

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CodeMasterMike · Nov 22, 2012 at 11:37 AM

If you want it to look like on the picture, you need to re-arrange your script so it will look like you want.

But you can also make a custom inspector, where you might have little more freedom in how you want to display your data. But it might take some time and effort to make it readable and look good.

If you want to have a custom inspector, you need to create a editor class. See more in the EditorGUILayout class

A Bare basic setp of the class would look something like this:

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 [CanEditMultipleObjects]
 [CustomEditor (typeof(YourClass))]
 public class CustomInspector : Editor {   
     
     public void OnEnable()    
     {        
 
     }
     
     public override void OnInspectorGUI()    
     {   
         // Do EditorGUILayout work here...
 
         SceneView.RepaintAll();
     }
 }

Now you can add your own, custom inspector parameters as you would like them to be. Here is a Answer link where you can read more about exteding the editor:

Tutorials on extending the editor

Good luck!

Comment
Add comment · Show 4 · 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 Essential · Nov 22, 2012 at 12:17 PM 0
Share

Hmm… I spent the last two days looking at custom editor scripts. I've got my head around how to display things and work with basic variables but I can't figure out how to work with arrays and classes… specifically, finding x number of arrays and x number of classes and have all that data be editable and reflect those changes back into those arrays. It seems like it's an incredibly complicated task.

avatar image CodeMasterMike · Nov 22, 2012 at 12:24 PM 0
Share

The good thing with the custom Inspector is that you don't need to change your class setup, you just need to work out how to display the data in the inspector.

Yes its takes some good planning and design to make it, but I think it will be worth it in the end.

avatar image CodeMasterMike · Nov 22, 2012 at 12:36 PM 1
Share

I would design the inspector something liket this, based on your prefered picture above:

  ->Level (popup with all your created levels from a level list   to  choose from) - Data below is changed depending on which level is chosen

     -> $$anonymous$$ap (enum popup)
     -> Game (enum popup)
     -> Rounds (popup with all created Rounds from a rounds list) - Data below is changed depending on which round is chosen

         ->Time (IntField)
         ->Enemies to kill (IntField)
         ->house Integry $$anonymous$$in (IntField)
         -> Bats (enum popup)
         -> Werewolves (enum popup)
         -> Waves (IntField)
         -> Reward (IntField)

Easiest would be to let the Editor class access your level list, and from that one take out the data it needs.

If you want to be able to add, lets say a Level or a round, you add a button that says "Add level" or "Add Round".

The "Add level" button creates a new instance of a Level and adds it to the Level list.

The "Add Round" button creates a new instance of Rounds and adds it to the Rounds list inside the current Level instance that the Level popup has as active. Notice that I removed the "rounds" variable, since I would rather have a button to activly add a new or remove a existing round, ins$$anonymous$$d or writing a int variable.

$$anonymous$$aybe this helps you a little :-)

avatar image Essential · Nov 22, 2012 at 01:46 PM 0
Share

Shoot! Actually you made it sound easy that way $$anonymous$$ike. Seems I'd been taking an unnecessarily complicated route. Thanks a lot, I'm going to give this a try :)

avatar image
2

Answer by Essential · Nov 26, 2012 at 12:33 PM

After a little effort learning the Custom Editor this is the new result. Quite a dramatic difference from what I originally wanted after a little inspiration from Mike's post. Code is beneath if it can help anyone out (not very well-commented but might be useful!)

alt text

 import System.Collections.Generic;        // So I can use lists
 
 @CustomEditor (LevelData)
 
 class LevelData_CustomInspector extends Editor
 {    
     var levelIndex = 0;
     var areaIndex = List.<int>();
     var roundIndex = List.<List.<int> >();
 
     function Awake()
     {
         // Reset
         //ResetValues();
         
         // Retrieve all inspector window selections
         levelIndex = EditorPrefs.GetInt("levelIndex", 0);        // Levels
         // Areas
         if ( !EditorPrefs.HasKey("areaIndex") )
             return;
         var areaArray : int[] = EditorPrefsX.GetIntArray ("areaIndex");
         for ( var i = 0; i < areaArray.Length; i++ )
         { areaIndex.Add(areaArray[i]); }
         // Rounds
         i = 0;
         while ( EditorPrefs.HasKey("roundIndex"+i) )
         {
             var roundIndex_temp = List.<int>();
             var roundArray : int[] = EditorPrefsX.GetIntArray ("roundIndex"+i);
             for ( var j = 0; j < roundArray.Length; j++ )
                 roundIndex_temp.Add(roundArray[j]);    
             roundIndex.Add(roundIndex_temp);
             i++;
         }
     }
     
     function ResetValues()
     {    // Resets all stored values
         EditorPrefs.DeleteKey("levelIndex");
         EditorPrefs.DeleteKey("areaIndex");
         var i = 0;
         while ( EditorPrefs.HasKey("roundIndex"+i) )
         {
             EditorPrefs.DeleteKey("roundIndex"+i);
             i++;
         }
         Debug.Log("All stored values reset");
     }
 
     function OnDestroy()
     {
         // Save all inspector window selections
         EditorPrefs.SetInt("levelIndex", levelIndex);        // Levels
         // Areas (check that areas exist)
         if ( areaIndex.Count < 1 )
             return;
         var areaArray : int[] = areaIndex.ToArray();
         EditorPrefsX.SetIntArray ("areaIndex", areaArray);
         // Rounds (check that rounds exist)
         for ( var i = 0; i < roundIndex.Count; i++ )
         {
             if ( roundIndex[i].Count > 0 )
                 var tempArray : int[] = roundIndex[i].ToArray();
             else
                 tempArray[0] = 0;
             EditorPrefsX.SetIntArray ("roundIndex"+i, tempArray);
         }
     }
 
     function OnInspectorGUI()
     {
         GUILayout.Space (20);
         // Level select
         GUILayout.BeginHorizontal();
         var parsedLevels = new String[target.levels.Count];
         for ( var i = 0; i < parsedLevels.Length; i++ )
             { parsedLevels[i] = "Level " + (i+1).ToString(); }
         levelIndex = GUILayout.Toolbar ( levelIndex, parsedLevels, EditorStyles.toolbarButton );
         GUILayout.Space (10);
         if ( GUILayout.Button("+", EditorStyles.toolbarButton, GUILayout.Width(50)) )
         {
             if ( target.levels.Count < 8 )
             {
                 target.levels.Add(target.Levels);
                 areaIndex.Add(0);        // Also increase areaIndex array
                 roundIndex.Add( new List.<int>() );        // Also increase roundIndex array 1st tier
             }
             else
                 Debug.Log("Maximum number of levels is 8");
         }
         if ( GUILayout.Button("-", EditorStyles.toolbarButton, GUILayout.Width(50)) && target.levels.Count > 0 )
         {    
             target.levels.RemoveAt(target.levels.Count -1);
             areaIndex.RemoveAt(areaIndex.Count -1);        // Also decrease areaIndex array
             roundIndex.RemoveAt(roundIndex.Count -1);        // Also decrease roundIndex array
             if ( levelIndex > target.levels.Count -1 )
                 levelIndex--;        // Lower levelIndex if player had it selected upon deletion
             if ( levelIndex < 0 )
                 levelIndex = 0;
         }
         GUILayout.EndHorizontal();
 
         GUILayout.Space (5);
         // Area select
         GUI.backgroundColor = Color(0.95,0.99,1,1);
         if ( target.levels.Count < 1 || target.levels[levelIndex] == null )
             return;        // Quit if there are no levels yet
         GUILayout.BeginHorizontal();
         var parsedAreas = new String[target.levels[levelIndex].areas.Count];
         for ( i = 0; i < parsedAreas.Length; i++ )
             { parsedAreas[i] = "Area " + (i+1).ToString(); }
         areaIndex[levelIndex] = GUILayout.Toolbar (areaIndex[levelIndex], parsedAreas, EditorStyles.toolbarButton );
         GUILayout.Space (10);
         if ( GUILayout.Button("+", EditorStyles.toolbarButton, GUILayout.Width(50)) )
         {
             if ( target.levels[levelIndex].areas.Count < 10 )
             {
                 target.levels[levelIndex].areas.Add(target.Areas);
                 roundIndex[levelIndex].Add(0);        // Also increase roundIndex array 2nd tier
             }
             else
                 Debug.Log("Maximum number of areas is 10");
         }
         if ( GUILayout.Button("-", EditorStyles.toolbarButton, GUILayout.Width(50)) && target.levels[levelIndex].areas.Count > 0 )
         {
             target.levels[levelIndex].areas.RemoveAt(target.levels[levelIndex].areas.Count -1);
             roundIndex[levelIndex].RemoveAt(roundIndex[levelIndex].Count -1);        // Also decrease roundIndex array 2nd tier
             if ( areaIndex[levelIndex] > target.levels[levelIndex].areas.Count -1 )
                 areaIndex[levelIndex] -= 1;        // Lower areaIndex if player had it selected upon deletion
             if ( areaIndex[levelIndex] < 0 )
                 areaIndex[levelIndex] = 0;
         }
         GUILayout.EndHorizontal();
         
         // Start Area Box
         EditorGUILayout.BeginVertical(EditorStyles.textField);
         GUILayout.Space (15);
         // Area details
         if ( target.levels[levelIndex].areas.Count < 1 || target.levels[levelIndex].areas[areaIndex[levelIndex]] == null )
             return;        // Quit if there's no areas for this level yet        
         var currentArea = target.levels[levelIndex].areas[areaIndex[levelIndex]];
         // Convert enum names to string
         var mapString = System.Enum.GetNames(typeof(currentArea.map));
         var gameString = System.Enum.GetNames(typeof(currentArea.game));
         // Map select
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(1,0.9,0.25,0.75);
         GUILayout.Label ("Map", GUILayout.Width(70));
         currentArea.map = GUILayout.Toolbar (currentArea.map, mapString, EditorStyles.toolbarButton);
         GUILayout.EndHorizontal();
         GUILayout.Space (10);
         // Game select
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(0.75,1,0.9,1);
         GUILayout.Label ("Game", GUILayout.Width(70));
         currentArea.game = GUILayout.Toolbar (currentArea.game, gameString, EditorStyles.toolbarButton);
         GUILayout.EndHorizontal();
         GUILayout.Space (10);
         // Round count
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(0.99,0.95,1,1);
         GUILayout.Label ("Rounds", GUILayout.Width(70));
         GUILayout.Label (target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count.ToString(), GUILayout.Width(40));
         if ( GUILayout.Button("+", EditorStyles.toolbarButton, GUILayout.Width(30)) )
             target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Add(target.Rounds);
         if ( GUILayout.Button("-", EditorStyles.toolbarButton, GUILayout.Width(30)) && target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count )
         {
             target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.RemoveAt(target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count -1);
             if ( roundIndex[levelIndex][areaIndex[levelIndex]] > target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count -1 )
                 roundIndex[levelIndex][areaIndex[levelIndex]] -= 1;        // Lower roundIndex if player had it selected upon deletion
             if ( roundIndex[levelIndex][areaIndex[levelIndex]] < 0 )
                 roundIndex[levelIndex][areaIndex[levelIndex]] = 0;
         }
         GUILayout.EndHorizontal();
         GUILayout.Space (10);
         // End Area box
         EditorGUILayout.EndVertical();
         // Round dropdown
         if ( target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count < 1 || target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds[roundIndex[levelIndex][areaIndex[levelIndex]]] == null )
             return;        // Quit if there are no rounds yet
         var parsedRounds = new String[target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds.Count];
         for ( i = 0; i < parsedRounds.Length; i++ )
             { parsedRounds[i] = "Round " + (i+1).ToString(); }
         roundIndex[levelIndex][areaIndex[levelIndex]] = EditorGUILayout.Popup (roundIndex[levelIndex][areaIndex[levelIndex]], parsedRounds, EditorStyles.toolbarDropDown);
         
         // Round details
         // if escape is selected, hide certain variables, and so on for each game type...
         EditorGUILayout.BeginVertical(EditorStyles.textField);
         GUILayout.Space (10);
         GUI.backgroundColor = Color.white;
         EditorGUIUtility.LookLikeInspector ();
         var currentRound = target.levels[levelIndex].areas[areaIndex[levelIndex]].rounds[roundIndex[levelIndex][areaIndex[levelIndex]]];
         currentRound.reward = EditorGUILayout.IntField ("Reward", currentRound.reward);
         currentRound.time = EditorGUILayout.IntField ("Time", currentRound.time);
         currentRound.enemiesToKill = EditorGUILayout.IntField ("Enemies to Kill", currentRound.enemiesToKill);
         currentRound.houseIntegrityMin = EditorGUILayout.IntSlider ("Minimum House Integrity %", currentRound.houseIntegrityMin, 0, 100);
         currentRound.waves = EditorGUILayout.IntField ("Waves of Enemies", currentRound.waves);
         // Enemy select
         // Convert enum names to string
         var werewolvesString = System.Enum.GetNames(typeof(currentRound.werewolves));
         var batsString = System.Enum.GetNames(typeof(currentRound.bats));
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(1,0.9,0.75,1);
         EditorGUILayout.LabelField ("Werewolves");
         currentRound.werewolves = EditorGUILayout.Popup (currentRound.werewolves, werewolvesString, EditorStyles.toolbarPopup);
         GUILayout.EndHorizontal();
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(0.9,1,0.75,1);
         EditorGUILayout.LabelField ("Bats");
         currentRound.bats = EditorGUILayout.Popup (currentRound.bats, batsString, EditorStyles.toolbarPopup);
         GUILayout.EndHorizontal();
         // Weapons
         GUILayout.Space (5);
         GUILayout.BeginHorizontal();
         GUI.backgroundColor = Color(0.9,0.9,0.9,1);
         GUILayout.Space(50);
         currentRound.pistol = GUILayout.Toggle(currentRound.pistol, "Pistol", EditorStyles.miniButton);
         currentRound.shotgun = GUILayout.Toggle(currentRound.shotgun, "Shotgun", EditorStyles.miniButton);
         currentRound.rifle = GUILayout.Toggle(currentRound.rifle, "Rifle", EditorStyles.miniButton);
         currentRound.automatic = GUILayout.Toggle(currentRound.automatic, "Automatic", EditorStyles.miniButton);
         GUILayout.Space(50);
         GUILayout.EndHorizontal();
         
         GUILayout.Space (10);
         EditorGUILayout.EndVertical();
     }
 }
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 CodeMasterMike · Dec 05, 2012 at 11:20 AM 0
Share

Looking really good!

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

11 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

Related Questions

How to add UnityEvent using Custom Inspector? 1 Answer

Possible to remove the Asset Browser button from an Objectfield? 0 Answers

Drawing a property in the inspector whose type could be one of several custom classes 2 Answers

Custom Inspector Invocation behaviour? 1 Answer

Problems With Variable Class Arrays... 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