Skip to content

Commit

Permalink
Update Injector.cs
Browse files Browse the repository at this point in the history
Attempt to create a descriptive error message for quick resolution of UnityEngine.Component injection problems.
  • Loading branch information
Jo Simard committed May 13, 2016
1 parent b828357 commit 21cab92
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Assets/Adic/Scripts/Framework/Injection/Injector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ protected void InjectFields(object instance, SetterInfo[] fields) {
var field = fields[fieldIndex];
var valueToSet = this.Resolve(field.type, InjectionMember.Field, instance, field.identifier);
field.setter(instance, valueToSet);
#if UNITY_EDITOR
ValidateInjection(instance, valueToSet, field.type);
#endif
}
}

Expand All @@ -340,6 +343,27 @@ protected void InjectProperties(object instance, SetterInfo[] properties) {
var property = properties[propertyIndex];
var valueToSet = this.Resolve(property.type, InjectionMember.Property, instance, property.identifier);
property.setter(instance, valueToSet);
#if UNITY_EDITOR
ValidateInjection(instance, valueToSet, property.type);
#endif
}
}

/// <summary>
/// Verify if injection is problematic and inform user accordingly
/// </summary>
protected void ValidateInjection(object instance, object value, Type valueType)
{
// Instance resolved to 'null' when Components are instanciated, an error will be logged soon after.
if (instance.ToString() == "null") return;

if(value is UnityEngine.Component)
{
UnityEngine.Component component = value as UnityEngine.Component;
if(component == null || component.gameObject==null)
{
UnityEngine.Debug.LogError("Could not inject '" + valueType.Name + "' from '" + instance.GetType() + "'. Please review your bindings values and order.");
}
}
}

Expand Down Expand Up @@ -538,4 +562,4 @@ protected void OnBeforeAddBinding(IBinder source, ref BindingInfo binding) {
}
}
}
}
}

0 comments on commit 21cab92

Please sign in to comment.