- Home /
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
Preferred custom layout
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!
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.
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.
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 :-)
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 :)
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!)
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();
}
}