As per this stackoverflow question. c# - Unity integration with WinRT with interfaces and not plugins - Stack Overflow
Jaime Rodriguez in this mva series (video 3: Native Integration at 53:00) says that there is an alternative to using plugins for accessing platform specific features. That alternative is coding against an interface in unity and implementing that interface in the app. Then unity will connect the interface with the implementation.
Does anybody know how this is done?
I think it means this:
- You create an interface
- Make a single place where the implementor is obtained from
- All you scripts only use this interface
- In your app you write an implementor for this interface and “set” it to this central place your scripts pull implementor from.
Simple example would be:
// somewhere in scripts
public class Interfaces {
public static Dictionary<Type, object> myImplementors;
}
// somewhere else in scripts
#if UNITY_EDITOR
Interfaces.myImplementors[typeof(IMyInterface)] = new MyInterfaceEditorImpl();
#endif
// somewhere in your exported app code
Interfaces.myImplementors[typeof(IMyInterface)] = new MyInterfaceWinRTImpl();
// your script then would do
IMyInterface impl = (IMyInterface) Interfaces.myImplementors[typeof(IMyInterface)];
…use impl…
So if I understand correctly the MyInterfaceEditorImpl class would basically be a stub class which would be substituted at runtime.
I am a beginer at this and i have one more question. Where is the interface declared?
If it is declared in a script then how does the app know about it?
If it is declared in the app then how does unity know about it?
Is it declared in both?
Any class implementing an interface is fine for you, you can have one class for all Mono platforms (including editor) or all desktop platforms and separate classes for mobiles etc. It’s up to you.
You declare interface in a .cs file and put that file into your Unity project, so that your scripts see it.
The exported VS solution references script assemblies, so it sees all your script classes and other types.
Thanks Aurimas Cernius. I tried it and it works.
I believe that for small projects that don’t require great scalability using interfaces is a good solution.