Inheritance: PropertyAttribute
示例#1
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        methodAttr = attribute as InspectorMethodAttribute;
        obj = property.serializedObject.targetObject;
        MethodInfo method = obj.GetType().GetMethod(methodAttr.methodName, methodAttr.flags);

        if (method == null) // Requested method DOESN'T exist?
        {
            EditorGUI.HelpBox(position, "Method Name Not Found", MessageType.Error);
        }
        else
        {// Method Found
            if (methodAttr.useValue)
            {
                valueRect = new Rect(position.x, position.y, position.width / 2f, position.height);
                buttonRect = new Rect(position.x + position.width / 2f, position.y, position.width / 2f, position.height);

                EditorGUI.PropertyField(valueRect, property, GUIContent.none);
                if (GUI.Button(buttonRect, methodAttr.buttonName))
                {
                    method.Invoke(obj, new object[] { fieldInfo.GetValue(obj) });
                }

            }
            else
            {
                if (GUI.Button(position, methodAttr.buttonName))
                {
                    method.Invoke(obj, null);
                }
            }
        }//End of Method Found

    }//End of OnGUI(Rect position, SerializedProperty property, GUIContent label)
    private void OnDrawInspectorButtons(MethodInfo methodInfo)
    {
        InspectorMethodAttribute attribute = (InspectorMethodAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(InspectorMethodAttribute));

        if (attribute == null)
        {
            return;
        }

        if (methodInfo.GetParameters().Length != 0)
        {
            return;
        }

        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.LabelField(ObjectNames.NicifyVariableName(methodInfo.Name));

        if (GUILayout.Button("Invoke"))
        {
            foreach (var t in targets)
            {
                methodInfo.Invoke(t, null);
            }
        }

        EditorGUILayout.EndHorizontal();
    }
    private void OnDrawInspectorElements <T>(MethodInfo methodInfo, Func <T, T> func)
    {
        InspectorMethodAttribute attribute = (InspectorMethodAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(InspectorMethodAttribute));

        if (attribute == null)
        {
            return;
        }

        if (methodInfo.GetParameters().Length == 0 || methodInfo.GetParameters().Length > 1)
        {
            return;
        }

        if (methodInfo.GetParameters()[0].ParameterType != typeof(T))
        {
            return;
        }

        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.LabelField(ObjectNames.NicifyVariableName(methodInfo.Name));

        if (!m_parameterDatas.ContainsKey(methodInfo))
        {
            m_parameterDatas.Add(methodInfo, null);
        }

        ParameterInfo[] parameterInfos = methodInfo.GetParameters();

        object[] parameterDatas = m_parameterDatas[methodInfo];

        if (parameterDatas == null || parameterDatas.Length != parameterInfos.Length)
        {
            parameterDatas = new object[parameterInfos.Length];
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                parameterDatas[i] = default(T);
            }

            m_parameterDatas[methodInfo] = parameterDatas;
        }

        for (int i = 0; i < parameterInfos.Length; i++)
        {
            m_parameterDatas[methodInfo][i] = func((T)m_parameterDatas[methodInfo][i]);
        }

        if (GUILayout.Button("Invoke"))
        {
            foreach (var t in targets)
            {
                methodInfo.Invoke(t, m_parameterDatas[methodInfo]);
            }
        }

        EditorGUILayout.EndHorizontal();
    }