is it possible to group my variables in tabs in the unity inspector?
I am going to have a ton of animation variables that I want to hide in nested tabs
is it possible to group my variables in tabs in the unity inspector?
I am going to have a ton of animation variables that I want to hide in nested tabs
The quick way is to have the animation variables in a serializable nested class that is instantiated on initialization. For instance:
public class ExampleScript : MonoBehaviour
{
[System.Serializable]
public class NestedClass
{
public int nestedInteger = 0;
public bool nestedBool = false;
}
public NestedClass nestedClass = new NestedClass();
}
The biggest problems with this are that it’s easy to forget about the scope limitations from within the nested classes (trying to access variables at the ExampleScript scope from within a NestedClass function obviously won’t work), it makes the scripts more difficult to read, and it’s too easy to pass around big chunks of data from your script rather than just safely referencing the script itself. If you make the nested class definition and the instance member private, you can avoid the last issue, and if you give the instance member the [SerializeField] attribute, it’ll still show up in the inspector so you can tweak the values as needed. I use this setup pretty frequently, myself.
The slower and more customizable method is to write a custom editor script and display the members in the inspector any which way you choose.
If your variables belong to one class you should put them in one scope but if you can separate them to several classes then do it. It can cause more understandable and readable
[System.Serializable]
public class Player{
public Player_info player_info;
public Player_currency player_currency;
...
}
[System.Serializable]
public class Player_info{
public int id;
public string name;
public string password;
}
[System.Serializable]
public class Player_currency{
public int coin;
public int Gem;
}
the [System.Serializable] method just makes the properties hidden from the inspector
using System;
using UnityEngine.AI;
using System.Security.AccessControl;
using UnityEditor;
[CustomEditor(typeof(LinkAction))]
[CanEditMultipleObjects]
[RequireComponent(typeof(Animation))]
public class LinkAction : MonoBehaviour {
[System.Serializable]
public class Modifiers
{
public float MoveSpeed = 4;
public float AnimSpeed = 4;
}
[System.Serializable]
public class Animations
{
[System.Serializable]
public class Idle
{
public Animation IdleAnimDown;
public Animation IdleAnimLeft;
public Animation IdleAnimUp;
public Animation IdleAnimRight;
}
[System.Serializable]
public class Walk
{
public Animation IdleAnimDown;
public Animation IdleAnimLeft;
public Animation IdleAnimUp;
public Animation IdleAnimRight;
}
}
private Vector2 inputAxis;
private Rigidbody2D rb2d;
/*
void Start(){
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
inputAxis = ZeldaEngine.UserInput.GetAxis(ZeldaEngine.UserInput.X,ZeldaEngine.UserInput.Y);
//rb2d.velocity = new Vector2(inputAxis.x * MoveSpeed, inputAxis.y * MoveSpeed);
}
*/
}
Also it throws a error:
LinkAction uses the CustomEditor attribute but does not inherit from Editor.
You must inherit from Editor. See the Editor class script documentation.
UnityEditor.CustomEditorAttributes:FindCustomEditorType(Object, Boolean)
You need to create an instance of those classes as well- look at line 9 in my example. The Serializable attribute doesn’t hide a class from the inspector, quite the opposite, but not having any sort of an object to show means nothing is going to be visible.
Your error has nothing to do with the nested classes- you’re trying to do both nested classes and a custom editor script at the same time without managing them separately first. A custom editor is a completely separate script that handles how the information in a MonoBehaviour class is displayed in the inspector, so it derives from Editor and not MonoBehaviour and you can’t just shove the data into your MonoBehaviour class. Avoid all of that for now, and just try to get the nested classes working.
@DonLoquacious I don’t understand you said
“The quick way is to have the animation variables in a serializable nested class that is instantiated on initialization”
that’s what I’m trying to do
I put everything in a classes so that I could serialize it.
I seem to be getting it to show sometimes but its showing it as a property and not a tab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using UnityEngine.AI;
using System.Security.AccessControl;
using UnityEditor;
[RequireComponent(typeof(Animation))]
public class LinkAction : Editor {
//private Animations animations = new Animations();
private Vector2 inputAxis;
private Rigidbody2D rb2d;
[System.Serializable]
public class Modifiers {
public float MoveSpeed = 4;
public float AnimSpeed = 4;
}
public Modifiers modifiers = new Modifiers();
}
// don't need all of the namespaces
using UnityEngine;
[RequireComponent(typeof(Animation))]
public class LinkAction : MonoBehaviour // derive from MonoBehaviour, not Editor
{
//private Animations animations = new Animations();
private Vector2 inputAxis;
private Rigidbody2D rb2d;
[System.Serializable]
public class Modifiers
{
public float MoveSpeed = 4;
public float AnimSpeed = 4;
}
public Modifiers modifiers = new Modifiers();
}
thankyou. what was I doing wrong?
At that point, only deriving from “Editor” instead of “MonoBehaviour” really, although all of the extra namespaces were getting in the way too. Editor scripts are special and require all sorts of fumbling and/or studying to use them properly, so it’s best to just stick with MonoBehaviours for now. If you need custom editors, be prepared to spend some time familiarizing yourself with what they are and how they work before even attempting to make one- you’ll save yourself a few headaches.
ok thankyou