コード例 #1
0
            public static object RenderObjectMemebers(object obj, Type objType, ref bool dataChanged, GUIStyle style = null, params GUILayoutOption[] options)
            {
                SerializedObjectMemberInfo[] serializedFields = SerializedObjectMemberInfo.GetSerializedFields(objType);

                for (int i = 0; i < serializedFields.Length; i++)
                {
                    if (!serializedFields[i].HideInEditor())
                    {
                        //Create GUIContent for label and optional tooltip
                        string           fieldName       = StringUtils.FromCamelCase(serializedFields[i].GetID());
                        TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(serializedFields[i]);
                        GUIContent       labelContent    = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName);

                        bool   fieldChanged    = false;
                        object nodeFieldObject = serializedFields[i].GetValue(obj);

                        if (serializedFields[i].GetFieldType().IsArray)
                        {
                            nodeFieldObject = ArrayField(labelContent, nodeFieldObject as Array, serializedFields[i].GetFieldType().GetElementType(), ref fieldChanged, style, options);
                        }
                        else
                        {
                            nodeFieldObject = ObjectField(nodeFieldObject, nodeFieldObject != null ? nodeFieldObject.GetType() : serializedFields[i].GetFieldType(), labelContent, ref fieldChanged, style, options);
                        }

                        if (fieldChanged)
                        {
                            dataChanged = true;
                            serializedFields[i].SetValue(obj, nodeFieldObject);
                        }
                    }
                }

                return(obj);
            }
コード例 #2
0
        protected static string GetPropertyInfo(System.Type type)
        {
            string markdown = "";

            foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                TooltipAttribute attribute = (TooltipAttribute)Attribute.GetCustomAttribute(field, typeof(TooltipAttribute));
                if (attribute == null)
                {
                    continue;
                }

                // Change field name to how it's displayed in the inspector
                string propertyName = Regex.Replace(field.Name, "(\\B[A-Z])", " $1");
                if (propertyName.Length > 1)
                {
                    propertyName = propertyName.Substring(0, 1).ToUpper() + propertyName.Substring(1);
                }
                else
                {
                    propertyName = propertyName.ToUpper();
                }

                markdown += propertyName + " | " + field.FieldType + " | " + attribute.tooltip + "\n";
            }

            if (markdown.Length > 0)
            {
                markdown = "\nProperty | Type | Description\n --- | --- | ---\n" + markdown + "\n";
            }

            return(markdown);
        }
コード例 #3
0
 public ConfigElement(PropertyFieldWrapper memberInfo, object item, IList array)
 {
     Width.Set(0f, 1f);
     Height.Set(30f, 0f);
     this.memberInfo           = memberInfo;
     this.item                 = item;
     this.array                = array;
     this.backgroundColor      = UICommon.defaultUIBlue;
     this._TextDisplayFunction = () => memberInfo.Name;
     labelAttribute            = ConfigManager.GetCustomAttribute <LabelAttribute>(memberInfo, item, array);
     if (labelAttribute != null)
     {
         this._TextDisplayFunction = () => labelAttribute.Label;
     }
     tooltipAttribute = ConfigManager.GetCustomAttribute <TooltipAttribute>(memberInfo, item, array);
     if (tooltipAttribute != null)
     {
         this._TooltipFunction = () => tooltipAttribute.Tooltip;
     }
     backgroundColorAttribute = ConfigManager.GetCustomAttribute <BackgroundColorAttribute>(memberInfo, item, array);
     if (backgroundColorAttribute != null)
     {
         backgroundColor = backgroundColorAttribute.color;
     }
     rangeAttribute     = ConfigManager.GetCustomAttribute <RangeAttribute>(memberInfo, item, array);
     incrementAttribute = ConfigManager.GetCustomAttribute <IncrementAttribute>(memberInfo, item, array);
 }
コード例 #4
0
    public static string getTooltipText <T>(string field)
    {
        FieldInfo        fieldInfo = typeof(T).GetField(field);
        TooltipAttribute toolTip   = fieldInfo != null?GetTooltip(fieldInfo, true) : null;

        return(toolTip != null ? toolTip.tooltip : "");
    }
