public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { Injectable injectable = attribute as Injectable; Object injectedReferenceValue = property.objectReferenceValue; if (property.propertyType == SerializedPropertyType.ObjectReference) { EditorGUI.BeginProperty(position, label, property); var fieldPosition = position; fieldPosition.height = 20; if (injectable.DisplayName != null) { label.text = injectable.DisplayName; } injectedReferenceValue = EditorGUI.ObjectField(fieldPosition, label, property.objectReferenceValue, typeof(Object), true); if (injectedReferenceValue != null && injectedReferenceValue.GetType() == typeof(GameObject)) { GameObject injectedGameObject = (GameObject)injectedReferenceValue; injectedReferenceValue = injectedGameObject.GetComponent(injectable.InjectedType); } else if (!injectable.IsValid(injectedReferenceValue)) { injectedReferenceValue = null; } error = false; } else { injectedReferenceValue = null; // Shows an error message if isn't an Object Reference ShowError("A reference type is needed, " + property.type + " isn't.", position); } if (!error) { if (injectedReferenceValue == null) { ShowInfo("Must implement " + injectable.InjectedType.Name, position); } else { ShowInfo("Injected [" + injectedReferenceValue.ToString() + "]", position, active: true); } } property.objectReferenceValue = injectedReferenceValue; EditorGUI.EndProperty(); }
private void TryRegisterProvider(Injectable injectable) { if (injectable.Type != null && injectable.Type.IsSubclassOf(typeof(Provider)) && injectable.Tag == null) { var key = new InjectionKey(injectable.Type, null); var provider = ProviderFactory.CreateGeneric(injectable.Type); providers.Add(key, provider); } }
public IResolver GetResolverForDependency(Injectable injectable) { var type = injectable.Attribute.GetType(); if (resolvers.ContainsKey(type)) { return(resolvers[type]); } return(null); }
private void TryInject(Injectable injectable, object dependency) { if (!IsValidDependency(injectable, dependency)) { Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find dependency for", injectable)); return; } injectable.Inject(dependency); }
public IProvider GetProviderForDependency(Injectable injectable) { var key = new InjectionKey(injectable.Type, injectable.Tag); if (providers.ContainsKey(key)) { return(providers[key]); } return(null); }
public object Resolve(Injectable injectable) { var attribute = (FindResourceOfTypeAttribute)injectable.Attribute; if (attribute.ComponentType == null) { return(Resources.LoadAll("", injectable.Type).FirstOrDefault()); } //else return(Resources.LoadAll("", attribute.ComponentType).FirstOrDefault()); }
private void EvaluateInjectable(Injectable injectable) { if (injectable.Attribute is UnityConvenienceAttribute) { dependencyMap.RegisterResolvableDependent(injectable); } else if (injectable.Attribute is InjectAttribute) { dependencyMap.RegisterProvidableDependent(injectable); } }
private void TryInjectResolvable(Injectable injectable) { var resolver = dependencyMap.GetResolverForDependency(injectable); if (resolver == null) { Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find resolver for", injectable)); return; } var dependency = resolver.Resolve(injectable); TryInject(injectable, dependency); }
private void EvaluateInjectableProvider(Injectable injectable, IProvider provider) { dependencyMap.RegisterProvider(injectable.Type, injectable.Tag, provider); if (injectable.Attribute is UnityConvenienceAttribute) { dependencyMap.RegisterResolvableDependent(injectable); } else { throw new InjectionException(injectable.Object, "A member cannot be annotated both with [Inject] and [Provides]" + injectable.Type); } }
public object Resolve(Injectable injectable) { var attribute = (FindObjectOfTypeAttribute)injectable.Attribute; if (attribute.ComponentType == null) { return(Object.FindObjectOfType(injectable.Type)); } else { return(Object.FindObjectOfType(attribute.ComponentType)); } }
private void TryInjectProvidable(Injectable injectable) { var provider = dependencyMap.GetProviderForDependency(injectable); if (provider == null) { Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find provider for", injectable)); return; } var dependency = provider.Get(); TryInject(injectable, dependency); }
private void EvaluateAttributes(MemberInfo info, Injectable injectable, IProvider provider) { if (injectable != null && provider != null) { EvaluateInjectableProvider(injectable, provider); } else if (injectable != null) { EvaluateInjectable(injectable); } else if (provider != null) { EvaluateProvider(provider); } }
public object Resolve(Injectable injectable) { var tag = ((FindWithTagAttribute)injectable.Attribute).Tag; var gameObject = GameObject.FindWithTag(tag); if (gameObject == null) { return(null); } if (injectable.Type != typeof(GameObject)) { return(gameObject.GetComponent(injectable.Type)); } return(gameObject); }
public void TestInjectorContainsAllInjectableFields() { var container = new DiContainer(); var injectable = new Injectable("A"); container.Bind <IInjectable>().ToInstance(injectable); var instance = new TestClass(); container.BindInstance(instance); container.TryResolveAll(); Assert.IsNotNull(instance.Injectable); Assert.AreEqual("A", instance.Injectable.Name); }
public object Resolve(Injectable injectable) { var attribute = (FindResourceOfTypeAttribute)injectable.Attribute; if (attribute.ComponentType == null) { //var resources = Resources.FindObjectsOfTypeAll(injectable.Type); var resources = Resources.LoadAll("", injectable.Type); return(resources?[0]); } else { //var resources = Resources.FindObjectsOfTypeAll(attribute.ComponentType); var resources = Resources.LoadAll("", attribute.ComponentType); //.ToArray(); return(resources?[0]); } }
public object Resolve(Injectable injectable) { var name = ((FindAttribute)injectable.Attribute).GameObjectName; var gameObject = GameObject.Find(name); if (gameObject == null) { return(null); } else if (injectable.Type != typeof(GameObject)) { return(gameObject.GetComponent(injectable.Type)); } else { return(gameObject); } }
public object Resolve(Injectable injectable) { var monoBehaviour = injectable.Object as MonoBehaviour; if (monoBehaviour == null) { throw new InjectionException(injectable.Object, "[GetComponent] annotation on a non-MonoBehaviour"); } var attribute = (GetComponentAttribute)injectable.Attribute; if (attribute.ComponentType == null) { return(monoBehaviour.GetComponent(injectable.Type)); } else { return(monoBehaviour.GetComponent(attribute.ComponentType)); } }
public object Resolve(Injectable injectable) { var name = ((FindRelativeAttribute)injectable.Attribute).PathName; var parent = ((MonoBehaviour)injectable.Object).transform; var transform = parent.FindRelativePath(name); if (transform == null) { return(null); } else if (injectable.Type.IsAssignableFrom(transform.GetType())) { return(transform); } else { return(transform.GetComponent(injectable.Type)); } }
public object Resolve(Injectable injectable) { var name = ((AnimatorStringHashAttribute)injectable.Attribute).HashName; return(Animator.StringToHash(name)); }
public void Inject(TTarget target, TInjectableValue value) => Injectable.Inject(target, value);
public InjectableWrapper(Injectable injectable) : base(injectable.Type) { this._injectable = injectable; }
private static bool IsValidDependency(Injectable injectable, object dependency) { return(dependency != null && !dependency.Equals(null) && injectable.Type.IsInstanceOfType(dependency)); }
public void RegisterResolvableDependent(Injectable injectable) { resolvableDependents.Add(injectable); }
public void RegisterProvidableDependent(Injectable injectable) { providableDependents.Add(injectable); TryRegisterProvider(injectable); }
public TReject Reject(TTarget target, TInjectableValue value) => Injectable.Reject(target, value);