private static IService InitializeService(Type targetType, ServiceAttribute attribute) { if (Is(targetType.BaseType, typeof(MonoBehaviour))) { return(InitializeMono(targetType, attribute)); } return((IService)Activator.CreateInstance(targetType)); }
private static IService InitializeMono(Type targetType, ServiceAttribute attribute) { GameObject serviceInstance = null; if (attribute != null) { //find attribute. No attribute == new GameObject + AddComponent if (!string.IsNullOrEmpty(attribute.PrefabPath)) { serviceInstance = attribute.PrefabPath.LoadAndInstantiate(); } else if (attribute.InScene) { var objectOfType = Object.FindObjectOfType(targetType); if (objectOfType != null) { return (IService)objectOfType; } } } if (serviceInstance == null) { serviceInstance = new GameObject(); } serviceInstance.name = "" + targetType; serviceInstance.transform.parent = ApplicationManager.Instance.Component.transform; serviceInstance.transform.position = Vector3.zero; var serviceComponent = serviceInstance.GetComponent(targetType) ?? serviceInstance.AddComponent(targetType); return (IService)serviceComponent; }
private static IService InitializeService(Type targetType, ServiceAttribute attribute) { if (Is(targetType.BaseType, typeof(MonoBehaviour))) { return InitializeMono(targetType, attribute); } return (IService)Activator.CreateInstance(targetType); }