예제 #1
0
        static AnimationCurveDrawer()
        {
            MethodInfo mi   = null;
            var        type = AssemblyUtilities.GetTypeByCachedFullName("UnityEditorInternal.AnimationCurvePreviewCache");

            if (type != null)
            {
                var method = type.GetMethod("ClearCache", Flags.StaticAnyVisibility);
                var pars   = method.GetParameters();
                if (pars != null && pars.Length == 0)
                {
                    mi = method;
                }
            }

            if (mi != null)
            {
                clearCache = EmitUtilities.CreateStaticMethodCaller(mi);
            }
#if SIRENIX_INTERNAL
            else
            {
                Debug.LogError("AnimationCurve fix no longer works, has Unity fixed it?");
            }
#endif
        }
        private void DoInspectorGUI(InspectorProperty property, string config, string methodName)
        {
            var methodConfig = property.Context.Get(this, config, (MethodContext)null);

            if (methodConfig.Value == null)
            {
                methodConfig.Value = new MethodContext();

                MethodInfo methodInfo = property.ParentType
                                        .FindMember()
                                        .IsMethod()
                                        .IsNamed(methodName)
                                        .HasNoParameters()
                                        .ReturnsVoid()
                                        .GetMember <MethodInfo>(out methodConfig.Value.ErrorMessage);

                if (methodConfig.Value.ErrorMessage == null && methodInfo != null)
                {
                    if (methodInfo.IsStatic)
                    {
                        methodConfig.Value.StaticMethod = EmitUtilities.CreateStaticMethodCaller(methodInfo);
                    }
                    else
                    {
                        methodConfig.Value.InstanceMethod = EmitUtilities.CreateWeakInstanceMethodCaller(methodInfo);
                    }
                }
                else
                {
                    string otherErrorMessage;
                    methodInfo = property.ParentType
                                 .FindMember()
                                 .IsMethod()
                                 .IsNamed(methodName)
                                 .HasParameters(property.ValueEntry.BaseValueType)
                                 .IsStatic()
                                 .ReturnsVoid()
                                 .GetMember <MethodInfo>(out otherErrorMessage);

                    if (otherErrorMessage == null)
                    {
                        methodConfig.Value.ErrorMessage = null;
                        var delegateType = typeof(Action <>).MakeGenericType(property.ValueEntry.TypeOfValue);
                        methodConfig.Value.StaticMethodWithParam = Delegate.CreateDelegate(delegateType, methodInfo);
                    }
                    else
                    {
                        methodConfig.Value.ErrorMessage += "\nor\n" + otherErrorMessage;
                    }
                }
            }

            if (methodConfig.Value.ErrorMessage != null)
            {
                SirenixEditorGUI.ErrorMessageBox(methodConfig.Value.ErrorMessage);
            }
            else
            {
                if (methodConfig.Value.InstanceMethod != null)
                {
                    methodConfig.Value.InstanceMethod(property.ParentValues[0]);
                }
                else if (methodConfig.Value.StaticMethodWithParam != null)
                {
                    methodConfig.Value.StaticMethodWithParam.DynamicInvoke(property.ValueEntry.WeakSmartValue);
                }
                else
                {
                    methodConfig.Value.StaticMethod();
                }
            }
        }
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(GUIContent label)
        {
            var entry     = this.ValueEntry;
            var attribute = this.Attribute;

            var context = entry.Property.Context.Get <ButtonContext>(this, "ButtonContext", (ButtonContext)null);

            if (context.Value == null)
            {
                context.Value             = new ButtonContext();
                context.Value.LabelHelper = new StringMemberHelper(this.Property, attribute.Label ?? attribute.MemberMethod.SplitPascalCase(), ref context.Value.ErrorMessage);

                if (context.Value.ErrorMessage == null)
                {
                    MethodInfo method;

                    if (MemberFinder.Start(entry.ParentType)
                        .IsMethod()
                        .IsNamed(attribute.MemberMethod)
                        .HasNoParameters()
                        .TryGetMember <MethodInfo>(out method, out context.Value.ErrorMessage))
                    {
                        if (method.IsStatic())
                        {
                            context.Value.StaticMethodCaller = EmitUtilities.CreateStaticMethodCaller(method);
                        }
                        else
                        {
                            context.Value.InstanceMethodCaller = EmitUtilities.CreateWeakInstanceMethodCaller(method);
                        }
                    }
                    else if (MemberFinder.Start(entry.ParentType)
                             .IsMethod()
                             .IsNamed(attribute.MemberMethod)
                             .HasParameters <T>()
                             .TryGetMember <MethodInfo>(out method, out context.Value.ErrorMessage))
                    {
                        if (method.IsStatic())
                        {
                            context.Value.ErrorMessage = "Static parameterized method is currently not supported.";
                        }
                        else
                        {
                            context.Value.InstanceParameterMethodCaller = EmitUtilities.CreateWeakInstanceMethodCaller <T>(method);
                        }
                    }
                }
            }

            if (context.Value.ErrorMessage != null)
            {
                SirenixEditorGUI.ErrorMessageBox(context.Value.ErrorMessage);
                this.CallNextDrawer(label);
            }
            else
            {
                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.BeginVertical();
                this.CallNextDrawer(label);
                EditorGUILayout.EndVertical();

                if (GUILayout.Button(context.Value.LabelHelper.GetString(entry), EditorStyles.miniButton, GUILayoutOptions.ExpandWidth(false).MinWidth(20)))
                {
                    // Invoke method.
                    if (context.Value.StaticMethodCaller != null)
                    {
                        context.Value.StaticMethodCaller();
                    }
                    //else if(context.Value.StaticParameterMethodCaller != null)
                    //{
                    //	context.Value.StaticParameterMethodCaller(entry.SmartValue);
                    //}
                    else if (context.Value.InstanceMethodCaller != null)
                    {
                        context.Value.InstanceMethodCaller(entry.Property.ParentValues[0]);
                    }
                    else if (context.Value.InstanceParameterMethodCaller != null)
                    {
                        context.Value.InstanceParameterMethodCaller(entry.Property.ParentValues[0], entry.SmartValue);
                    }
                    else
                    {
                        Debug.LogError("No method found.");
                    }
                }

                EditorGUILayout.EndHorizontal();
            }
        }
