欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity Font size and style overrides are only supported for dynamic fonts.

程序员文章站 2024-02-16 23:18:28
...

跑游戏大量出现错误:

unity Font size and style overrides are only supported for dynamic fonts.

Font size and style overrides are only supported for dynamic fonts.

UnityEngine.Canvas:SendWillRenderCanvases()

 

unity跑游戏,不断弹出这个。

查出原因:

unity Font size and style overrides are only supported for dynamic fonts.

MSYH字体只有Normal没有Bold字体,导致不断提示这个错误,改回Normal字体就可以了。

 

解决方法:由于游戏UI预设很多,写了一个脚本SetFontStyleTools.cs脚本自动改预设。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UI;


public class SetFontStyleTools  {

    private const string FindFontName = "MSYH";

    [MenuItem("Assets/SetFontStyleTools")]
    /// <summary>
    /// 设置所以预设Text组件FontStyle设置为Normal
    /// </summary>
    static void SetALLUIFontStyleTools() {       
        //测试一个
        //List<GameObject> prefabList = new List<GameObject>(Selection.gameObjects);//GetAllUIPrefab();
        List<GameObject> prefabList = GetAllUIPrefab();

        int prefabCount = prefabList.Count;
        Debug.Log("prefabCount = " + prefabCount);

        List<Text> allTexts = new List<Text>();
        for (int i = prefabCount - 1; i >= 0; i--) {
            List<Text> onePrefabImageList = GetPrefabAllImages(prefabList[i]);
            Debug.Log("Prefab name = " + prefabList[i].name + ", imageCount: " + onePrefabImageList.Count);
            allTexts.AddRange(onePrefabImageList);
        }
        Debug.Log("Text count: " + allTexts.Count);
        SetFontStyle(allTexts);

        for (int i = prefabCount - 1; i >= 0; i--) {
            EditorUtility.SetDirty(prefabList[i]);
        }
        AssetDatabase.SaveAssets();
    }

    static List<Text> GetPrefabAllImages(GameObject obj) {
        List<Text> fitText = new List<Text>();
        Text[] texts = obj.GetComponentsInChildren<Text>(true);
        if (texts == null) return fitText;
        int childCount = texts.Length;
        for (int i = 0; i < childCount; i++) {
            if (texts[i] != null && texts[i].font != null && texts[i].font.name == FindFontName) {
                fitText.Add(texts[i]);
            }
        }
        return fitText;
    }

    /// <summary>
    /// 设置字体格式
    /// </summary>
    /// <param name="allTexts"></param>
    static void SetFontStyle(List<Text> allTexts) {
        int childCount = allTexts.Count; 
        for (int i = 0; i < childCount; i++) {
            allTexts[i].fontStyle = FontStyle.Normal;
        }
    }

    /// <summary>
    /// 获取所有的  
    /// </summary>
    /// <returns></returns>
    static List<GameObject> GetAllUIPrefab() {
        Object[] allObjs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        List<Object> allObjsList = new List<Object>(allObjs);
        List<GameObject> prefabList = new List<GameObject>();
        int count = allObjsList.Count;
        for (int i = count - 1; i >= 0; i--) {
            if (CheckObjectIsUIPrefab(allObjsList[i])) {
                prefabList.Add((GameObject)allObjsList[i]);
            }
        }
        return prefabList;
    }

    /// <summary>
    /// 判断是否为UI预制体
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    static bool CheckObjectIsUIPrefab(Object obj) {
        if (obj.GetType() != typeof(GameObject)) {
            return false;
        }
        GameObject item = (GameObject)obj;
        if (item != null && item.layer != LayerMask.NameToLayer("UI")) {
            // Debug.Log(obj.name + " is not ui prefab!");
            return false;
        }
        return true;
    }
}

 

 

知识扩展:gameObject.GetComponentsInChildren<T>(bool)

这个bool值在获取隐藏的物体时候必须传递参数为true,否则无法获取隐藏的组件。

相关标签: Unity UGUI Font