コード例 #5
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ItemInfo"/> class.
            /// </summary>
            /// <param name="info">The reflection information.</param>
            /// <param name="attributes">The attributes.</param>
            public ItemInfo(MemberInfo info, object[] attributes)
            {
                Info              = info;
                Order             = (EditorOrderAttribute)attributes.FirstOrDefault(x => x is EditorOrderAttribute);
                Display           = (EditorDisplayAttribute)attributes.FirstOrDefault(x => x is EditorDisplayAttribute);
                Tooltip           = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
                CustomEditor      = (CustomEditorAttribute)attributes.FirstOrDefault(x => x is CustomEditorAttribute);
                CustomEditorAlias = (CustomEditorAliasAttribute)attributes.FirstOrDefault(x => x is CustomEditorAliasAttribute);
                Space             = (SpaceAttribute)attributes.FirstOrDefault(x => x is SpaceAttribute);
                Header            = (HeaderAttribute)attributes.FirstOrDefault(x => x is HeaderAttribute);
                VisibleIf         = (VisibleIfAttribute)attributes.FirstOrDefault(x => x is VisibleIfAttribute);
                IsReadOnly        = attributes.FirstOrDefault(x => x is ReadOnlyAttribute) != null;
                ExpandGroups      = attributes.FirstOrDefault(x => x is ExpandGroupsAttribute) != null;

                if (Display?.Name != null)
                {
                    // Use name provided by the attribute
                    DisplayName = Display.Name;
                }
                else
                {
                    // Use filtered member name
                    DisplayName = CustomEditorsUtil.GetPropertyNameUI(info.Name);
                }
            }
コード例 #6
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ItemInfo"/> class.
            /// </summary>
            /// <param name="info">The reflection information.</param>
            /// <param name="attributes">The attributes.</param>
            public ItemInfo(MemberInfo info, object[] attributes)
            {
                Info              = info;
                Order             = (EditorOrderAttribute)attributes.FirstOrDefault(x => x is EditorOrderAttribute);
                Display           = (EditorDisplayAttribute)attributes.FirstOrDefault(x => x is EditorDisplayAttribute);
                Tooltip           = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
                CustomEditor      = (CustomEditorAttribute)attributes.FirstOrDefault(x => x is CustomEditorAttribute);
                CustomEditorAlias = (CustomEditorAliasAttribute)attributes.FirstOrDefault(x => x is CustomEditorAliasAttribute);
                Space             = (SpaceAttribute)attributes.FirstOrDefault(x => x is SpaceAttribute);
                Header            = (HeaderAttribute)attributes.FirstOrDefault(x => x is HeaderAttribute);
                VisibleIf         = (VisibleIfAttribute)attributes.FirstOrDefault(x => x is VisibleIfAttribute);
                IsReadOnly        = attributes.FirstOrDefault(x => x is ReadOnlyAttribute) != null;
                ExpandGroups      = attributes.FirstOrDefault(x => x is ExpandGroupsAttribute) != null;

                if (!IsReadOnly && info is FieldInfo fieldInfo && fieldInfo.IsInitOnly)
                {
                    // Field declared with `readonly` keyword
                    IsReadOnly = true;
                }
                if (!IsReadOnly && info is PropertyInfo propertyInfo && !propertyInfo.CanWrite)
                {
                    // Property without a setter
                    IsReadOnly = true;
                }
                if (Display?.Name != null)
                {
                    // Use name provided by the attribute
                    DisplayName = Display.Name;
                }
                else
                {
                    // Use filtered member name
                    DisplayName = CustomEditorsUtil.GetPropertyNameUI(info.Name);
                }
            }
コード例 #7
0
        public static GUIContent BuildGUIContent <T>(string fieldName, string displayOverride = null)
        {
            string           displayName      = (displayOverride != null ? displayOverride : ObjectNames.NicifyVariableName(fieldName));
            FieldInfo        fieldInfo        = typeof(T).GetField(fieldName);
            TooltipAttribute tooltipAttribute = GetTooltipAttribute(fieldInfo);

            return(tooltipAttribute == null ? new GUIContent(displayName) : new GUIContent(displayName, tooltipAttribute.tooltip));
        }
コード例 #8
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            TooltipAttribute tooltip = PropertyUtility.GetAttribute <TooltipAttribute>(property);

            label.tooltip = tooltip?.tooltip;

            EditorGUI.PropertyField(position, property.FindPropertyRelative("_length"), label);
        }
コード例 #9
0
    void Init(SerializedProperty prop)
    {
        type = prop.propertyType;
        var tooltip = fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), false);

        if (tooltip.GetLength(0) > 0)
        {
            Tooltip = (TooltipAttribute)tooltip[0];
        }
    }
コード例 #10
0
        public static string getFieldName(this FieldInfo fi)
        {
            TooltipAttribute t = fi.GetCustomAttribute <TooltipAttribute>();

            if (t == null)
            {
                return(fi.Name);
            }
            return(t.tooltip);
        }
コード例 #11
0
        public FunctionCompletionData(DndFunction function)
        {
            Function = function;
            Text     = function.Name;
            TooltipAttribute tooltipAttribute = function.GetType().GetCustomAttribute <TooltipAttribute>();

            if (tooltipAttribute != null)
            {
                ToolTip = tooltipAttribute.DisplayText;
            }
        }
コード例 #12
0
        public static string GetTooltip(this SerializedProperty property)
        {
            TooltipAttribute tooltipAttribute = property.GetAttribute <TooltipAttribute>();

            if (tooltipAttribute != null)
            {
                return(tooltipAttribute.tooltip);
            }

            return("");
        }
