static bool GetComponenets(MonoBehaviour behaviour, object customAttribute, Type elementType, out object components)
    {
        if (customAttribute is GetComponentInWorldAttribute)
        {
            components = GameObject.FindObjectsOfType(elementType);
        }
        else if (customAttribute is GetComponentAttribute)
        {
            var getter = typeof(MonoBehaviour)
                         .GetMethod("GetComponents", new Type[0])
                         .MakeGenericMethod(elementType);
            components = getter.Invoke(behaviour, null);
        }
        else if (customAttribute is GetComponentInChildrenAttribute)
        {
            GetComponentInChildrenAttribute ca = customAttribute as GetComponentInChildrenAttribute;

            var getter = typeof(MonoBehaviour)
                         .GetMethod("GetComponentsInChildren", new Type[] { typeof(bool) })
                         .MakeGenericMethod(elementType);
            components = getter.Invoke(behaviour,
                                       new object[] { ((GetComponentInChildrenAttribute)customAttribute).includeInactive });

            if (ca.gameObjectKey != null)
            {
            }
        }
        else if (customAttribute is GetComponentInParentAttribute)
        {
            var getter = typeof(MonoBehaviour)
                         .GetMethod("GetComponentsInParent", new Type[] { typeof(bool) })
                         .MakeGenericMethod(elementType);
            components = getter.Invoke(behaviour,
                                       new object[] { ((GetComponentInParentAttribute)customAttribute).includeInactive });
        }
        else
        {
            components = null;
            return(false);
        }

        return(true);
    }
Beispiel #2
0
    static private object ProcUpdateComponent_Dictionary(MonoBehaviour pTargetMono, MemberInfo pMember, System.Type pTypeField, GetComponentInChildrenAttribute pAttributeInChildren, Type pType_DictionaryKey, Type pType_DictionaryValue)
    {
        object pComponent   = pAttributeInChildren.GetComponent(pTargetMono, pType_DictionaryValue);
        Array  arrComponent = pComponent as Array;

        if (arrComponent.Length == 0)
        {
            return(null);
        }

        var Method_Add = pTypeField.GetMethod("Add", new[] {
            pType_DictionaryKey, pType_DictionaryValue
        });

        // Reflection의 메소드는 Instance에서만 호출할수 있다.
        var  pInstanceDictionary       = System.Activator.CreateInstance(pTypeField);
        bool bIsDerived_DictionaryItem = false;

        Type[] arrInterfaces = pType_DictionaryValue.GetInterfaces();
        string strTypeName   = typeof(IDictionaryItem <>).Name;

        for (int i = 0; i < arrInterfaces.Length; i++)
        {
            if (arrInterfaces[i].Name.Equals(strTypeName))
            {
                bIsDerived_DictionaryItem = true;
                break;
            }
        }

        if (pType_DictionaryKey == typeof(string))
        {
            for (int k = 0; k < arrComponent.Length; k++)
            {
                Component pComponentChild = arrComponent.GetValue(k) as Component;
                Method_Add.Invoke(pInstanceDictionary, new object[] {
                    pComponentChild.name,
                    pComponentChild
                });
            }
        }
        else
        {
            if (bIsDerived_DictionaryItem)
            {
                var pMethod_GetKey = pType_DictionaryValue.GetMethod("IDictionaryItem_GetKey");
                for (int k = 0; k < arrComponent.Length; k++)
                {
                    Component pComponentChild = arrComponent.GetValue(k) as Component;
                    Method_Add.Invoke(pInstanceDictionary, new object[] {
                        pMethod_GetKey.Invoke(pComponentChild, null),
                        pComponentChild
                    });
                }
            }
            else
            {
                if (pType_DictionaryKey.IsEnum)
                {
                    for (int k = 0; k < arrComponent.Length; k++)
                    {
                        Component pComponentChild = arrComponent.GetValue(k) as Component;
                        try
                        {
                            var pEnum = System.Enum.Parse(pType_DictionaryKey, pComponentChild.name, true);
                            Method_Add.Invoke(pInstanceDictionary, new object[] {
                                pEnum,
                                pComponentChild
                            });
                        }
                        catch { }
                    }
                }
            }

            // Debug.LogError("GetComponentAttribute Error Dictionary Key 타입은 string, enum만 됩니다. Key Type : " + pType_DictionaryKey.Name);
            // return null;
        }

        pMember.SetValue_Extension(pTargetMono, pInstanceDictionary);
        return(pComponent);
    }
Beispiel #3
0
    static private object ProcUpdateComponent_Generic(MonoBehaviour pTargetMono, MemberInfo pMember, GetComponentInChildrenAttribute pGetComponentAttribute, System.Type pTypeField)
    {
        object pComponent = null;

        System.Type pTypeField_Generic = pTypeField.GetGenericTypeDefinition();
        Type[]      arrArgumentsType   = pTypeField.GetGenericArguments();

        if (pTypeField_Generic == typeof(List <>))
        {
            // List의 경우 GetComponentsInChildren 을 통해 Array를 얻은 뒤
            pComponent = pGetComponentAttribute.GetComponent(pTargetMono, arrArgumentsType[0]);
            // List 생성자에 Array를 집어넣는다.
            pMember.SetValue_Extension(pTargetMono, System.Activator.CreateInstance(pTypeField, pComponent));
        }
        else if (pTypeField_Generic == typeof(Dictionary <,>))
        {
            pComponent = ProcUpdateComponent_Dictionary(pTargetMono, pMember, pTypeField, pGetComponentAttribute, arrArgumentsType[0], arrArgumentsType[1]);
        }

        return(pComponent);
    }
Beispiel #4
0
    static private void UpdateComponentAttribute(MonoBehaviour pTargetMono, MemberInfo[] arrMember)
    {
        for (int i = 0; i < arrMember.Length; i++)
        {
            MemberInfo pMemberInfo         = arrMember[i];
            object[]   arrCustomAttributes = pMemberInfo.GetCustomAttributes(true);

            for (int j = 0; j < arrCustomAttributes.Length; j++)
            {
                GetComponentAttributeBase pGetcomponentAttribute = arrCustomAttributes[j] as GetComponentAttributeBase;
                if (pGetcomponentAttribute == null)
                {
                    continue;
                }

                System.Type pTypeField = pMemberInfo.MemberType();
                object      pComponent = null;

                if (pTypeField.IsArray)
                {
                    pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeField.GetElementType());
                }
                else if (pTypeField.IsGenericType)
                {
                    pComponent = ProcUpdateComponent_Generic(pTargetMono, pMemberInfo, (GetComponentInChildrenAttribute)pGetcomponentAttribute, pTypeField);
                }
                else
                {
                    if (pGetcomponentAttribute is GetComponentAttribute)
                    {
                        pComponent = pTargetMono.GetComponent(pTypeField);
                    }
                    else if (pGetcomponentAttribute is GetComponentInChildrenAttribute)
                    {
                        GetComponentInChildrenAttribute pAttributeInChildren = (GetComponentInChildrenAttribute)pGetcomponentAttribute;
                        if (pAttributeInChildren.bSearch_By_ComponentName)
                        {
                            pComponent = pTargetMono.GetComponentInChildren(pAttributeInChildren.strComponentName, pTypeField, pAttributeInChildren.bInclude_DeActive);
                        }
                        else
                        {
                            pComponent = pTargetMono.GetComponentInChildren(pTypeField, pAttributeInChildren.bInclude_DeActive);
                        }
                    }
                    else if (pGetcomponentAttribute is GetComponentInParentAttribute)
                    {
                        pComponent = pTargetMono.GetComponentInParent(pTypeField);
                    }
                }

                if (pComponent == null && pGetcomponentAttribute.bIsPrint_OnNotFound)
                {
                    GetComponentInChildrenAttribute pAttribute = (GetComponentInChildrenAttribute)pGetcomponentAttribute;
                    if (pAttribute != null && pAttribute.bSearch_By_ComponentName)
                    {
                        Debug.LogWarning(pTargetMono.name + string.Format(".{0}<{1}>({2}) Result == null", pGetcomponentAttribute.GetType().Name, pTypeField, pAttribute.strComponentName), pTargetMono);
                    }
                    else
                    {
                        Debug.LogWarning(pTargetMono.name + string.Format(".{0}<{1}> Result == null", pGetcomponentAttribute.GetType().Name, pTypeField), pTargetMono);
                    }
                    continue;
                }

                if (pTypeField.IsGenericType == false)
                {
                    if (pTypeField == typeof(GameObject))
                    {
                        pMemberInfo.SetValue_Extension(pTargetMono, ((Component)pComponent).gameObject);
                    }
                    else
                    {
                        pMemberInfo.SetValue_Extension(pTargetMono, pComponent);
                    }
                }
            }
        }
    }
    static public void DoUpdateGetComponentAttribute(MonoBehaviour pTargetMono, object pMemberOwner, MemberInfo pMemberInfo)
    {
        object[] arrCustomAttributes = pMemberInfo.GetCustomAttributes(true);
        for (int i = 0; i < arrCustomAttributes.Length; i++)
        {
            IGetComponentAttribute pGetcomponentAttribute = arrCustomAttributes[i] as IGetComponentAttribute;
            if (pGetcomponentAttribute == null)
            {
                continue;
            }

            System.Type pTypeMember = pMemberInfo.MemberType();
            object      pComponent  = null;

            if (pTypeMember.IsGenericType)
            {
                pComponent = SetMember_OnGeneric(pGetcomponentAttribute, pTargetMono, pMemberOwner, pMemberInfo, pTypeMember);
            }
            else if (pTypeMember.HasElementType)
            {
                pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeMember.GetElementType());
            }
            else
            {
                pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeMember);
            }

            if (pComponent == null)
            {
                if (pGetcomponentAttribute.bIsPrint_OnNotFound_GetComponent)
                {
                    GetComponentInChildrenAttribute pAttribute = pGetcomponentAttribute as GetComponentInChildrenAttribute;
                    if (pAttribute != null && pAttribute.bSearch_By_ComponentName)
                    {
                        Debug.LogError(pTargetMono.name + string.Format(".{0}<{1}>({2}) Result == null", pGetcomponentAttribute.GetType().Name, pTypeMember, pAttribute.strComponentName), pTargetMono);
                    }
                    else
                    {
                        Debug.LogError(pTargetMono.name + string.Format(".{0}<{1}> Result == null", pGetcomponentAttribute.GetType().Name, pTypeMember), pTargetMono);
                    }
                }

                continue;
            }

            if (pTypeMember.IsGenericType == false)
            {
                if (pTypeMember.HasElementType == false)
                {
                    Array arrComponent = pComponent as Array;
                    if (arrComponent != null && arrComponent.Length != 0)
                    {
                        pMemberInfo.SetValue_Extension(pMemberOwner, arrComponent.GetValue(0));
                    }
                }
                else
                {
                    if (pTypeMember == typeof(GameObject))
                    {
                        pMemberInfo.SetValue_Extension(pMemberOwner, ((Component)pComponent).gameObject);
                    }
                    else
                    {
                        pMemberInfo.SetValue_Extension(pMemberOwner, pComponent);
                    }
                }
            }
        }
    }
Beispiel #6
0
    static private void ProcUpdateComponentAttribute(MonoBehaviour pTargetMono, FieldInfo[] arrFields)
    {
        for (int i = 0; i < arrFields.Length; i++)
        {
            FieldInfo pField = arrFields[i];
            object[]  arrCustomAttributes = pField.GetCustomAttributes(true);

            for (int j = 0; j < arrCustomAttributes.Length; j++)
            {
                GetComponentAttributeBase pGetcomponentAttribute = arrCustomAttributes[j] as GetComponentAttributeBase;
                if (pGetcomponentAttribute == null)
                {
                    continue;
                }

                System.Type pTypeField = pField.FieldType;
                object      pComponent = null;

                if (pTypeField.IsArray)
                {
                    pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeField.GetElementType());
                }
                else if (pTypeField.IsGenericType)
                {
                    pComponent = ProcUpdateComponent_Generic(pTargetMono, pField, (GetComponentInChildrenAttribute)pGetcomponentAttribute, pTypeField);
                }
                else
                {
                    if (pGetcomponentAttribute is GetComponentAttribute)
                    {
                        pComponent = pTargetMono.GetComponent(pTypeField);
                    }
                    else if (pGetcomponentAttribute is GetComponentInChildrenAttribute)
                    {
                        GetComponentInChildrenAttribute pAttributeInChildren = (GetComponentInChildrenAttribute)pGetcomponentAttribute;
                        if (pAttributeInChildren.bSearch_By_ComponentName)
                        {
                            pComponent = pTargetMono.GetComponentInChildren(pAttributeInChildren.strComponentName, pTypeField);
                        }
                        else
                        {
                            pComponent = pTargetMono.GetComponentInChildren(pTypeField, true);
                        }
                    }
                    else if (pGetcomponentAttribute is GetComponentInParentAttribute)
                    {
                        pComponent = pTargetMono.GetComponentInParent(pTypeField);
                    }
                }

                if (pComponent == null && pGetcomponentAttribute.bIsPrint_OnNotFound)
                {
                    GetComponentInChildrenAttribute pAttribute = (GetComponentInChildrenAttribute)pGetcomponentAttribute;
                    if (pAttribute != null && pAttribute.bSearch_By_ComponentName)
                    {
                        Debug.LogWarning(pTargetMono.name + string.Format(".{0}<{1}>({2}) Result == null", pGetcomponentAttribute.GetType().Name, pTypeField, pAttribute.strComponentName), pTargetMono);
                    }
                    else
                    {
                        Debug.LogWarning(pTargetMono.name + string.Format(".{0}<{1}> Result == null", pGetcomponentAttribute.GetType().Name, pTypeField), pTargetMono);
                    }
                    continue;
                }

                if (pTypeField.IsGenericType == false)
                {
                    pField.SetValue(pTargetMono, pComponent);
                }
            }
        }
    }