Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement a static nested class that can be modified in unity inspector?

I’m hoping to implement a static class with nested classes which can be viewed and modified in the Unity Inspector.

Any recommendations on how to achieve this?

I’ve tried using nested static classes successfully, but I can’t add them as a component in order to view in the inspector.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any recommendations on how to achieve this?

Here’s my example code. Without Monobehaviour I can’t add it as a component to get it in the inspector.

using UnityEngine;

public static class Parameters
{
    [System.Serializable]
    public static class GroupA
    {
    public static float varA = 1f;
    }
    
    [System.Serializable]
    public static class GroupB
    {
    public static float varB = 2f;
    }

}

I can then call this with, e.g.

Parameters.GroupA.varA;

Thanks in advance and sorry for my code stinks.

best, Rob

>Solution :

I use "Odin inspector" for such cases.

using UnityEngine;
using Sirenix.OdinInspector;

public static class Parameters
{
[System.Serializable]
public class GroupA
{
public float varA = 1f;
}

[System.Serializable]
public class GroupB
{
public float varB = 2f;
}

[ShowInInspector]
public static GroupA staticGroupA;
[ShowInInspector]
public static GroupB staticGroupB;

void Start()
{
GroupA = new GroupA();
GroupA.varA = 1f;
GroupB = new GroupB(); 
GroupB.varB = 2f;
}

I hope it helped.
Best.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading