修改Unity 的 UI 菜单中默认创建出的控件(反射)or (扩展)

反射 ------------------------------------------------------------------------------------------

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

[InitializeOnLoad]
static internal class UIMenuOptionsExtend
{
    // The reflected dafault methods.
    private static MethodInfo m_miGetDefaultResource = null;
    private static MethodInfo m_miPlaceUIElementRoot = null;

    static UIMenuOptionsExtend()
    {
        L.LogError("UIMenuOptionsExtend");
        Initialize();
    }

    private static void Initialize()
    {
        // Get all loaded assemblies.
        Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
        Assembly uiEditorAssembly = null;
        foreach (Assembly assembly in allAssemblies)
        {
            AssemblyName assemblyName = assembly.GetName();
            if ("UnityEditor.UI" == assemblyName.Name)
            {
                uiEditorAssembly = assembly;
                break;
            }
        }

        // Check if we find ui assembly.
        if (null == uiEditorAssembly)
        {
            Debug.LogError("Can not find assembly: UnityEditor.UI.dll");
            return;
        }

        // Get things we need.
        Type menuOptionType = uiEditorAssembly.GetType("UnityEditor.UI.MenuOptions");
        m_miGetDefaultResource = menuOptionType.GetMethod("GetStandardResources", BindingFlags.NonPublic | BindingFlags.Static);
        m_miPlaceUIElementRoot = menuOptionType.GetMethod("PlaceUIElementRoot", BindingFlags.NonPublic | BindingFlags.Static);
    }

    [MenuItem("GameObject/UI/Text", false, 2000)]
    static public void AddText(MenuCommand menuCommand)
    {
        GameObject go = DefaultControls.CreateText((DefaultControls.Resources)m_miGetDefaultResource.Invoke(null, null));
        m_miPlaceUIElementRoot.Invoke(null, new object[] { go, menuCommand });

        // Remove raycast target.
        Text text = go.GetComponent<Text>();
        text.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/Resources/Windows/Font/MyArial.ttf"); 
        text.raycastTarget = true;
    }
}

-------------------------------------------------------------------------------------------------------------------------------------

扩展

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


public class OverrideCreateUIMenu 
 {
        // 如果第一次创建UI元素 可能没有 Canvas、EventSystem对象!  
        private static GameObject SecurityCheck()
        {
            GameObject canvas;
            var cc = Object.FindObjectOfType<Canvas>();
            if (!cc)
            {
                canvas = new GameObject("_Canvas", typeof(Canvas));
                if (!Object.FindObjectOfType<EventSystem>())
                {
                    GameObject eventSystem = new GameObject("_EventSystem", typeof(EventSystem));
                 }
        }
            else
            {
                canvas = cc.gameObject;
            }


            return canvas;
        }

        // 重写设置字体
        [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("MyArialText", typeof(Text));  
            var text = go.GetComponent<Text>();  
            text.raycastTarget = false;  //默认去掉点击事件
            text.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/Resources/Windows/Font/MyArial.ttf");   // 默认MyArial字体  
            go.transform.SetParent(Selection.activeTransform);  
            Selection.activeGameObject = go;    
            return go;  
       } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值