コード例 #13
0
        public static string GetTooltip(Type type, IEnumerable <object> attributes)
        {
            string           text      = Labels.GetTypeTooltip(type);
            TooltipAttribute attribute = CustomAttributeHelpers.GetAttribute <TooltipAttribute>(attributes);

            if (attribute != null)
            {
                text = text + Environment.get_NewLine() + attribute.get_Text();
            }
            return(text);
        }
コード例 #14
0
 public static string GetTooltip(object[] attributes)
 {
     object[] objArray = attributes;
     for (int i = 0; i < (int)objArray.Length; i++)
     {
         TooltipAttribute tooltipAttribute = objArray[i] as TooltipAttribute;
         if (tooltipAttribute != null)
         {
             return(tooltipAttribute.Text);
         }
     }
     return(string.Empty);
 }
コード例 #15
0
 public static string GetTooltip(object[] attributes)
 {
     for (int i = 0; i < attributes.Length; i++)
     {
         object           obj = attributes[i];
         TooltipAttribute tooltipAttribute = obj as TooltipAttribute;
         if (tooltipAttribute != null)
         {
             return(tooltipAttribute.get_Text());
         }
     }
     return(string.Empty);
 }
コード例 #16
0
 /// <summary>
 /// Draws a tooltip as text in the editor
 /// </summary>
 /// <param name="member"></param>
 public static void DrawToolTip(MemberInfo member)
 {
     if (member.IsDefined(typeof(TooltipAttribute), true))
     {
         TooltipAttribute tooltip   = member.GetCustomAttributes(typeof(TooltipAttribute), true)[0] as TooltipAttribute;
         Color            prevColor = GUI.color;
         GUI.color = helpBoxColor;
         EditorGUI.indentLevel--;
         EditorGUILayout.LabelField(tooltip.tooltip, toolTipStyle);
         EditorGUI.indentLevel++;
         GUI.color = prevColor;
     }
 }
コード例 #17
0
 public GraphParameterData(GraphParameter parameter, string name, bool isPublic, Type type, Attribute[] attributes, object tag)
 {
     Parameter   = parameter;
     IsPublic    = isPublic;
     Type        = type;
     Tag         = tag;
     Attributes  = attributes;
     Order       = (EditorOrderAttribute)Attributes.FirstOrDefault(x => x is EditorOrderAttribute);
     Display     = (EditorDisplayAttribute)Attributes.FirstOrDefault(x => x is EditorDisplayAttribute);
     Tooltip     = (TooltipAttribute)Attributes.FirstOrDefault(x => x is TooltipAttribute);
     Space       = (SpaceAttribute)Attributes.FirstOrDefault(x => x is SpaceAttribute);
     Header      = (HeaderAttribute)Attributes.FirstOrDefault(x => x is HeaderAttribute);
     DisplayName = Display?.Name ?? name ?? parameter.Name;
 }
コード例 #18
0
        /// <summary>
        /// Retrieve GUI Content objects with nicified names for enumeration elements in the supplied type
        /// </summary>
        /// <param name="enumType">The <see cref="System.Enum"/> type that is to be processed</param>
        /// <param name="customReplacements">[Optional] A callback function that can be used to provide additional processing to the label text that is to be returned (E.g. replacing characters)</param>
        /// <returns>Returns a GUIContent object for each entry in the numeration type in the order they are defined, or null if not an enumeration type</returns>
        /// <remarks>
        /// Names are nicified through the application of <see cref="UnityEditor.ObjectNames.NicifyVariableName(string)"/>
        /// </remarks>
        public static GUIContent[] CreateEnumContent(Type enumType, Func <string, string> additionalProcessing = null)
        {
            // If the type isn't an enumeration, nothing we can do
            if (!enumType.IsEnum)
            {
                Debug.LogErrorFormat("Unable to get the nicified enumeration names for the type '{0}'. Type is not an enumeration", enumType);
                return(null);
            }

            // If there is no processing callback, just use what is supplied
            if (additionalProcessing == null)
            {
                additionalProcessing = x => x;
            }

            // Get the entrys within the enumeration type
            string[]     labels   = Enum.GetNames(enumType);
            GUIContent[] contents = new GUIContent[labels.Length];
            for (int i = 0; i < contents.Length; ++i)
            {
                // Look for a tooltip to be assigned to the label
                string       tooltip     = string.Empty;
                MemberInfo[] memberInfos = enumType.GetMember(labels[i]);
                if (memberInfos.Length > 0)
                {
                    // Look for the enum member entry for this array
                    MemberInfo enumEntry = Array.Find(memberInfos, x => x.DeclaringType == enumType);
                    if (enumEntry != null)
                    {
                        // Look for a tooltip associated with this member value
                        TooltipAttribute attribute = enumEntry.GetCustomAttribute <TooltipAttribute>(false);
                        if (attribute != null)
                        {
                            tooltip = attribute.tooltip;
                        }
                    }
                }

                // Get the display name for this entry
                labels[i] = additionalProcessing(
                    ObjectNames.NicifyVariableName(labels[i])
                    );

                // Create the content object
                contents[i] = new GUIContent(labels[i], tooltip);
            }
            return(contents);
        }
