示例#1
0
    public void InjectInto(Object obj, MethodInfo method)
    {
        GameObject fromObj;

        if (string.IsNullOrEmpty(buttonPath))
        {
            var component = obj as Component;
            if (component != null)
            {
                fromObj = component.gameObject;
            }
            else
            {
                Debug.LogError("fromObject empty for field " + method.Name + ", and no default gameObject could be found!");
                return;
            }
        }
        else
        {
            fromObj = GameObject.Find(buttonPath);

            if (fromObj == null)
            {
                fromObj = UnityBinder.FindInActiveObjectByName(buttonPath);

                if (fromObj == null)
                {
                    Debug.LogError("Could not find GameObject with name " + buttonPath + " for field " + method.Name);

                    return;
                }
            }
        }

        var button = fromObj.GetComponent <Button>();

        if (button == null)
        {
            Debug.LogError("No Button Component found on GameObject @ " + buttonPath);
            return;
        }

        button.onClick.AddListener(delegate { method.Invoke(obj, new object[0]); });
    }
示例#2
0
    public override void InjectInto(Object obj, FieldInfo field)
    {
        var injectArrayType = field.FieldType;

        bool isList = false;
        Type injectType;

        if (injectArrayType.IsArray)
        {
            injectType = injectArrayType.GetElementType();
        }
        else if (injectArrayType.IsGenericType && injectArrayType.GetGenericTypeDefinition() == typeof(List <>))
        {
            injectType = injectArrayType.GetGenericArguments()[0];
            isList     = true;
        }
        else
        {
            Debug.LogError("Could not find suitable type " + injectArrayType + " for field " + field.Name + ". Field must either be an Array or a List");
            return;
        }

        var unityCall = typeof(GameObject).GetMethod("GetComponentsInChildren", new Type[0]);

        if (unityCall == null)
        {
            Debug.LogError("Could not find method GetComponents !!");
            return;
        }

        GameObject fromObj;

        if (string.IsNullOrEmpty(fromObject))
        {
            var component = obj as Component;
            if (component != null)
            {
                fromObj = component.gameObject;
            }
            else
            {
                Debug.LogError("fromObject empty for field " + field.Name + ", and no default gameObject could be found!");
                return;
            }
        }
        else
        {
            fromObj = GameObject.Find(fromObject);

            if (fromObj == null)
            {
                fromObj = UnityBinder.FindInActiveObjectByName(fromObject);

                if (fromObj == null)
                {
                    Debug.LogError("Could not find GameObject with name " + fromObject + " for field " + field.Name);

                    return;
                }
            }
        }

        if (injectType == typeof(GameObject) && !string.IsNullOrEmpty(fromObject))
        {
            field.SetValue(obj, fromObj);
            return;
        }


        var genericMethod = unityCall.MakeGenericMethod(injectType);
        var rawResult     = genericMethod.Invoke(fromObj, null);

        if (rawResult == null)
        {
            Debug.LogError("Could not find component of type " + injectType + " for field " + field.Name);
        }
        else if (rawResult is object[])
        {
            var result = rawResult as object[];

            if (!isList)
            {
                field.SetValue(obj, result);
            }
            else
            {
                MethodInfo convertMethod = typeof(BindComponentsInChildren).GetMethod("ConvertArray",
                                                                                      BindingFlags.NonPublic | BindingFlags.Static);
                if (convertMethod != null)
                {
                    MethodInfo generic = convertMethod.MakeGenericMethod(new[] { injectType });

                    field.SetValue(obj, generic.Invoke(null, new object[] { rawResult }));
                }
                else
                {
                    Debug.LogError("Fatel Error! Cannot make generic method call to BindComponentsInChildren.ConvertArray");
                }
            }
        }
    }
