I know the question may be a bit vague but basically all I’m trying to achieve is for some public variables to be shown in the inspector. To give some context, I have an AI base class (removed unnecessary code) here:
AI Base Class
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour
{
protected StateMachine SM;
protected virtual void Start()
{
// Setup state machine with state idle as default state
SM = new StateMachine(this, typeof(Idle));
}
protected virtual void Update()
{
// Acts out the current state update behavior
SM.UpdateState();
}
}
As you can see it contains an instance of the custom class StateMachine. It’s simplified version is as follows:
State Machine Class
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
[System.Serializable]
public class StateMachine
{
// Reference to the owning AI
public AI OwningAI { get; private set;}
// A stack of states currently in the state machine
public Stack<State> StateStack { get; private set;}
// Default(initial) state of this state machine (Initialized in constructor)
private State DefaultState;
// Constructor
public StateMachine(AI DwarfObj, Type StateType)
{
OwningAI = DwarfObj;
StateStack = new Stack<State>();
// Initialization of the default state
if(StateType.BaseType == typeof(State))
DefaultState = (State)Activator.CreateInstance(StateType, this);
else
Debug.LogException(new Exception("Parameter 'StateType' must be of type 'State'"), OwningAI);
// Adding the default state to the state stack
PushState(DefaultState);
}
// Adds a new state to the top of the stack & calls the enter state method
public void PushState(State StateObj)
{
StateStack.Push(StateObj);
StateStack.Peek().EnterState();
}
// Calls the update method for the state at the top of the stack
public void UpdateState()
{
StateStack.Peek().UpdateState();
}
// Removes the state at the top of the stack & calls the exit state method
// Note: Will not pop first (default) state
public void PopState()
{
if (StateStack.Count > 1)
StateStack.Pop().ExitState();
else
Debug.LogWarning("Attempted to pop default state", OwningAI);
}
}
Take note of the use of the [System.Serializable] attribute and the property:
public Stack<State> StateStack { get; private set;}
Almost there. Here we have the abstract base class State in its entirety:
State Class
using UnityEngine;
using System.Collections;
[System.Serializable]
public abstract class State
{
// State machine reference
protected StateMachine SM { get; private set; }
public State(StateMachine StateMachineObj)
{
SM = StateMachineObj;
}
// Called at the beginning of the state
public abstract void EnterState();
// Called every tick during the state
public abstract void UpdateState();
// Called at the end of the state
public abstract void ExitState();
}
For the sake of example, here’s the Idle state:
Idle State Example
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Idle : State
{
public Idle(StateMachine StateMachineObj) : base(StateMachineObj) { }
public int ShowMeInTheInspector;
public string MeTooPlease;
public override void EnterState()
{
Debug.Log("Idle Entry");
}
public override void UpdateState()
{
Debug.Log("Idle Update");
}
public override void ExitState()
{
Debug.Log("Idle Exit");
}
}
Question
Okay so this is exactly what I want to achieve; when selecting an object that inherits from class AI, I want the inspector to display all public variables in its Current State. Its current state is the state that’s on the top of the State Stack in its State Machine instance. Which from the example would be the ShowMeInTheInspector float and the MeTooPlease string. However these variables should only be shown if the current state is Idle as different states have their own variables. What would be the best way to go about doing this?
Keep in mind that I am using multiple custom classes, child classes, stacks and properties.