/// <summary>
        /// Extract a sub property (Int, String, Bool... etc.) from the serialized property container
        /// </summary>
        /// <param name="property"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static SerializedProperty GetSlot(SerializedProperty property, ref Type type)
        {
            SerializedProperty serializedTypeProp = property.FindPropertyRelative("SerializedType");
            var enumValue =
                (SerializedType)Enum.GetValues(typeof(SerializedType)).GetValue(serializedTypeProp.enumValueIndex);

            var propertyName = NameValueBase.GetSlotName(enumValue);

            if ("ObjectReference" == propertyName)
            {
                type = GetType(property);
            }

            return(property.FindPropertyRelative(propertyName));
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the style property value from a serialized value<br/>
        /// Used by the system when in need to read the serialized value
        /// </summary>
        /// <param name="serializedProperty"></param>
        public static object Read(SerializedProperty serializedProperty)
        {
            var typeStr        = serializedProperty.FindPropertyRelative("Type").stringValue;
            var serializedType = (SerializedType)serializedProperty.FindPropertyRelative("SerializedType").enumValueIndex;

            //var type = value.GetType();
            if (!GlobalTypeDictionary.Instance.ContainsKey(typeStr))
            {
                throw new Exception("Couldn't get type from GlobalTypeDictionary: " + typeStr);
            }

            var type      = GlobalTypeDictionary.Instance[typeStr];
            var slotName  = NameValueBase.GetSlotName(serializedType);
            var valueProp = serializedProperty.FindPropertyRelative(slotName);

            if (type == typeof(int))
            {
                return(valueProp.intValue);
            }
            if (type == typeof(bool))
            {
                return(valueProp.boolValue);
            }
            if (type == typeof(float))
            {
                return(valueProp.floatValue);
            }
            if (type == typeof(string))
            {
                return(valueProp.stringValue);
            }
            if (type == typeof(Color))
            {
                return(valueProp.colorValue);
            }

            /*else if (type == typeof(LayerMask))
             *  valueProp.objectReferenceValue = (LayerMask)styleProperty.Value;*/
            if (type.IsEnum)  // == typeof(Enum))
            {
                return(Enum.Parse(GlobalTypeDictionary.Instance.Get(typeStr), valueProp.stringValue));
            }
            if (type == typeof(Vector2))
            {
                return(valueProp.vector2Value);
            }
            if (type == typeof(Vector3))
            {
                return(valueProp.vector3Value);
            }
            if (type == typeof(Rect))
            {
                return(valueProp.rectValue);
            }

            /*else if (type == typeof(Char))
             *  valueProp.stringValue = (Char)styleProperty.Value;*/
            if (type == typeof(AnimationCurve))
            {
                return(valueProp.animationCurveValue);
            }
            if (type == typeof(Bounds))
            {
                return(valueProp.boundsValue);
            }

            /*else if (type == typeof(Gradient))
             *  valueProp.Gradient = (Gradient)styleProperty.Value;*/
            if (type == typeof(Quaternion))
            {
                return(valueProp.quaternionValue);
            }
            if (type == typeof(UnityEngine.Object))
            {
                return(valueProp.objectReferenceValue);
            }

            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// Applies the style property value to a serialized value<br/>
        /// Used by the system when in need to update the serialized value (either just created or edited)
        /// </summary>
        /// <param name="serializedProperty"></param>
        /// <param name="styleProperty"></param>
        public static void Apply(SerializedProperty serializedProperty, NameValueBase styleProperty)
        {
            serializedProperty.FindPropertyRelative("Type").stringValue =
                styleProperty.Type;
            serializedProperty.FindPropertyRelative("SerializedType").enumValueIndex =
                (int)styleProperty.SerializedType;

            var value = styleProperty.Value;

            if (null != value)
            {
                //var type = value.GetType();
                if (!GlobalTypeDictionary.Instance.ContainsKey(styleProperty.Type))
                {
                    throw new Exception("Couldn't get type from GlobalTypeDictionary: " + styleProperty.Type);
                }

                var type      = GlobalTypeDictionary.Instance[styleProperty.Type];
                var slotName  = NameValueBase.GetSlotName(styleProperty.SerializedType);
                var valueProp = serializedProperty.FindPropertyRelative(slotName);

                if (type == typeof(int))
                {
                    valueProp.intValue = (int)styleProperty.Value;
                }
                else if (type == typeof(bool))
                {
                    valueProp.boolValue = (bool)styleProperty.Value;
                }
                else if (type == typeof(float))
                {
                    valueProp.floatValue = (float)styleProperty.Value;
                }
                else if (type == typeof(string))
                {
                    valueProp.stringValue = (string)styleProperty.Value;
                }
                else if (type == typeof(Color))
                {
                    valueProp.colorValue = (Color)styleProperty.Value;
                }

                /*else if (type == typeof(LayerMask))
                 *  valueProp.objectReferenceValue = (LayerMask)styleProperty.Value;*/
                else if (type.IsEnum)
                {// == typeof(Enum))
                    //valueProp.intValue = (int)styleProperty.Value;
                    valueProp.stringValue = Enum.GetName(GlobalTypeDictionary.Instance.Get(styleProperty.Type), value);
                }
                else if (type == typeof(Vector2))
                {
                    valueProp.vector2Value = (Vector2)styleProperty.Value;
                }
                else if (type == typeof(Vector3))
                {
                    valueProp.vector3Value = (Vector3)styleProperty.Value;
                }
                else if (type == typeof(Rect))
                {
                    valueProp.rectValue = (Rect)styleProperty.Value;
                }

                /*else if (type == typeof(Char))
                 *  valueProp.stringValue = (Char)styleProperty.Value;*/
                else if (type == typeof(AnimationCurve))
                {
                    valueProp.animationCurveValue = (AnimationCurve)styleProperty.Value;
                }
                else if (type == typeof(Bounds))
                {
                    valueProp.boundsValue = (Bounds)styleProperty.Value;
                }

                /*else if (type == typeof(Gradient))
                 *  valueProp.Gradient = (Gradient)styleProperty.Value;*/
                else if (type == typeof(Quaternion))
                {
                    valueProp.quaternionValue = (Quaternion)styleProperty.Value;
                }
                else if (type == typeof(UnityEngine.Object))
                {
                    valueProp.objectReferenceValue = (UnityEngine.Object)styleProperty.Value;
                }
            }
        }