예제 #4
0
        /// <summary>
        /// Draws the property.
        /// </summary>
        protected override void DrawPropertyLayout(GUIContent label)
        {
            var property  = this.Property;
            var attribute = this.Attribute;

            if (property.Info.PropertyType == PropertyType.Method)
            {
                var methodContext = property.Context.Get(this, "Config", (MethodContext)null);

                if (methodContext.Value == null)
                {
                    methodContext.Value = new MethodContext();

                    MethodInfo methodInfo = property.Info.GetMemberInfo() as MethodInfo;

                    if (methodInfo == null)
                    {
                        methodInfo = property.Info.GetMethodDelegate().Method;
                    }

                    if (methodInfo.ReturnType != typeof(void))
                    {
                        methodContext.Value.ErrorMessage = "The method '" + methodInfo.Name + "' must have a return type of type void.";
                    }
                    else if (methodInfo.GetParameters().Length > 0)
                    {
                        methodContext.Value.ErrorMessage = "The method '" + methodInfo.Name + "' cannot take any parameters.";
                    }
                    else if (methodInfo.IsStatic)
                    {
                        var del = property.Info.GetMethodDelegate();

                        if (del == null)
                        {
                            methodContext.Value.StaticMethod = EmitUtilities.CreateStaticMethodCaller(methodInfo);
                        }
                        else
                        {
                            methodContext.Value.StaticMethod = (Action)del;
                        }
                    }
                    else
                    {
                        var del = property.Info.GetMethodDelegate();

                        if (del == null)
                        {
                            methodContext.Value.InstanceMethod = EmitUtilities.CreateWeakInstanceMethodCaller(methodInfo);
                        }
                        else
                        {
                            methodContext.Value.StaticMethod = () => del.DynamicInvoke();
                        }
                    }
                }

                if (methodContext.Value.ErrorMessage != null)
                {
                    SirenixEditorGUI.ErrorMessageBox(methodContext.Value.ErrorMessage);
                }
                else
                {
                    if (methodContext.Value.InstanceMethod != null)
                    {
                        methodContext.Value.InstanceMethod(property.ParentValues[0]);
                    }
                    else
                    {
                        methodContext.Value.StaticMethod();
                    }
                }
            }
            else
            {
                if (attribute.PrependMethodName != null)
                {
                    this.DoInspectorGUI(property, "Prepend", attribute.PrependMethodName);
                }

                this.CallNextDrawer(label);

                if (attribute.AppendMethodName != null)
                {
                    this.DoInspectorGUI(property, "Append", attribute.AppendMethodName);
                }
            }
        }