Unity定制 Image、Text的对象生成

来源:互联网 发布:淘宝购物车删除 编辑:程序博客网 时间:2024/04/28 13:05

2016.4.14


昨天看到 雨凇的 Unity3D研究院之UGUI一个优化效率小技巧:  http://www.xuanyusong.com/archives/4006


完善了他所说的代码:

 

using UnityEngine;using UnityEngine.UI;using System.Collections;using UnityEditor;using UnityEngine.EventSystems;/// <summary>/// 创建 Text、Image 的时候默认不选中 raycastTarget 等/// </summary>public class OverrideCreateUIMenu{    /// <summary>    /// 第一次创建UI元素时,没有canvas、EventSystem所有要生成,Canvas作为父节点    /// 之后再空的位置上建UI元素会自动添加到Canvas下    /// 在非UI树下的GameObject上新建UI元素也会 自动添加到Canvas下(默认在UI树下)    /// 添加到指定的UI元素下    /// </summary>    [MenuItem("GameObject/UI/Image")]    static void CreatImages()    {        var canvasObj = SecurityCheck();        if (!Selection.activeTransform)      // 在根目录创建的, 自动移动到 Canvas下        {            // Debug.Log("没有选择对象");            Image().transform.SetParent(canvasObj.transform);        }        else // (Selection.activeTransform)        {            if (!Selection.activeTransform.GetComponentInParent<Canvas>())    // 没有在UI树下            {                Image().transform.SetParent(canvasObj.transform);            }            else            {                Image();            }        }    }    private static GameObject Image()    {        GameObject go = new GameObject("x_Image", typeof(Image));        go.GetComponent<Image>().raycastTarget = false;        go.transform.SetParent(Selection.activeTransform);        Selection.activeGameObject = go;        return go;    }    // 我们要设置默认字体    [MenuItem("GameObject/UI/Text")]    static void CreatTexts()    {        var canvasObj = SecurityCheck();        if (!Selection.activeTransform)      // 在根目录创建的, 自动移动到 Canvas下        {            // Debug.Log("没有选择对象");            Text().transform.SetParent(canvasObj.transform);        }        else // (Selection.activeTransform)        {            if (!Selection.activeTransform.GetComponentInParent<Canvas>())    // 没有在UI树下            {                Text().transform.SetParent(canvasObj.transform);            }            else            {                Text();            }        }    }    private static GameObject Text()    {        GameObject go = new GameObject("x_Text", typeof(Text));        var text = go.GetComponent<Text>();        text.raycastTarget = false;        text.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/Arts/Fonts/zh_cn.TTF");   // 默认字体        go.transform.SetParent(Selection.activeTransform);        Selection.activeGameObject = go;        //go.AddComponent<Outline>();   // 默认添加 附加组件        return go;    }    // 如果第一次创建UI元素 可能没有 Canvas、EventSystem对象!    private static GameObject SecurityCheck()    {        GameObject canvas;        var cc = Object.FindObjectOfType<Canvas>();        if (!cc)        {            canvas = new GameObject("_Canvas", typeof(Canvas));        }        else        {            canvas = cc.gameObject;        }        if (!Object.FindObjectOfType<EventSystem>())        {            GameObject eventSystem = new GameObject("_EventSystem", typeof(EventSystem));        }        return canvas;    }}







3 0
原创粉丝点击