Suppose I have implemented a core module/package (interfaces, factories, services, etc.) and several concrete modules (e.g. Video Ads, IAPs, etc.)
I would like to be able to add/ remove the modules in my project easily.
How can I solve the dependencies and references?
Concrete modules require the core module to implement. Also, inside the core module, there are factory classes. the factory classes require concrete classes to create an instance based on the type argument. Should I use preprocessor to resolve compile time error inside each concrete module?
// Package Module1
public class Module1:IModule{
}
//...
//-------------------------------------
// Package Module2
public class Module2:IModule{
}
//...
//-------------------------------------
//Package Core
public interface IModule{
}
public class ModuleFactory{
public IModule Create(ModuleType type){
//switch/case
#if ADMOB
#endif
//...
}
}
Whatever you do, keep it firmly in mind that EVERYTHING in a Unity scene or prefab is connected together only by the GUID associated with a given class, which is stored in the meta file for the C# file (or in the case of partial classes, the one matching the name of the class).
Do all the crazy inheritance and abstraction and factories that you want, but it will not change this fundamental fact about how code is connected to GameObjects. Ignore it at your own peril.
Some info about Missing script warnings, GUIDs, renaming GUIDs, etc:
EVERYTHING in Unity is connected to the above GUID. It is super-easy to inadvertently change it by renaming outside of Unity. Don’t do that. Instead:
close Visual Studio (important!)
rename the file(s) in Unity
in Unity do Assets → Open C# Project to reopen Visual Studio
now rename the actual classes, and MAKE SURE THE FILE NAMES DO NOT CHANGE!
If you are NOT using source control while you do this, renaming files is an EXTREMELY dangerous process. Use source control at all times so that you can trivially revert if you miss a critical step and damage your project.