示例#3
0
    public override void InjectInto(Object obj, FieldInfo field)
    {
        var injectType = field.FieldType;

        var unityCall = typeof(GameObject).GetMethod("GetComponents", new Type[0]);

        if (unityCall == null)
        {
            Debug.LogError("Could not find method GetComponents !!");
            return;
        }

        GameObject fromObj;

        if (string.IsNullOrEmpty(fromObject))
        {
            var component = obj as Component;
            if (component != null)
            {
                fromObj = component.gameObject;
            }
            else
            {
                Debug.LogError("fromObject empty for field " + field.Name + ", and no default gameObject could be found!");
                return;
            }
        }
        else
        {
            fromObj = GameObject.Find(fromObject);

            if (fromObj == null)
            {
                fromObj = UnityBinder.FindInActiveObjectByName(fromObject);

                if (fromObj == null)
                {
                    Debug.LogError("Could not find GameObject with name " + fromObject + " for field " + field.Name);

                    return;
                }
            }
        }

        if (injectType == typeof(GameObject) && !string.IsNullOrEmpty(fromObject))
        {
            field.SetValue(obj, fromObj);
            return;
        }


        var genericMethod = unityCall.MakeGenericMethod(injectType);
        var rawResult     = genericMethod.Invoke(fromObj, null);

        if (rawResult == null)
        {
            Debug.LogError("Could not find component of type " + injectType + " for field " + field.Name);
        }
        else if (rawResult is object[])
        {
            var result = rawResult as object[];

            if (result.Length > 0)
            {
                if (index >= result.Length)
                {
                    Debug.LogError("Could not find component of type " + injectType + " for field " + field.Name + " at index " + index);
                }
                else
                {
                    var found = result[index];

                    field.SetValue(obj, found);
                }
            }
            else
            {
                Debug.LogError("Could not find component of type " + injectType + " for field " + field.Name);
            }
        }
    }
    public override void InjectInto(Object obj, FieldInfo field)
    {
        var injectType = field.FieldType;

        var unityCall = typeof(GameObject).GetMethod("GetComponents", new Type[0]);

        if (unityCall == null)
        {
            Debug.LogError("Could not find method GetComponents !!");
            return;
        }

        GameObject fromObj;

        if (string.IsNullOrEmpty(fromObject))
        {
            var component = obj as Component;
            if (component != null)
            {
                fromObj = component.gameObject;
            }
            else if (warnOnly)
            {
                Debug.LogWarning("fromObject empty for field " + field.Name + ", and no default gameObject could be found!");
                return;
            }
            else
            {
                Debug.LogError("fromObject empty for field " + field.Name + ", and no default gameObject could be found!");
                return;
            }
        }
        else
        {
            fromObj = GameObject.Find(fromObject);

            if (fromObj == null)
            {
                fromObj = UnityBinder.FindInActiveObjectByName(fromObject);

                if (fromObj == null)
                {
                    if (warnOnly)
                    {
                        Debug.LogWarning("Could not find GameObject with name " + fromObject + " for field " + field.Name);
                    }
                    else
                    {
                        Debug.LogError("Could not find GameObject with name " + fromObject + " for field " + field.Name);
                    }

                    return;
                }
            }
        }

        if (!injectType.IsGenericList())
        {
            return;
        }

        var elementType = injectType.ListOfWhat();

        var listType            = typeof(List <>);
        var constructedListType = listType.MakeGenericType(elementType);

        var resultedList = (IList)Activator.CreateInstance(constructedListType);

        var genericMethod = unityCall.MakeGenericMethod(elementType);
        var rawResult     = genericMethod.Invoke(fromObj, null);

        if (rawResult == null)
        {
            if (warnOnly)
            {
                Debug.LogWarning("Could not find component of type " + elementType + " for field " + field.Name);
            }
            else
            {
                Debug.LogError("Could not find component of type " + elementType + " for field " + field.Name);
            }
        }
        else if (rawResult is object[])
        {
            var result = rawResult as object[];

            if (result.Length == 0)
            {
                if (warnOnly)
                {
                    Debug.LogWarning("Could not find component of type " + elementType + " for field " + field.Name);
                }
                else
                {
                    Debug.LogError("Could not find component of type " + elementType + " for field " + field.Name);
                }
            }

            //If above is true, then this is never executed
            for (int i = 0; i < result.Length; i++)
            {
                resultedList.Add(result[i]);
            }
        }

        field.SetValue(obj, resultedList);
    }