- Home /
Is there a way to show Monobehaviours class properties inside another Monobehaviour class in the Unity inspector?
Hi all. Just as the title says - is there any equivalent to [System.Serializable] on Monobehaviour classes so I could put that into another class and its properties could be still editable in the inspector? Basically what I am trying to do is to be able to create mission objectives in the Unity inspector that may dynamically change during the game and this functionality could be very useful, but I am not sure if it's even possible.
So I have this MissionObjectives class (simplified) which derives from MonoBehaviour. I would like to put this class into another MonoBehaviour class and still be able to edit its serializable classes properties in the Unity inspector. Is it possible?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class MissionObjectives: MonoBehaviour
{
[Serializable]
public class Objective
{
[Serializable]
public class EntityToSpawn
{
public Entity entity_pref;
public Transform spawnPosition;
}
public string objectiveDescription;
public List<EntityToSpawn> entitiesToSpawn = new List<EntityToSpawn>();
public List<Entity> entitiesToDestroy = new List<Entity>();
public void StartObjective()
{
}
public void UpdateObjective()
{
}
}
public List<Objective> objectives = new List<Objective>();
void Start()
{
objectives[0].StartObjective ();
}
}
Answer by jmonasterio · Dec 28, 2015 at 04:18 PM
It's not super clear what you want because you wrote things like: 'put this class into another' and 'class properties inside another'. It's not exactly clear what you mean by this.
I think, though, you may want inheritance like:
public class MissionObjectives: Objectives
{
// All properties/methods of MissionObjectives available in inspector, plus these extra ones defined below.
// In constructor you can set some default values, if you like public MissionObjective() { }
}
and
public class Objective : MonoBehaviour
{
// Your objectives class here.
}
Thanks for your comment. But that's not the answer. Let's say I have a class:
using UnityEngine;
using System.Collections;
using System;
public class $$anonymous$$bClass : $$anonymous$$onoBehaviour
{
[Serializable]
public class AnotherClass
{
public float value1;
public float value2;
}
public AnotherClass anotherClass;
public float valueA;
public float valueB;
}
When I attach $$anonymous$$bClass to a GameObject in Unity, I will be able to set values in the Unity inspector of AnotherClass (which sits inside of $$anonymous$$bClass). I am able to do that thanks so Serializable attribute.
And now, my question is: Is it possible to put $$anonymous$$bClass into yet another class so that I can edit all of the values in the inspector. For example I would create $$anonymous$$bClass2 which will have a list of $$anonymous$$bClass classes, but I want to be able to edit all of the values of $$anonymous$$bClass in Unity Inspector.
In other words is there a [Serializable] attribute equivalent for $$anonymous$$onobehaviour classes?