- Home /
Organizing variables in the inspector
Hi, is there a way with classes and with javascript to do the same thing in the inspector that arrays does. The thing where you can fold out so you can see more variables. Lets say i got some code like this. What would i need to do so the class Attack and the Movement class is a part of the Enemy in the inspector but as a thing that you can fold out and in.
class Enemy
{
var Health : float;
}
class Attack
{
var Damage : int;
}
class Movement
{
var moveSpeed : float;
var normalSpeed : float;
}
var enemy = new Enemy();
Answer by Steven-Walker · Oct 18, 2011 at 07:10 PM
To organize your parameters in the inspector, you need to create an editor script. However, the code example you give won't really work. You need to create 1 class that encompasses all the attributes of your object, rather than being spread out across multiple classes/objects. For example, I'd move Damage, moveSpeed and normalSpeed all into the Enemy class. Then your editor script would look something like:
@CustomEditor(Enemy)
class EnemyEditor extends Editor {
private var showParams : boolean = true;
function OnInspectorGUI() {
showParams = EditorGUILayout.Foldout(showParams, "Enemy Parameters");
if(showParams) {
target.Health = EditorGUILayout.FloatField("Health", target.Health);
target.Damage = EditorGUILayout.FloatField("Damage", target.Damage);
target.moveSpeed = EditorGUILayout.FloatField("moveSpeed", target.moveSpeed);
target.normalSpeed = EditorGUILayout.FloatField("normalSpeed", target.normalSpeed);
}
}
}
Name your editor script something like 'EnemyEditor.js' and place it in Assets/Editor. Then when you select an object with the Enemy.js script, the inspector will display your custom editor script layout.
This looks promising. Cant test it out now as i'm not by a computer with unity. But just a question, say i got a couple of variables for lets say movement. Can i name a foldout like "$$anonymous$$ovement" and have all my movement variables in that foldout?
Ok, will try this tomorrow. Will come back if there is something thats not working. Thanks.
Hm, i tried this out but cannot get it to work. I put the EnemyEditor in a folder that i made thats in assets thats named Editor. But nothing happens when i press my object with the Enemy script on it? Any idea why it's not working? -Never $$anonymous$$d i figured it out the thing at the top (@CustomEditor($$anonymous$$ainEnemy)) was not same as my script name. But now i get a error "NullReferenceException: Object reference not set to an instance of an object". - And never $$anonymous$$d again.. Fixed it too. In the script Steven wrote he called health directly, that wont work you have to call the object you set the class to. So it will be "target.enemy.Health = EditorGUILayout.FloatField("Health", target.enemy.Health);" If you set your class like this "var enemy = new Enemy();".
Answer by syclamoth · Oct 19, 2011 at 10:29 AM
How about just doing it the simple way, using composition? I don't understand why the other two posts here are so complex!
class Enemy { var attack : Attack; var move : Movement;
}
Then, when you put it in your script they should show up in the inspector.
First, i think thats C# not javascript and second i want to be able to make foldouts where i can group together variables.
First, no it's not, second, the foldouts happen automatically! You don't need to make custom editors unless you're talking about really unusual behaviour.
This is the best answer; curious I can't vote it up.
Useful to notice is that in C#, you must add [System.Serializable] before the class definition, then it will show up. Of course, the class needs to be serializable...
Answer by chemicalvamp · Oct 18, 2011 at 06:18 PM
You could put your variables in a region like so:
#region Variables
//variables go here..
#endregion
But i have noticed monodevelope doesnt always put in the + and - in for collapsing it should look like this:
I don't think we're talking about the same thing, here.
Answer by Bilelmnasser · Jun 06, 2014 at 10:59 PM
Simple if i understand your " in and out " , test this two example :
#pragma strict
public class Enemy
{
var Health : float;
var attack:Attack;
var mouvement:Movement;
}
class Attack
{
var Damage : int;
}
class Movement
{
var moveSpeed : float;
var normalSpeed : float;
}
var enemy = new Enemy();
function Start () {
}
function Update () {
}
and this with array element for those 2 subclasses stript :
#pragma strict
public class Enemy
{
var Health : float;
var attack:Attack [];
var mouvement:Movement[];
}
class Attack
{
var Damage : int;
}
class Movement
{
var moveSpeed : float;
var normalSpeed : float;
}
var enemy = new Enemy();
function Start () {
}
function Update () {
}
Answer by Bryan-Legend · Oct 20, 2014 at 05:54 PM
I've added a feedback item to have attribute based categories added to the default inspector.
Please go here and vote for it: