Esempio n. 1
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);
                    }
                }
            }
        }
    }
Esempio n. 2
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);
                }
            }
        }
    }