コード例 #19
0
            internal FieldRecord(FieldInfo info, GameObject defaultsGo)
            {
                this.info = info;

                if (info.ReflectedType == typeof(MonoBehaviour))
                {
                    MonoBehaviour obj = defaultsGo.AddComponent(info.ReflectedType) as MonoBehaviour;
                    defaultValue = info.GetValue(obj);
                    this.script  = MonoScript.FromMonoBehaviour(obj);
                }
                else
                {
                    ScriptableObject instance = ScriptableObject.CreateInstance(info.ReflectedType);
                    this.defaultValue = info.GetValue(instance);
                    this.script       = MonoScript.FromScriptableObject(instance);
                    ScriptableObject.DestroyImmediate(instance);
                }

                object[] attributes = info.GetCustomAttributes(true);
                foreach (object attr in attributes)
                {
                    if (attr is SerializeField)
                    {
                        // we don't need to record that it is serialized as we are only processing serialized fields.
                        continue;
                    }

                    if (attr is TooltipAttribute)
                    {
                        tooltip = attr as TooltipAttribute;
                        continue;
                    }

                    if (attr is RangeAttribute)
                    {
                        range = attr as RangeAttribute;
                        continue;
                    }

                    if (attr is DocGenAttribute)
                    {
                        docGenAttr = attr as DocGenAttribute;
                        continue;
                    }

                    Debug.LogWarning("Unable to document attribute type " + attr.GetType());
                }
            }
コード例 #20
0
            public static string GetFieldTooltip(FieldInfo field, bool inherit = true)
            {
                if (null == field)
                {
                    return(default(string));
                }

                TooltipAttribute attribute = GetFieldFirstAttribute <TooltipAttribute>(field, inherit);

                if (null == attribute)
                {
                    return(default(string));
                }

                return(attribute.tooltip);
            }
コード例 #21
0
        private void DrawInvokableMethods(string category)
        {
            foreach (var m in invokableMethods)
            {
                var attr = m.GetCustomAttribute <InspectorMethodAttribute>();
                if (attr.category == category)
                {
                    TooltipAttribute ta      = m.GetCustomAttribute <TooltipAttribute>(true);
                    string           tooltip = ta != null ? ta.tooltip : string.Empty;
                    if (string.IsNullOrEmpty(tooltip))
                    {
                        var lta = m.GetCustomAttribute <LokiTooltipAttribute>(true);
                        tooltip = lta != null ? lta.tooltip : string.Empty;
                    }

                    string buttonName = string.IsNullOrEmpty(attr.aliasName) ? m.Name : attr.aliasName;
                    if (GUILayout.Button(new GUIContent(buttonName, tooltip), GUILayout.Width(attr.width)))
                    {
                        if (attr.allowMultipleTargets)
                        {
                            foreach (var t in targets)
                            {
                                if (m.IsStatic)
                                {
                                    m.Invoke(null, null);
                                }
                                else
                                {
                                    m.Invoke(t, null);
                                }
                            }
                        }
                        else
                        {
                            if (m.IsStatic)
                            {
                                m.Invoke(null, null);
                            }
                            else
                            {
                                m.Invoke(target, null);
                            }
                        }
                    }
                }
            }
        }
コード例 #22
0
    public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    {
        // get serialized property
        SerializedProperty valueStringProp = property.FindPropertyRelative("serializedValue");

        // get tooltip
        string           tooltipText = null;
        TooltipAttribute tooltip     = GetTooltip(fieldInfo, true);

        if (tooltip != null)
        {
            tooltipText = tooltip.tooltip;
        }

        // cache rect width
        float rectWidth = rect.width;

        // show label as int field for scroll capabilities
        rect.width = EditorGUIUtility.labelWidth + 2;

        EditorGUI.BeginChangeCheck();
        long scrollVal = EditorGUI.LongField(rect, new GUIContent(property.displayName, tooltipText), 0);

        if (EditorGUI.EndChangeCheck())
        {
            InfiniteNumber nb = new InfiniteNumber(valueStringProp.stringValue);
            nb += scrollVal;

            valueStringProp.stringValue = nb.ToString();
        }

        rect.x    += rect.width;
        rect.width = rectWidth - rect.width;

        // show text area
        EditorGUI.BeginProperty(rect, label, property);
        {
            EditorGUI.BeginChangeCheck();
            string newVal = EditorGUI.DelayedTextField(rect, new GUIContent(""), InfiniteNumber.FormatString(valueStringProp.stringValue),
                                                       EditorStyles.numberField);
            if (EditorGUI.EndChangeCheck())
            {
                valueStringProp.stringValue = newVal;
            }
        }
        EditorGUI.EndProperty();
    }
