Example #1
0
        /// <summary>
        /// Create an instance of this tag's prefab, and make it the current instance being worked on by this tag handler
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public virtual XmlElement GetInstance(RectTransform parent, XmlLayout xmlLayout, string overridePrefabPath = null)
        {
            currentInstanceTransform = Instantiate(parent, overridePrefabPath ?? this.prefabPath);
            var xmlElement = currentInstanceTransform.gameObject.GetComponent <XmlElement>() ?? currentInstanceTransform.gameObject.AddComponent <XmlElement>();

            xmlElement.Initialise(xmlLayout, currentInstanceTransform, this);

            var parentXmlElement = parent.GetComponent <XmlElement>();

            if (parentXmlElement != null)
            {
                parentXmlElement.AddChildElement(xmlElement);
            }

            return(xmlElement);
        }
Example #2
0
        /// <summary>
        /// Set the Instance this ElementTagHandler is currently working with.
        /// </summary>
        /// <param name="instanceTransform"></param>
        /// <param name="xmlLayout"></param>
        public virtual void SetInstance(RectTransform instanceTransform, XmlLayout xmlLayout)
        {
            currentInstanceTransform = instanceTransform;
            currentXmlLayoutInstance = xmlLayout;

            var xmlElement = this.currentXmlElement;

            if (instanceTransform != null && xmlElement == null)
            {
                // Normally this won't be necessary, but sometimes we may be applying attribute values to child elements that aren't top-level XmlElements, so just in case
                xmlElement = currentInstanceTransform.gameObject.AddComponent <XmlElement>();
            }

            if (xmlElement != null)
            {
                xmlElement.Initialise(xmlLayout, instanceTransform, this);
            }
        }
Example #3
0
        public void OnEnable()
        {
            XmlLayout = (XmlLayout)target;
            SO_Target = new SerializedObject(target);

            XmlFile = SO_Target.FindProperty("XmlFile");
            AutomaticallyReloadXmlFileIfItChanges = SO_Target.FindProperty("AutomaticallyReloadXmlFileIfItChanges");
            Xml           = SO_Target.FindProperty("Xml");
            DefaultsFiles = SO_Target.FindProperty("DefaultsFiles");

            ForceRebuildOnAwake       = SO_Target.FindProperty("ForceRebuildOnAwake");
            ForceReloadXmlFileOnAwake = SO_Target.FindProperty("ForceReloadXmlFileOnAwake");

            LocalizationFile = SO_Target.FindProperty("LocalizationFile");

            editor_showXml           = SO_Target.FindProperty("editor_showXml");
            editor_xmlScrollPosition = SO_Target.FindProperty("editor_xmlScrollPosition");
            editor_showLocalization  = SO_Target.FindProperty("editor_showLocalization");
        }
        public static List <Color> GetColorList(string str, XmlLayout xmlLayout = null)
        {
            List <Color> list = new List <Color>();

            if (string.IsNullOrEmpty(str))
            {
                return(list);
            }

            var parts = GetParts(str, colorSeparators, false);

            foreach (var part in parts)
            {
                list.Add(part.ToColor(xmlLayout));
            }

            return(list);

            /*return str.Split(' ', '|')
             *        .Select(s => s.ToColor(xmlLayout))
             *        .ToList();*/
        }
        public static ColorBlock ToColorBlock(this string str, XmlLayout xmlLayout = null)
        {
            var colorBlock = new ColorBlock();

            colorBlock.normalColor     = colorBlock.disabledColor = colorBlock.pressedColor = Color.white;
            colorBlock.disabledColor   = new Color(1, 1, 1, 0.5f);
            colorBlock.colorMultiplier = 1;

            var colorList = GetColorList(str, xmlLayout);

            if (colorList.Count > 0)
            {
                colorBlock.normalColor = colorList[0];
            }
            if (colorList.Count > 1)
            {
                colorBlock.highlightedColor = colorList[1];
            }
#if UNITY_2019_1_OR_NEWER
            if (colorList.Count > 2)
            {
                colorBlock.pressedColor = colorBlock.selectedColor = colorList[2];
            }
#else
            if (colorList.Count > 2)
            {
                colorBlock.pressedColor = colorList[2];
            }
#endif
            if (colorList.Count > 3)
            {
                colorBlock.disabledColor = colorList[3];
            }

            return(colorBlock);
        }
Example #6
0
 public XmlElementReference(XmlLayout xmlLayout, string id, bool useInternalId = false)
 {
     this.xmlLayout     = xmlLayout;
     this.id            = id;
     this.useInternalId = useInternalId;
 }
