public interface IMyClass {
void MyMethod();
}
[System.Serializable]
public class MyClass : System.Object, IMyClass {
public void MyMethod(){
// do stuff;
}
}
public class MyBehaviour : MonoBehaviour {
public MyClass m_myClass;
public IMyClass myClass {
get{
return m_myClass as IMyClass;
}
set {
m_myClass=value as MyClass;
}
}
}
Hmm, thanks for your answer, but it doesn’t really solve my problem. In your implementation I’m only able to assign instances of class MyClass, whereas I want to be able to assign instances of any class implementing the interface.
In the meantime I came up with a solution which works, but doesn’t satisfy me entirely: use of abstract classes.
public abstract class IMyAbstractClass : MonoBehaviour {
public abstract void MyMethod();
}
public class MyImplementation : IMyAbstractClass {
public override void MyMethod(){
//do smth
}
}
public class MyTestClass : MonoBehaviour {
//this is visible in the editor:
public IMyAbstractClass bla;
public IMyAbstractClass[] blaArray;
}
Now I can assign instance of any class implementing IMyAbstractClass to the fields of MyTestClass in the editor: for example I can assign instances of MyImplementation there.
The downside of that solution, is that a class can only inherit after one parent, whereas I could be implementing multiple interfaces, if the editor would only allow me to see the fields later on.
If you were to forego the luxury of Serialization
you could
public interface IMyClass {
void MyMethod();
int id {get;set;}
}
public class MyClass : IMyClass { // TODO : MyClass<T>
public void MyMethod(){
// do stuff;
}
public int m_id=-1;
public int id {
get{
if(m_id!=-1)return m_id;
m_id = MyClass.id_GET(this);
}
set { m_id =value;}
}
// GET
public static int id_GET(IMyClass mc){
if(mc is MyClassSO) return 1;
if(mc is MyClassBehaviour) return 2;
}
//SET
}
[System.Serializable]
public class MyClassSO : System.Object, IMyClass {
#region IMyClass implimentation
public void MyMethod(){
// do stuff;
}
public int m_id=-1;
public int id {
get{
if(m_id!=-1)return m_id;
m_id = MyClass.id_GET(this);
}
set { m_id =value;}
}
#endregion
}
public class MyClassBehaviour : MonoBehaviour, IMyClass {
#region IMyClass implimentation
public void MyMethod(){
// do stuff;
}
public int m_id=-1;
public int id {
get{
if(m_id!=-1)return m_id;
m_id = MyClass.id_GET(this);
}
set { m_id =value;}
}
#endregion
}
public class MyBehaviour : MonoBehaviour {
public IMyClass m_myClass;
public IMyClass myClass {
get{
return m_myClass;
}
set {
m_myClass=value;
}
}
}
I’m wondering… How are you flagged “Unity Technologies” and don’t know that Unity is unable to serialize interface or base class that does not implement MonoBehaviour/ScriptableObject?
It’s a paid asset, 5 bucks.
It’s a minor annoyance having to implement the container class for each interface (once per interface, not per implementor and it’s just a class declaration), but other than that it works well for me.
So I’ve done drastic improvements and changes to ShowEmAll. I haven’t updated the thread yet there’s still stuff I’d like to add. Here’s an overview of the serialization features you’ll eventually have. Pretty much most if not all problems are solved. Polymorphic serialization, generics, structs, properties, etc.