UnityEditor之VisualElement的样式uss的背景颜色和字体设置

众所周知,Unity2019(2017??)之后推出了UIToolKit,

使得UI可以支持样式文件(uss)

(其实我是最后一个才知道)

说起样式文件,其实大家第一时间应该想到的应该是html+css

而事实也是如此

写法也是可以无限接近.css

.gs{
    /*--備注了的都是不行的寫法(貌似图片必须放在指定目录下,url()參考:https://docs.unity3d.com/Manual/UIE-USS-PropertyTypes.html*/
    --background-image: url("UssImg/bg");
    --background-image: resource("UssImg/bg.jpg");//有后缀不行
    background-image: resource("UssImg/bg");        //和Unity传统一样,res获取不带后缀
    
}

只不过,我们也发现,整个UIToolkit的基类是VisualElement

我们也查了一下,其实C#的xamain 就那么刚刚又visualelement,也又backgroundcolor属性

当然,也没有更多的证据证明Unity是抄袭Xmmarin的

反正,就是看着办,直接写代码吧

想,把一个搜索框改变样式

uss代码

.pro貌似是。。。(个人冤枉啊,良好程序员啊,没有用暴力啊)

.search-input则是样式文件的class命名,暂时没发现# id命名(命名可以随意)

.pro .gs.search-input TextInput {
    --border-color: #4f4f4f;
    background-color: white;
    color:#9a828a;
}

.gs.search-input TextInput {
    padding-left: 24px;
    padding-right: 24px;
    margin-bottom: 0px;
    border-width: 1px;
    border-radius: 5px;
    border-color: #c6c6c6;
    -unity-overflow-clip-box: content-box;
}

c#代码

           this.AddToClassList("search-input");

添加.uss文件代码

//.GetNewExcelSheetPath()用了一些技巧,主要是获取uss文件的绝对路径
var styleSheetPath = Utilities.GetNewExcelSheetPath();
var stylesheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(styleSheetPath);
rootVisualElement.styleSheets.Add(stylesheet);

获取路径代码(Unity Editor)

      internal static string GetNewExcelSheetPath()
        {
            var styleSheetsInProject = AssetDatabase.FindAssets("t:StyleSheet");
            foreach (var id in styleSheetsInProject )
            {
                var path = AssetDatabase.GUIDToAssetPath(id);
                var name = GetFileName(path);
                if (name.Contains("excelsheet"))
                {
                    var content = File.ReadAllText(path);
                    if (content.Contains("ac00000b1d1e4330a4d11276fc6dcea9"))//暫時為 excelsheetW1.uss
                    {
                        return path;
                    }
                }
            }
            //如果找不到目录文件,则返回默认目录路径
            return "Assets/ExcelSheetScripts/Editor/Styles/excelsheet.uss";
        }

根据以上代码,修改后的样式

参考

UIElement USS属性_漫漫无期的博客-CSDN博客

Unity2019 UIElement 笔记(六)USS介绍上_工 具 人-CSDN博客_unity uss

USS 支持的属性 - Unity 手册

html中input中文字的字体颜色,修改input框中placeholder的字体颜色_浪个锤子情报局的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/avi9111/article/details/123285938