Home > Back-end >  How to implement a static nested class that can be modified in unity inspector?
How to implement a static nested class that can be modified in unity inspector?

Time:10-26

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.

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

CodePudding user response:

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.

CodePudding user response:

I ended up using a singleton which worked second time round. For this I added in the parent class:

public static Parameters myInstance {get; private set;}

and for each inner class

public GroupA groupA = new GroupA();

And I removed all other static references.

  • Related