示例#1
0
        public static GUISkin LoadCSS(string name, string contents)
        {
            var           skin      = ScriptableObject.CreateInstance <GUISkin>();
            var           styles    = new Dictionary <string, GUIStyle>();
            GUIStyle      active    = null;
            GUIStyleState state     = null;
            var           stateName = "";
            //var textures = FileManager.GetAssets<Texture2D>().ToDictionary(x=>x.name,x=>x);
            //var fonts = FileManager.GetAssets<Texture2D>().ToDictionary(x=>x.name,x=>x);
            var textures = FileManager.GetNamedAssets <Texture2D>();
            var fonts    = FileManager.GetNamedAssets <Font>();

            foreach (var current in contents.GetLines())
            {
                var line = current.TrimLeft(".").Trim();
                if (line.Contains("{"))
                {
                    state     = null;
                    stateName = "";
                    bool builtin   = !current.StartsWith(".");
                    var  styleName = line.Contains(":") ? line.Parse("", ":") : line.Parse("", "{");
                    styleName   = styleName.Trim();
                    active      = builtin ? skin.GetVariable <GUIStyle>(styleName) : styles.AddNew(styleName);
                    active.name = styleName;
                    if (line.Contains(":"))
                    {
                        state     = new GUIStyleState();
                        stateName = line.Parse(":", "{").Trim();
                        active.SetVariable <GUIStyleState>(stateName, state);
                    }
                    continue;
                }
                if (line.StartsWith("}"))
                {
                    continue;
                }
                var term  = line.Parse("", ":").Trim();
                var value = line.Parse(":", "").TrimRight(";").Trim();
                if (!state.IsNull())
                {
                    if (term.Contains("background"))
                    {
                        state.background = textures.ContainsKey(value) ? textures[value] : null;
                    }
                    if (term.Contains("text-color"))
                    {
                        state.textColor = value.ToColor();
                    }
                    continue;
                }
                if (term.Contains("border"))
                {
                    active.border = value.ToRectOffset(" ");
                }
                if (term.Contains("margin"))
                {
                    active.margin = value.ToRectOffset(" ");
                }
                if (term.Contains("padding"))
                {
                    active.padding = value.ToRectOffset(" ");
                }
                if (term.Contains("overflow"))
                {
                    active.overflow = value.ToRectOffset(" ");
                }
                if (term.Contains("font"))
                {
                    active.font = fonts.ContainsKey(value) ? fonts[value] : null;
                }
                if (term.Contains("font-size"))
                {
                    active.fontSize = value.ToInt();
                }
                if (term.Contains("font-style"))
                {
                    active.fontStyle = (FontStyle)Enum.Parse(typeof(FontStyle), value);
                }
                if (term.Contains("alignment"))
                {
                    active.alignment = (TextAnchor)Enum.Parse(typeof(TextAnchor), value);
                }
                if (term.Contains("word-wrap"))
                {
                    active.wordWrap = value.ToBool();
                }
                if (term.Contains("rich-text"))
                {
                    active.wordWrap = value.ToBool();
                }
                if (term.Contains("text-clipping"))
                {
                    active.clipping = (TextClipping)Enum.Parse(typeof(TextClipping), value);
                }
                if (term.Contains("image-position"))
                {
                    active.imagePosition = (ImagePosition)Enum.Parse(typeof(ImagePosition), value);
                }
                if (term.Contains("content-offset"))
                {
                    active.imagePosition = (ImagePosition)Enum.Parse(typeof(ImagePosition), value);
                }
                if (term.Contains("fixed-width"))
                {
                    active.fixedWidth = value.ToFloat();
                }
                if (term.Contains("fixed-height"))
                {
                    active.fixedHeight = value.ToFloat();
                }
                if (term.Contains("stretch-width"))
                {
                    active.stretchWidth = value.ToBool();
                }
                if (term.Contains("stretch-height"))
                {
                    active.stretchHeight = value.ToBool();
                }
            }
            skin.customStyles = styles.Values.ToArray();
            return(skin);
        }