コード例 #23
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ItemInfo"/> class.
            /// </summary>
            /// <param name="info">The reflection information.</param>
            /// <param name="attributes">The attributes.</param>
            public ItemInfo(ScriptMemberInfo info, object[] attributes)
            {
                Info              = info;
                Order             = (EditorOrderAttribute)attributes.FirstOrDefault(x => x is EditorOrderAttribute);
                Display           = (EditorDisplayAttribute)attributes.FirstOrDefault(x => x is EditorDisplayAttribute);
                Tooltip           = (TooltipAttribute)attributes.FirstOrDefault(x => x is TooltipAttribute);
                CustomEditor      = (CustomEditorAttribute)attributes.FirstOrDefault(x => x is CustomEditorAttribute);
                CustomEditorAlias = (CustomEditorAliasAttribute)attributes.FirstOrDefault(x => x is CustomEditorAliasAttribute);
                Space             = (SpaceAttribute)attributes.FirstOrDefault(x => x is SpaceAttribute);
                Header            = (HeaderAttribute)attributes.FirstOrDefault(x => x is HeaderAttribute);
                VisibleIf         = (VisibleIfAttribute)attributes.FirstOrDefault(x => x is VisibleIfAttribute);
                IsReadOnly        = attributes.FirstOrDefault(x => x is ReadOnlyAttribute) != null;
                ExpandGroups      = attributes.FirstOrDefault(x => x is ExpandGroupsAttribute) != null;

                IsReadOnly |= !info.HasSet;
                DisplayName = Display?.Name ?? CustomEditorsUtil.GetPropertyNameUI(info.Name);
            }
コード例 #24
0
        // TODO Make this be in a generic class
        /// <summary>
        /// Gets the specified by the reference object's TooltipAttribute.
        /// </summary>
        /// <returns>The tooltip to display when the label is moused over.</returns>
        /// <param name="info">
        /// Field info. This comes with PropertyDrawer classes, but we have it as a parameter to make
        /// this function more flexible.
        /// </param>
        private string GetTooltip(System.Reflection.FieldInfo info)
        {
            // Partial credit to https://answers.unity.com/answers/1421384/view.html

            string tooltip = "";

            object[] attributes = info.GetCustomAttributes(typeof(TooltipAttribute), true);

            if (attributes.Length > 0)
            {
                TooltipAttribute tt = attributes[0] as TooltipAttribute;
                if (tt != null)
                {
                    tooltip = tt.tooltip;
                }
            }

            return(tooltip);
        } // End GetTooltip
コード例 #25
0
        /// <summary>
        /// Get the tooltip text for the specified field
        /// </summary>
        /// <param name="rFieldName"></param>
        /// <returns></returns>
        protected string GetTooltip(string rFieldName)
        {
            FieldInfo lFieldInfo = typeof(T).GetField(rFieldName);

            if (lFieldInfo == null)
            {
                return(string.Empty);
            }

            TooltipAttribute[] lAttributes = lFieldInfo.GetCustomAttributes(
                typeof(TooltipAttribute), true) as TooltipAttribute[];
            if (lAttributes == null)
            {
                return(string.Empty);
            }

            TooltipAttribute lToolTip = lAttributes.Length > 0 ? lAttributes[0] : null;

            return((lToolTip != null) ? lToolTip.tooltip : string.Empty);
        }
コード例 #26
0
        /// <summary>
        /// Initialise the property target.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="property">The property reflection object.</param>
        public void SetTarget(object target, PropertyInfo property)
        {
            INotifyPropertyChanged notify;

            if (_target != null)
            {
                notify = target as INotifyPropertyChanged;
                if (notify != null)
                {
                    notify.PropertyChanged -= OnPropertyChanged;
                }
                OnReleaseTarget();
            }

            _target   = target;
            _property = property;

            notify = target as INotifyPropertyChanged;
            if (notify != null)
            {
                notify.PropertyChanged += OnPropertyChanged;
            }

            if (_property != null)
            {
                if (NameField != null)
                {
                    NameField.text = _property.Name;
                }
                if (ToolTipDisplay != null)
                {
                    TooltipAttribute tta = PropertyEditors.FindAttribute <TooltipAttribute>(_property);
                    if (tta != null)
                    {
                        ToolTipDisplay.ToolTip = tta.Tooltip;
                    }
                }
            }

            OnSetTarget();
        }
コード例 #27
0
ファイル: ConfigElement.cs プロジェクト: hamstar0/tModLoader
 public virtual void OnBind()
 {
     TextDisplayFunction = () => memberInfo.Name;
     labelAttribute      = ConfigManager.GetCustomAttribute <LabelAttribute>(memberInfo, item, list);
     if (labelAttribute != null)
     {
         TextDisplayFunction = () => labelAttribute.Label;
     }
     tooltipAttribute = ConfigManager.GetCustomAttribute <TooltipAttribute>(memberInfo, item, list);
     if (tooltipAttribute != null)
     {
         this.TooltipFunction = () => tooltipAttribute.Tooltip;
     }
     backgroundColorAttribute = ConfigManager.GetCustomAttribute <BackgroundColorAttribute>(memberInfo, item, list);
     if (backgroundColorAttribute != null)
     {
         backgroundColor = backgroundColorAttribute.color;
     }
     rangeAttribute     = ConfigManager.GetCustomAttribute <RangeAttribute>(memberInfo, item, list);
     incrementAttribute = ConfigManager.GetCustomAttribute <IncrementAttribute>(memberInfo, item, list);
 }