Example #7
0
        /// <summary>
        /// Set the value of a nested member, e.g.
        /// ParentClass.ChildClass.childProperty
        /// </summary>
        /// <param name="type"></param>
        /// <param name="path"></param>
        /// <param name="o"></param>
        /// <param name="newValue"></param>
        public static void SetNestedMemberValue(Type type, string path, object o, object newValue, XmlLayout xmlLayout = null)
        {
            MemberInfo member = null;

            string[] parts = path.Split('.');

            for (int i = 0; i < parts.Length; ++i)
            {
                member = type.GetMember(parts[i])[0];
                type   = member.GetMemberDataType();

                if (i != parts.Length - 1)
                {
                    o = member.GetMemberValue(o);
                }
            }

            if (member != null)
            {
                if (newValue.GetType() == typeof(System.String) && newValue.GetType() != type)
                {
                    member.SetMemberValue(o, ((string)newValue).ChangeToType(type, xmlLayout));
                }
                else
                {
                    member.SetMemberValue(o, newValue);
                }
            }
        }
        public static object ChangeToType(this string str, Type type, XmlLayout xmlLayout = null)
        {
            if (String.IsNullOrEmpty(str) || (str.ToLower() == "none" && type != typeof(string)) || (str.StartsWith("{") && str.EndsWith("}")))
            {
                return(null);
            }

            if (CustomTypeConverters.ContainsKey(type))
            {
                return(CustomTypeConverters[type].Invoke(str, xmlLayout));
            }

            // Special cases : Enums & specific types
            if (type.IsEnum)
            {
                return(Enum.Parse(type, str, true));
            }

            switch (type.Name)
            {
            case "RectOffset":
                return(str.ToRectOffset());

            case "Rect":
                return(str.ToRect());

            case "Vector2":
                return(str.ToVector2());

            case "Vector3":
                return(str.ToVector3());

            case "Vector4":
                return(str.ToVector4());

            case "Boolean":
            case "bool":
                return(str.ToBoolean());

            case "Color":
                return(str.ToColor(xmlLayout));

            case "Color32":
                return((Color32)str.ToColor(xmlLayout));

            case "ColorBlock":
                return(str.ToColorBlock(xmlLayout));

            case "Sprite":
                return(str.ToSprite());

            case "Texture":
                return(str.ToTexture());

            case "Quaternion":
                return(str.ToQuaternion());

            case "Font":
                return(str.ToFont());

            case "AudioClip":
                return(str.ToAudioClip());

            case "Material":
                return(str.ToMaterial());

            case "CursorInfo":
                return(str.ToCursorInfo());

            case "float":
                return(str.ToFloat());

            case "int":
            case "Int32":
            case "Int64":
                return(Convert.ChangeType(str.ToInt(), type, CultureInfo));

            case "Transform":
                return(str.ToTransform());

#if DATEPICKER_PRESENT
            case "SerializableDate":
                return(new UI.Dates.SerializableDate(DateTime.Parse(str)));
#endif
            }

            // Special handling for float lists
            if (typeof(IEnumerable <float>).IsAssignableFrom(type))
            {
                return(GetFloatList(str));//.Select(i => (Single)i).ToList();
            }

            // Default behaviour
            return(Convert.ChangeType(str, type, CultureInfo));
        }
 public static T ChangeToType <T>(this string str, XmlLayout xmlLayout = null)
 {
     return((T)str.ChangeToType(typeof(T), xmlLayout));
 }
        public static Color ToColor(this string str, XmlLayout xmlLayout = null)
        {
            // MVVM color, will be set by properties
            if (str.StartsWith("{"))
            {
                return(Color.white);
            }

            str = str.ToLower();

            MatchCollection matches;

            // new: by user-defined name
            if (xmlLayout != null)
            {
                if (xmlLayout.namedColors.ContainsKey(str))
                {
                    return(xmlLayout.namedColors[str]);
                }
            }

            // a) HTML code
            if (str.StartsWith("#"))
            {
                return(HexStringToColor(str));
            }

            // b) RGB / RGBA
            if (str.StartsWith("rgb"))
            {
                matches = rgbTest.Matches(str);

                if (matches.Count >= 3)
                {
                    float r = GetColorValue(matches[0].Value);
                    float g = GetColorValue(matches[1].Value);
                    float b = GetColorValue(matches[2].Value);

                    float a = 1f;
                    if (matches.Count == 4)
                    {
                        a = GetColorValue(matches[3].Value);
                    }

                    return(new Color(r, g, b, a));
                }
                else
                {
                    Debug.LogWarning("[XmlLayout] Warning: '" + str + "' is not a valid Color value.");
                }
            }

            // c) By name
            var propertyInfo = typeof(Color).GetProperty(str);

            if (propertyInfo != null && propertyInfo.PropertyType == typeof(Color))
            {
                return((Color)propertyInfo.GetValue(null, XmlLayoutUtilities.BindingFlags, null, null, null));
            }

            Debug.LogWarning("[XmlLayout] Warning: '" + str + "' is not a valid Color value.");

            // default
            return(Color.clear);
        }
Example #11
0
 public XmlElementReference(XmlLayout xmlLayout, string id)
 {
     this.xmlLayout = xmlLayout;
     this.id        = id;
 }