Esempio n. 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!_toShow)
            {
                return;
            }

            if (!CustomDrawerUsed())
            {
                EditorGUI.PropertyField(position, property, label, true);
            }


            bool CustomDrawerUsed()
            {
                if (_customPropertyDrawer == null)
                {
                    return(false);
                }

                try
                {
                    _customPropertyDrawer.OnGUI(position, property, label);
                    return(true);
                }
                catch (Exception e)
                {
                    WarningsPool.LogWarning(property,
                                            "Unable to use CustomDrawer of type " + _customPropertyDrawer.GetType() + ": " + e,
                                            property.serializedObject.targetObject);

                    return(false);
                }
            }
        }
Esempio n. 2
0
        public static SerializedProperty FindRelativeProperty(SerializedProperty property, string propertyName)
        {
            if (property.depth == 0)
            {
                return(property.serializedObject.FindProperty(propertyName));
            }

            var path     = property.propertyPath.Replace(".Array.data[", "[");
            var elements = path.Split('.');

            var nestedProperty = NestedPropertyOrigin(property, elements);

            // if nested property is null = we hit an array property
            if (nestedProperty == null)
            {
                var cleanPath = path.Substring(0, path.IndexOf('['));
                var arrayProp = property.serializedObject.FindProperty(cleanPath);
                var target    = arrayProp.serializedObject.targetObject;

                var who     = "Property <color=brown>" + arrayProp.name + "</color> in object <color=brown>" + target.name + "</color> caused: ";
                var warning = who + "Array fields is not supported by [ConditionalFieldAttribute]";

                WarningsPool.Log(warning, target);

                return(null);
            }

            return(nestedProperty.FindPropertyRelative(propertyName));
        }
Esempio n. 3
0
        /// <summary>
        /// Create PropertyDrawer for specified property if any PropertyDrawerType for such property is found.
        /// FieldInfo and Attribute will be inserted in created drawer.
        /// </summary>
        public static PropertyDrawer GetPropertyDrawerForProperty(SerializedProperty property, FieldInfo fieldInfo, Attribute attribute)
        {
            var propertyId = property.GetUniquePropertyId();

            if (PropertyDrawersCache.TryGetValue(propertyId, out var drawer))
            {
                return(drawer);
            }

            var targetType = fieldInfo.FieldType;
            var drawerType = GetPropertyDrawerTypeForFieldType(targetType);

            if (drawerType != null)
            {
                drawer = InstantiatePropertyDrawer(drawerType, fieldInfo, attribute);

                if (drawer == null)
                {
                    WarningsPool.LogWarning(property,
                                            $"Unable to instantiate CustomDrawer of type {drawerType} for {fieldInfo.FieldType}",
                                            property.serializedObject.targetObject);
                }
            }

            PropertyDrawersCache[propertyId] = drawer;
            return(drawer);
        }
Esempio n. 4
0
        private void LogWarning(string log, SerializedProperty property)
        {
            var warning = "Property <color=brown>" + fieldInfo.Name + "</color>";

            if (fieldInfo != null && fieldInfo.DeclaringType != null)
            {
                warning += " on behaviour <color=brown>" + fieldInfo.DeclaringType.Name + "</color>";
            }
            warning += " caused: " + log;

            WarningsPool.Log(warning, property.serializedObject.targetObject);
        }
Esempio n. 5
0
 private static void CheckForUpdates()
 {
     MyEditorEvents.OnEditorStarts -= CheckForUpdates;
     MyBoxUtilities.GetMyBoxLatestVersionAsync(version =>
     {
         _installedVersion = MyBoxUtilities.GetMyBoxInstalledVersion();
         _latestVersion    = version;
         if (!_installedVersion.VersionsMatch(_latestVersion))
         {
             var versions = "Installed version: " + _installedVersion.AsSting + ". Latest version: " + _latestVersion.AsSting;
             var message  = "It's time to update MyBox :)! Use \"Tools/MyBox/Update MyBox\". " + versions;
             WarningsPool.Log(message);
         }
     });
 }
Esempio n. 6
0
 static MyBoxWindow()
 {
     if (AutoUpdateCheckIsEnabled)
     {
         MyBoxUtilities.GetMyBoxLatestVersionAsync(version =>
         {
             _installedVersion = MyBoxUtilities.GetMyBoxInstalledVersion();
             _latestVersion    = version;
             if (!_installedVersion.VersionsMatch(_latestVersion))
             {
                 var versions = "Installed version: " + _installedVersion.AsSting + ". Latest version: " + _latestVersion.AsSting;
                 var message  = "It's time to update MyBox :)! Use \"Tools/MyBox/Update MyBox\". " + versions;
                 WarningsPool.Log(message);
             }
         });
     }
 }
