C# exposing derived class in inspector

Hi there,

I’m having trouble exposing derived classes in the inspector.

I have a BaseClass

using UnityEngine;
using System.Collections;

//expose in inspector
[System.Serializable]

//no inherit
public class BaseClass {
	
	//fields
	public string m_name;
	public string m_desc;
	
	//constructor
	public BaseClass () {
		m_name = "empty name";
		m_desc = "empty desc";	
	}
	
	//properties
	public string Name {
		get{return m_name;}	
		set{m_name = value;}
	}
	public string Desc {
		get{return m_desc;}
		set{m_desc = value;}
	}
}

And a DerivedClass

using UnityEngine;
using System.Collections;

//expose in inspector
[System.Serializable]

//inhertis from BaseClass
public class DerivedClass : BaseClass {
	
	//fields
	public int m_viscosity;
	public int m_temp;
	
	//constructor
	public DerivedClass () {
		m_viscosity = -1;
		m_temp = -1;	
	}
	
	//properties
	public int Viscosity {
		get{return m_viscosity;}	
		set{m_viscosity = value;}
	}
	public int Temp {
		get{return m_temp;}
		set{m_temp = value;}
	}
}

I add three instances of DerivedClass to a list called MyList

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyList : MonoBehaviour {
	
	// My List
	public List<BaseClass> myList = new List<BaseClass>();
	// Base Class
	public BaseClass newBaseClass = new BaseClass();
	// Derived Class
	public DerivedClass newDerivedClass = new DerivedClass();

	// Use this for initialization
	void Start () {
		PopulateList();
	}
	
	// Add three empty derived classes to the list
	public void PopulateList(){
		for(int i = 0; i < 3; i++){
			myList.Add (new DerivedClass());	
		}
	}	
}

I attached MyList to a game object.

When I run the game and check the contents of MyList in the inspector, it only shows a BaseClass instance for each list element, that is I see a ‘Name’ and ‘Desc’ in each list entry. I know that the DerivedClass is there too, because I can access its fields via code, it just doesn’t show in the inspector.

Does anyone have any idea how I’d go about showing DerivedClass so I can see ‘Name’, ‘Desc’, ‘Viscosity’ and ‘Temp’ in each list element in the inspector?

Thanks,
Pli

P.S. just to note, this is for learning purposes so that I can see what’s going on in the list. I would make the m_ fields private in reality which is why they have the prefix.

P.P.S is it normal to keep finding a mountain twice the size of the one you just climbed when learning a language for the first time? :stuck_out_tongue:

Yeah, you must either specify your derived class as generic param for your list. You can also try to write a custom PropertyDrawer which can overcome this limitation.

Excellent, thanks for the help Marrrk. I think having separate lists for every item type is going to make sorting a bit awkward later. I’m going to give it a go and try to write a custom PropertyDrawer. Thanks for pointing out they exist, I wouldn’t have found that in a million years.

All the best,
Pli

A PropertyDrawer for the base class will probably work, or you could implement a custom Editor for the component type, and leave the lists private. Your custom Editor can chain to its base class to render the standard Inspector view, then you can emit whatever GUI calls you like afterwards to render your debug data below it.