zoukankan      html  css  js  c++  java
  • unity编辑器脚本工具练习

      因为刚入公司不久,一直在做UI,前两天老大让我改一下默认UI字体,我看了下UI目录,十几个子目录,几十个UI预制体,每个UI下的Text对象数量不确定,而且Text对象所在的层还不确定,找一个预制体下的所有Text对象再修改至少需要2分钟时间。如果我手动一个个找并修改,这活不用2小时是干不完的。网游的UI你懂得,多而复杂。

      所以我并没有拼手速,而是开始了制作一个简易插件,现在把这个插件分享一下,相当于给大家提供一个编辑器工具学习的例子吧。

      首先需求很明确,就是选中一个预制体,并将他的子对象中的Text组件的字体修改为公司要求的字体。

      首先,不能在运行条件下修改,因为公司的UI架构比较变态,各种组件有关联。我开始懒省劲,运行加载所有的预制体,修改后保存,结果悲剧了,具体不想说。所以还是要通过编辑器代码来修改。同时又不能更改预制体目录,这就意味着不能使用Resources,以及其他限制。

      好,介绍了那么多废话,现在正题:

      1,获取到需要修改的预制体对象

      2,获取预制体下的所有Text对象,并筛选出需要修改的Text对象

      3,修改Text对象的默认字体为要求字体。

      我说一下第二步遇到的问题,获取所有的Text组件。因为预制体中有些子对象是隐藏的,所以不能使用GetComponetsInChildren<>(),因为这个函数无法得到隐藏的对象,所以需要自己写个找到所有子对象的函数。

      接下来的就很简单了,先弄个菜单编辑器,选中对象,并筛选,其代码如下:

      

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using UnityEngine.UI;
    using System.Collections.Generic;
    public class TestFont
    {
        static GameObject targetObj;
        static string DefaultFont = "Arial";//默认字体的名称
        static List<Text> waiteForUpdate = new List<Text>();
    
        static List<Transform> chindernList = new List<Transform>();
    
        static Transform GetAllTransform(Transform parent)
        {
    
            foreach (Transform child in parent)
            {
                chindernList.Add(child);
                if (child.childCount > 0)
                    GetAllTransform(child);
            }
    
            return null;
        }
    
        [MenuItem("FontEdit/SelectTarget")]
        static void ShowTextCount()
        {
            targetObj = Selection.activeGameObject;//这个函数可以得到你选中的对象
    
    
            if (targetObj)
            {
                bool isok = false;
                chindernList.Clear();
                GetAllTransform(targetObj.transform);//通过一个递归函数得到所有的子对象
                for (int i = 0; i < chindernList.Count; i++)
                {
                    Text _Text = null;
                    if ((_Text = chindernList[i].GetComponent<Text>()) && (!_Text.font || _Text.font.name == DefaultFont))
                    {
                        waiteForUpdate.Add(chindernList[i].GetComponent<Text>());
                        isok = true;
                    }
                }
    
                if (isok)
                {
                    if (!targetObj.GetComponent<FontSet>())
                    {
                        targetObj.AddComponent<FontSet>();
                    }
                    FontSet set = targetObj.GetComponent<FontSet>();//为对象添加个组件
    
                    set.UpdateFont(waiteForUpdate);
                    chindernList.Clear();
                    waiteForUpdate.Clear();
                    GameObject.DestroyImmediate(targetObj.GetComponent<FontSet>());//修改完毕移除组件
                }
            }
        }
    
    
    
    }

    代码很简单,我就不详细注释了,接下来的代码也很简单,就是修改字体而已

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using System.Collections.Generic;
    public class FontSet : MonoBehaviour
    {
    
        public Font UYFont;
        public void UpdateFont(List<Text> sets)
        {
            print(sets.Count);
            for (int i = 0; i < sets.Count; i++)
            {
                string str = sets[i].font == null ? "null" : sets[i].font.name;
                print(sets[i].name + ":" + str);
                sets[i].font = UYFont;
            }
        }
    }

    需要注意的是,你要在选中这个脚本,然后在Inspector里面对字体对象赋值,这样就可以放心的去修改了。

      接下来就是一个个去选择预制体,然后点击FontEdit/SelectTarget就Ok了,剩下的玩家自己测试吧。很简单的小插件,希望帮到你。

      本文链接:http://www.cnblogs.com/jqg-aliang/p/4834488.html,转载请申明出处,谢谢!

  • 相关阅读:
    MySQL存储引擎--MyISAM与InnoDB区别
    HTTP Keep-Alive模式
    php通过curl下载远程图片实例
    使用PHP QR Code生成二维码
    PHP中输出文件,怎么区别什么时候该用readfile() , fread(), file_get_contents(), fgets()
    SSDB 一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis.
    html头文件设置常用之<meta>设置缓存
    redis使用watch完成秒杀抢购功能
    Linux信号(signal) 机制分析
    php信号处理
  • 原文地址:https://www.cnblogs.com/jqg-aliang/p/4834488.html
Copyright © 2011-2022 走看看