コード例 #28
0
        public static void HandleGUI(object obj, FieldInfo objInfo, ConfigNode configNode, Rect placementBase, ref Rect placement)
        {
            var objfields = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).Where(
                field => Attribute.IsDefined(field, typeof(ConfigItem)));

            foreach (FieldInfo field in objfields)
            {
                bool isNode      = ConfigHelper.IsNode(field, configNode);
                bool isValueNode = ConfigHelper.IsValueNode(field);


                if (isNode || isValueNode)
                {
                    placement.y += spacingOffset;
                    bool isOptional = Attribute.IsDefined(field, typeof(Optional));

                    ConfigNode node    = configNode.GetNode(field.Name);
                    GUIStyle   gsRight = new GUIStyle(GUI.skin.label);
                    gsRight.alignment = TextAnchor.MiddleCenter;


                    Rect     boxRect = GUIHelper.GetRect(placementBase, ref placement, node, field.FieldType, field);
                    GUIStyle gs      = new GUIStyle(GUI.skin.textField);
                    GUI.Box(boxRect, "", gs);
                    placement.height = 1;
                    placement.y     += spacingOffset;

                    Rect boxPlacementBase = new Rect(placementBase);
                    boxPlacementBase.x += 10;
                    Rect boxPlacement = new Rect(placement);
                    boxPlacement.width -= 20;

                    Rect toggleRect = GUIHelper.GetRect(boxPlacementBase, ref boxPlacement);
                    Rect titleRect  = GUIHelper.GetRect(boxPlacementBase, ref boxPlacement);
                    Rect fieldRect  = GUIHelper.GetRect(placementBase, ref boxPlacement);
                    if (isValueNode)
                    {
                        GUIHelper.SplitRect(ref titleRect, ref fieldRect, valueRatio);
                    }
                    GUIHelper.SplitRect(ref toggleRect, ref titleRect, (1f / 16));

                    String tooltipText = "";
                    if (Attribute.IsDefined(field, typeof(TooltipAttribute)))
                    {
                        TooltipAttribute tt = (TooltipAttribute)Attribute.GetCustomAttribute(field, typeof(TooltipAttribute));
                        tooltipText = tt.tooltip;
                    }
                    GUIStyle   style = new GUIStyle(GUI.skin.label);
                    GUIContent gc    = new GUIContent(field.Name, tooltipText);

                    Vector2 labelSize = style.CalcSize(gc);
                    titleRect.width = Mathf.Min(labelSize.x, titleRect.width);
                    GUI.Label(titleRect, gc);

                    bool removeable    = node == null ? false : true;
                    bool conditionsMet = ConfigHelper.ConditionsMet(field, objInfo, configNode);
                    if (conditionsMet)
                    {
                        if (isOptional || isValueNode)
                        {
                            String value        = null;
                            String defaultValue = ConfigHelper.GetConfigValue(obj, field);
                            String valueField   = "";
                            if (isValueNode)
                            {
                                valueField = field.FieldType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).First(
                                    f => Attribute.IsDefined(f, typeof(NodeValue))).Name;
                            }
                            String newValue = "";
                            if (isValueNode)
                            {
                                if (configNode.HasValue(field.Name))
                                {
                                    value = configNode.GetValue(field.Name);
                                }
                                else if (node != null && node.HasValue(valueField))
                                {
                                    value = node.GetValue(valueField);
                                }

                                if (value == null)
                                {
                                    if (defaultValue == null)
                                    {
                                        defaultValue = "";
                                    }
                                    value = defaultValue;
                                }

                                GUIStyle fieldStyle = new GUIStyle(GUI.skin.textField);
                                if (value != "" && !ConfigHelper.CanParse(field, value, node))
                                {
                                    fieldStyle.normal.textColor  = Color.red;
                                    fieldStyle.active.textColor  = Color.red;
                                    fieldStyle.focused.textColor = Color.red;
                                    fieldStyle.hover.textColor   = Color.red;
                                }
                                newValue = GUI.TextField(fieldRect, value, fieldStyle);
                            }
                            bool toggle = removeable != GUI.Toggle(toggleRect, removeable, "");
                            if (toggle)
                            {
                                if (removeable)
                                {
                                    configNode.RemoveNode(field.Name);
                                    node = null;
                                }
                                else
                                {
                                    node = configNode.AddNode(new ConfigNode(field.Name));
                                    if (configNode.HasValue(field.Name))
                                    {
                                        configNode.RemoveValue(field.Name);
                                    }
                                }
                            }

                            if (isValueNode)
                            {
                                if ((newValue != defaultValue && value != newValue) || toggle)
                                {
                                    if (newValue != defaultValue)
                                    {
                                        if (node != null)
                                        {
                                            node.SetValue(valueField, newValue, true);
                                        }
                                        else
                                        {
                                            configNode.SetValue(field.Name, newValue, true);
                                        }
                                    }
                                    if (newValue == defaultValue)
                                    {
                                        if (node != null)
                                        {
                                            if (node.HasValue(valueField))
                                            {
                                                node.RemoveValue(valueField);
                                            }
                                        }
                                        else
                                        {
                                            if (configNode.HasValue(field.Name))
                                            {
                                                configNode.RemoveValue(field.Name);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (node == null)
                        {
                            node = configNode.AddNode(new ConfigNode(field.Name));
                        }
                        boxPlacement.y += 1f;
                        if (node != null)
                        {
                            object subObj = field.GetValue(obj);
                            if (subObj == null)
                            {
                                ConstructorInfo ctor = field.FieldType.GetConstructor(System.Type.EmptyTypes);
                                subObj = ctor.Invoke(null);
                            }

                            HandleGUI(subObj, field, node, boxPlacementBase, ref boxPlacement);
                        }
                        boxPlacement.y += spacingOffset;

                        placement.y = boxPlacement.y;
                        placement.x = boxPlacement.x;
                    }
                    else
                    {
                        if (configNode.HasNode(field.Name))
                        {
                            configNode.RemoveNode(field.Name);
                        }
                        if (configNode.HasValue(field.Name))
                        {
                            configNode.RemoveValue(field.Name);
                        }
                    }
                }
                else
                {
                    if (ConfigHelper.ConditionsMet(field, objInfo, configNode))
                    {
                        GUIHelper.DrawField(placementBase, ref placement, obj, field, configNode);
                    }
                    else if (configNode.HasValue(field.Name))
                    {
                        configNode.RemoveValue(field.Name);
                    }
                }
            }
        }
コード例 #29
0
        public static void DrawField(Rect placementBase, ref Rect placement, object obj, FieldInfo field, ConfigNode config)
        {
            if (!Attribute.IsDefined(field, typeof(GUIHidden)))
            {
                placement.y     += spacingOffset;
                placement.height = 1;
                String value        = config.GetValue(field.Name);
                String defaultValue = ConfigHelper.GetConfigValue(obj, field);
                if (value == null)
                {
                    if (defaultValue == null)
                    {
                        defaultValue = "";
                    }
                    value = defaultValue;
                }

                Rect labelRect = GUIHelper.GetRect(placementBase, ref placement);
                Rect fieldRect = GUIHelper.GetRect(placementBase, ref placement);
                GUIHelper.SplitRect(ref labelRect, ref fieldRect, valueRatio);
                String tooltipText = "";
                if (Attribute.IsDefined(field, typeof(TooltipAttribute)))
                {
                    TooltipAttribute tt = (TooltipAttribute)Attribute.GetCustomAttribute(field, typeof(TooltipAttribute));
                    tooltipText = tt.tooltip;
                }
                GUIStyle   style = new GUIStyle(GUI.skin.label);
                GUIContent gc    = new GUIContent(field.Name, tooltipText);

                Vector2 labelSize = style.CalcSize(gc);
                labelRect.width = Mathf.Min(labelSize.x, labelRect.width);
                GUI.Label(labelRect, gc);

                string newValue = value;
                if (field.FieldType.IsEnum)
                {
                    newValue = ComboBox(fieldRect, value, field.FieldType.GetFields().Where(
                                            m => (m.IsLiteral) && !Attribute.IsDefined(m, typeof(EnumMask))).Select(m => m.Name).ToArray());
                }
                else
                {
                    GUIStyle fieldStyle = new GUIStyle(GUI.skin.textField);
                    if (value != "" && !ConfigHelper.CanParse(field, value))
                    {
                        fieldStyle.normal.textColor  = Color.red;
                        fieldStyle.active.textColor  = Color.red;
                        fieldStyle.focused.textColor = Color.red;
                        fieldStyle.hover.textColor   = Color.red;
                    }
                    newValue = GUI.TextField(fieldRect, value, fieldStyle);
                }

                if (newValue != defaultValue && value != newValue)
                {
                    config.SetValue(field.Name, newValue, true);
                }
                else if (newValue == defaultValue && config.HasValue(field.Name))
                {
                    config.RemoveValue(field.Name);
                }
                placement.y += 1f;
            }
        }
コード例 #30
0
        /// <summary>
        /// Draws a <see cref="ManagedField{T}"/>. Optionally also allow scene objects to be selected.
        /// </summary>
        public void DrawGUI(Rect position, SerializedProperty property, GUIContent label, bool allowSceneObjects)
        {
            object[] tooltipAttributeArray = fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), true);

            if (tooltipAttributeArray.Length > 0)
            {
                TooltipAttribute tooltipAttribute = (TooltipAttribute)tooltipAttributeArray[0];
                label.tooltip = tooltipAttribute.tooltip;
            }

            Type baseType = fieldInfo.FieldType.IsArray ? fieldInfo.FieldType.GetElementType() : fieldInfo.FieldType;
            Assert.IsNotNull(baseType);

            Type managedType = baseType.GenericTypeArguments[0];
            string suffix = $"* {managedType.FullName}";
            string tooltip = string.IsNullOrEmpty(label.tooltip) ? suffix : $"{label.tooltip}{Environment.NewLine}{suffix}";
            GUIContent labelWithTooltip = new GUIContent($"{label.text}*", label.image, tooltip);
            SerializedProperty systemObject = property.FindPropertyRelative(SystemObjectSerializedProperty);
            SerializedProperty unityObject = property.FindPropertyRelative(UnityObjectSerializedProperty);

            using EditorGUI.PropertyScope propertyScope = new EditorGUI.PropertyScope(position, labelWithTooltip, unityObject);

            position.height = EditorGUI.GetPropertyHeight(unityObject, true);

            if (typeof(UnityEngine.Object).IsAssignableFrom(baseType.GenericTypeArguments[0]))
            {
                DrawUnityField(position, managedType, systemObject, unityObject, propertyScope.content, allowSceneObjects, true);
            }
            else if (unityObject.objectReferenceValue != null)
            {
                float buttonWidth = EditorStyles.miniPullDown.CalcSize(ClearLabel).x;
                Rect unityFieldPosition = position;
                unityFieldPosition.xMax -= buttonWidth + EditorGUIUtility.standardVerticalSpacing;
                DrawUnityField(unityFieldPosition, managedType, systemObject, unityObject, propertyScope.content, allowSceneObjects, false);

                Rect buttonPosition = position;
                buttonPosition.xMin = unityFieldPosition.xMax + EditorGUIUtility.standardVerticalSpacing;

                if (GUI.Button(buttonPosition, ClearLabel, EditorStyles.miniButton))
                {
                    systemObject.serializedObject.Update();
                    unityObject.objectReferenceValue = null;
                    systemObject.serializedObject.ApplyModifiedProperties();
                }
            }
            else
            {
                string typename = systemObject.managedReferenceFullTypename;

                if (string.IsNullOrWhiteSpace(typename))
                {
                    if (managedType.IsInterface)
                    {
                        float dropdownWidth = EditorStyles.miniPullDown.CalcSize(NewLabel).x;
                        Rect unityFieldPosition = position;
                        unityFieldPosition.xMax -= dropdownWidth + EditorGUIUtility.standardVerticalSpacing;
                        DrawUnityField(unityFieldPosition, managedType, systemObject, unityObject, propertyScope.content, allowSceneObjects, false);

                        Rect systemDropdownPosition = position;
                        systemDropdownPosition.xMin = unityFieldPosition.xMax + EditorGUIUtility.standardVerticalSpacing;
                        DrawSystemDropdown(systemDropdownPosition, managedType, systemObject);
                    }
                    else
                    {
                        Rect valuePosition = position;
                        valuePosition.height = EditorGUI.GetPropertyHeight(systemObject, true);
                        EditorGUI.PropertyField(valuePosition, systemObject, propertyScope.content, true);

                        Rect systemDropdownPosition = position;
                        systemDropdownPosition.x += EditorGUIUtility.labelWidth;
                        systemDropdownPosition.width -= EditorGUIUtility.labelWidth;
                        DrawSystemDropdown(systemDropdownPosition, managedType, systemObject);
                    }
                }
                else
                {
                    float buttonWidth = EditorStyles.miniButton.CalcSize(ClearLabel).x;
                    string separator = typename.Contains(".") ? "." : " ";
                    GUIContent value = new GUIContent(typename.Substring(typename.LastIndexOf(separator, StringComparison.Ordinal) + 1));
                    Rect labelPosition = position;
                    labelPosition.xMax -= buttonWidth + EditorGUIUtility.standardVerticalSpacing;
                    EditorGUI.LabelField(labelPosition, EmptyLabel, value);

                    Rect buttonPosition = position;
                    buttonPosition.xMin = labelPosition.xMax + EditorGUIUtility.standardVerticalSpacing;

                    if (GUI.Button(buttonPosition, ClearLabel, EditorStyles.miniButton))
                    {
                        systemObject.serializedObject.Update();
                        systemObject.managedReferenceValue = null;
                        systemObject.serializedObject.ApplyModifiedProperties();
                    }

                    Rect valuePosition = position;
                    valuePosition.height = EditorGUI.GetPropertyHeight(systemObject, true);
                    EditorGUI.PropertyField(valuePosition, systemObject, propertyScope.content, true);
                }
            }
        }