Interface reference available in editor

The problem I have is that if I define an interface

public interface IMyInterface {
	void MyMethod();
}

and define public references to objects implementing that interface in another class

public class MyClass : MonoBehaviour {
	public int i;
	public IMyInterface myInterface;
	public IMyInterface[] myInterfaceArray;
}

then those fields (myInterface and myInterfaceArray) do not appear in editor (int i does, of course) :?

I could of course have references to Transform and then cast, but that’s a crappy solution…

Here are unity assets for this, if you’d like to try it out quickly:

126417–4740–$interfacetestunitypackage_597.zip (4.78 KB)

Perhaps this will help

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.

1 Like

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;    
      } 
   } 

}

this is way to use interfaces for your problem

I see your point, but there’s still something missing, since I can’t see the fields of MyBehaviour in the editor…

public class MyBehaviour : MonoBehaviour { 

   public IMyClass m_myClass; 
   public IMyClass myClass { 
      get{ 
	if(m_myClass!=null) return m_myClass;

	// code to find / Instantiate object
         return m_myClass; 
      } 
      set { 
         m_myClass=value;    
      } 
   }

    void Start() {
	myClass= myClass;	
    }

 

}

[/code]

I’d like to share my solution: (SOLUTION) How to serialize interfaces, generics, auto-properties and abstract System.Object classes? - Questions & Answers - Unity Discussions

Checkout ShowEmAll (free): Serialize interfaces, generics, auto-properties, dictionaries, abstracts, etc.

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?

There’s also IUnified: [RELEASED] IUnified - C# Interfaces for Unity! - Community Showcases - Unity Discussions

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.