Esempio n. 7
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.isArray)
            {
                WarningsPool.LogCollectionsNotSupportedWarning(property, nameof(OverrideLabelAttribute));
            }

            label.text = ((OverrideLabelAttribute)attribute).NewLabel;

            var customDrawer = CustomDrawerUtility.GetPropertyDrawerForProperty(property, fieldInfo, attribute);

            if (customDrawer != null)
            {
                customDrawer.OnGUI(position, property, label);
            }
            else
            {
                EditorGUI.PropertyField(position, property, label, true);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Get the other Property which is stored alongside with specified Property, by name
        /// </summary>
        private static SerializedProperty FindRelativeProperty(SerializedProperty property, string propertyName)
        {
            if (property.depth == 0)
            {
                return(property.serializedObject.FindProperty(propertyName));
            }

            var path     = property.propertyPath.Replace(".Array.data[", "[");
            var elements = path.Split('.');

            var nestedProperty = NestedPropertyOrigin(property, elements);

            // if nested property is null = we hit an array property
            if (nestedProperty == null)
            {
                var cleanPath = path.Substring(0, path.IndexOf('['));
                var arrayProp = property.serializedObject.FindProperty(cleanPath);
                WarningsPool.LogCollectionsNotSupportedWarning(arrayProp, nameof(ConditionalFieldAttribute));

                return(null);
            }

            return(nestedProperty.FindPropertyRelative(propertyName));
        }
Esempio n. 9
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializedProperty minProp = property.FindPropertyRelative("Min");
            SerializedProperty maxProp = property.FindPropertyRelative("Max");

            if (minProp == null || maxProp == null)
            {
                WarningsPool.Log("MinMaxRangeAttribute used on <color=brown>" +
                                 property.name +
                                 "</color>. Must be used on types with Min and Max fields",
                                 property.serializedObject.targetObject);

                return;
            }

            var minValid = minProp.propertyType == SerializedPropertyType.Integer || minProp.propertyType == SerializedPropertyType.Float;
            var maxValid = maxProp.propertyType == SerializedPropertyType.Integer || maxProp.propertyType == SerializedPropertyType.Float;

            if (!maxValid || !minValid || minProp.propertyType != maxProp.propertyType)
            {
                WarningsPool.Log("MinMaxRangeAttribute used on <color=brown>" +
                                 property.name +
                                 "</color>. Min and Max fields must be of int or float type",
                                 property.serializedObject.targetObject);

                return;
            }

            MinMaxRangeAttribute rangeAttribute = (MinMaxRangeAttribute)attribute;

            label    = EditorGUI.BeginProperty(position, label, property);
            position = EditorGUI.PrefixLabel(position, label);

            bool isInt = minProp.propertyType == SerializedPropertyType.Integer;

            float minValue = isInt ? minProp.intValue : minProp.floatValue;
            float maxValue = isInt ? maxProp.intValue : maxProp.floatValue;
            float rangeMin = rangeAttribute.Min;
            float rangeMax = rangeAttribute.Max;


            const float rangeBoundsLabelWidth = 40f;

            var rangeBoundsLabel1Rect = new Rect(position);

            rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth;
            GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString(isInt ? "F0" : "F2")));
            position.xMin += rangeBoundsLabelWidth;

            var rangeBoundsLabel2Rect = new Rect(position);

            rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth;
            GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString(isInt ? "F0" : "F2")));
            position.xMax -= rangeBoundsLabelWidth;

            EditorGUI.BeginChangeCheck();
            EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax);

            if (EditorGUI.EndChangeCheck())
            {
                if (isInt)
                {
                    minProp.intValue = Mathf.RoundToInt(minValue);
                    maxProp.intValue = Mathf.RoundToInt(maxValue);
                }
                else
                {
                    minProp.floatValue = minValue;
                    maxProp.floatValue = maxValue;
                }
            }

            EditorGUI.EndProperty();
        }
Esempio n. 10
0
 public static void LogMethodNotFound(UnityEngine.Object owner, string method) => WarningsPool.LogWarning(owner,
                                                                                                          $"Conditional Attribute is trying to invoke method {method.Colored(Colors.brown)} " +
                                                                                                          "which is missing or not with a bool return type",
                                                                                                          owner);
Esempio n. 11
0
 private static void LogFieldNotFound(UnityEngine.Object owner, string field) => WarningsPool.LogWarning(owner,
                                                                                                         $"Conditional Attribute is trying to check field {field.Colored(Colors.brown)} which is not present",
                                                                                                         owner);
Esempio n. 12
0
 private static void LogFieldNotFound(SerializedProperty property, string field) => WarningsPool.LogWarning(property,
                                                                                                            $"Conditional Attribute is trying to check field {field.Colored(Colors.brown)} which is not present",
                                                                                                            property.serializedObject.targetObject);