private void Inject(object target, object injectionKey) { // For circular dependencies, the object that is currently created for the injectionKey is stored temporarily. // The map is prioritized when resolving dependencies. // Thus, further newly created objects (i.e. dependencies of the result that is constructed here) // that have a dependency to injectionKey (i.e. to the result that is constructed here), // can have the object injected that has already been instantiated here. injectionKeyToObjectWithOngoingInjectionMap.Add(injectionKey, target); // Find all members to be injected via reflection. List <InjectionData> injectionDatas = UniInjectUtils.GetInjectionDatas(target.GetType()); // Inject existing bindings into the fields. foreach (InjectionData injectionData in injectionDatas) { Inject(target, injectionData); } injectionKeyToObjectWithOngoingInjectionMap.Remove(injectionKey); // Notify target that its injection is now finished. if (target is IInjectionFinishedListener) { (target as IInjectionFinishedListener).OnInjectionFinished(); } }
private static int CheckInjectable(MonoBehaviour script, Type type, List <IBinding> bindings) { int errorCount = 0; List <InjectionData> injectionDatas = UniInjectUtils.GetInjectionDatas(type); foreach (InjectionData injectionData in injectionDatas) { if (injectionData.isOptional) { continue; } if (injectionData.searchMethod == SearchMethods.SearchInBindings) { errorCount += CheckInjectableFromBindings(script, type, bindings, injectionData); } else { errorCount += CheckInjectableFromUnitySearchMethod(script, type, injectionData); } } return(errorCount); }
private void AnalyzeScriptsRecursively(GameObject gameObject) { MonoBehaviour[] scripts = gameObject.GetComponents <MonoBehaviour>(); foreach (MonoBehaviour script in scripts) { // The script can be null if it is a missing component. if (script == null) { continue; } // Analyzing a type for InjectionData is costly. // The types of the UnityEngine do not make use of UniInject. // Thus, the scripts from the UnityEngine itself should be skipped for better performance. Type type = script.GetType(); if (!string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith("UnityEngine.")) { continue; } if (script is IBinder) { binders.Add(script as IBinder); } if (script is ISceneInjectionFinishedListener) { sceneInjectionFinishedListeners.Add(script as ISceneInjectionFinishedListener); } if ((!onlyInjectScriptsWithMarkerInterface || script is INeedInjection) && !(script is IExcludeFromSceneInjection)) { List <InjectionData> injectionDatas = UniInjectUtils.GetInjectionDatas(script.GetType()); if (injectionDatas.Count > 0) { scriptsThatNeedInjection.Add(script); } } } foreach (Transform child in gameObject.transform) { AnalyzeScriptsRecursively(child.gameObject); } }