text ui unity3d_unity ui text - CSDN
  • Unity3D_UI

    2018-08-20 15:39:06
    using UnityEngine.UI; Text控件 Text text; text = GameObject.Find("ATestText").GetComponent<Text>() ; text.text="需要显示的的字符串"; Image控件 using System....

    UGUI

    using UnityEngine.UI;

    Text控件
    Text text;
    text = GameObject.Find("ATestText").GetComponent<Text>() ;
    text.text="需要显示的的字符串";


    Image控件
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System.IO;
    using System;
    public class UGUIImageControl : MonoBehaviour {

        // Use this for initialization
        Texture2D texture2D;
        public Image image;
        string ImagePath = "Assets/Cinema Suite/About/Resources/MocapKeyImage.png";
        string ImagePath2 = "sprites/tempIMG";//overrideSprite方法必须用Resources文件夹里的资源
        int a = 1;
        void Start () {

            image= GameObject.Find("Image_A").GetComponent<Image>();
            LoadByIO();
            
        }
        
        private void LoadByIO()
        { 
            /*
             * 读取图片文件
             */
            FileStream fileStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            fileStream.Seek(0, SeekOrigin.Begin);
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, (int)fileStream.Length);
            fileStream.Close();
            fileStream.Dispose();
            fileStream = null;

            /*
             * 创建Textture
             */
            int width = 300;
            int height = 372;
            Texture2D texture2D = new Texture2D(width,height);
            texture2D.LoadImage(bytes);
            
            /*
             * 创建精灵。
             */
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));

            image.sprite = sprite;
        }
        // Update is called once per frame
        void Update () {
            a++;
            if(a>300)
            {
                print(a);
                image.overrideSprite= Resources.Load<Sprite>(ImagePath2) as Sprite;
                //overrideSprite方法必须用Resources文件夹里的资源
            }
        }
    }


    Button控件
    1. 在onClick选项里操作。
    2. Button.onClick.AddListener(f);//f为编写好的函数。
    3. EventTrigger添加:(也可以用层级试图操作)
    using UnityEngine.EventSystems;
    using UnityEngine.Events;
    using UnityEngine;

    public class AddTrigger : MonoBehaviour {

        // Use this for initialization
        void Start() {
            UnityAction<BaseEventData> click = new UnityAction<BaseEventData>(MyClick);
            EventTrigger.Entry myClick = new EventTrigger.Entry();
            myClick.eventID = EventTriggerType.PointerClick;
            myClick.callback.AddListener(click);

            EventTrigger trigger = gameObject.AddComponent<EventTrigger>();
            trigger.triggers.Add(myClick);
        }

        public void MyClick(BaseEventData data)
        {
            Debug.Log("自己写的evnt trigger");
        }
    }

     

    Toggle 控件
    监听Toggle。
    public Toggle toggle;
    toggle.onValueChanged.AddListener((bool value)=>OnToggleClick(toggle,value));


    public void OnToggleClick(Toggle  toggle,bool value)
        {
            Debug.Log("toggle change"+(value?"On":"Off"));
        }


    slider滑动条
    scrollbar滚动条

     

    NGUI

    面板滚动:

    父物体Clipping加ScrollView脚本,子物体加 DragScrollView脚本,脚本参数为父物体。(注意锚点,锚点挂住无法滑动)

    添加Button的方式:

    新建gameObject,然后添加Button脚本 或者在

    Toogle和开关

    添加Toogle脚本和BoxCollider2D,3D的不行。

    Grid 整齐排列

    Table 表格排列,多出的列放到新的一行

    聊天窗口的制作(box Collider 要换成 2D 的)

    聊天窗口的制作

    PopupList 下拉列表

    展开全文
  • Unity3D UGUI Text得分数字增加 代码 一、首先在Hierarchy中创建Text组建,并绑定脚本。 using UnityEngine; using System.Collections; using UnityEngine.UI; public class Score : MonoBehaviour { ...

    Unity3D UGUI Text得分数字增加 代码

    在这里插入图片描述
    一、首先在Hierarchy中创建Text,并绑定脚本。

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;				**//导入资源库**
    
    public class Score : MonoBehaviour {
    	public static Text txt;				**//定义静态变量名以用于其他脚本内的引用**
    	public static float x = 0;
    	void Start () 
    	{
    		txt = GameObject.Find ("Text").GetComponent<Text> ();	 
    	}
    }
    
    

    二、在确定变量名指定的组建后,对得分时情况进行判断。这里进入判断绑定刚体是否碰撞的脚本。

    using UnityEngine;
    using System.Collections;
    
    public class collision2 : MonoBehaviour {
    	public GameObject bullet;	
    
    	void OnCollisionEnter(Collision col)				**//当刚体碰撞开始时**
    	{
    		if (col.gameObject.name.Equals ("Bullet(Clone)")) 
    		{
    			Instantiate (bullet,col.transform.position,Quaternion.identity);
    			Destroy(col.gameObject);  
    			Score.x += 1;				**//刚体碰撞结束后得分加1**
    			Score.txt.text = "Score : " + Score.x;				//在text文本中显示Score : x
    		}
    	}
    

    总结:这里的代码示例是以制定刚体碰撞的开始时,进行得分判断。当Bullet(Clone)与脚本绑定的刚体碰撞,便得分数 X+1 ,并在Score脚本中的txt中加上转换成文本类型后的X。

    展开全文
  • Text文本显示/隐藏 一、首先创建一个脚本,并绑定在对应的text中。 二、定义变量,并使文本保持隐藏 using UnityEngine; using System.Collections; using UnityEngine.UI; **//导入UI资源库** public ...

    Text文本显示/隐藏

    一、首先创建一个脚本,并绑定在对应的text中。

    在这里插入图片描述

    二、定义变量,并使文本保持隐藏

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;				**//导入UI资源库**
    
    public class die : MonoBehaviour 
    {
    	public static GameObject txt;				**//定义公共静态变量用于另外脚本的引用**
    
    	void Start () 
    	{
    		txt = GameObject.Find("die");
    		txt.SetActive (false);				**//保持txt的隐藏状态**
    	}
    }
    
    

    三、接下来进入另一个脚本,使玩家碰到指定物体时,显示文本

    using UnityEngine;
    using System.Collections;
    
    public class collision1 : MonoBehaviour {
    
    	private GameObject man;
    
    	void OnCollisionEnter(Collision col)				
    	{
    		if (col.gameObject.name.Equals ("prop_samoflange(Clone)")) 				**//当碰撞开始时**
    		{
    			die.txt.SetActive(true);				**//文本显示**
    		}
    

    首先文本利用SetActive函数控制其隐藏,当利用OnCollisionEnter方法,检测到物体碰撞时,显示文本。

    展开全文
  • 之前学了刘国柱老师的UI框架加上我自己的理解做了一个UI框架正好可以使用 这里附上刘国柱老师的博客http://www.cnblogs.com/LiuGuozhu/ 受益颇深 基本UI框架的类图就是这样了 大体是根据MVC加上我的魔改而成...

    最新:

    参考公司lua的mvvm写了一个c#版的,https://github.com/so-sos-so/SoF,欢迎提建议

    后续会新开一篇文章补充开发思路

    ------------------------------弃用---------------------------------

    之前学了刘国柱老师的UI框架加上我自己的理解做了一个UI框架正好可以使用

    这里附上刘国柱老师的博客 http://www.cnblogs.com/LiuGuozhu/ 受益颇深

    基本UI框架的类图就是这样了

    大体是根据MVC加上我的魔改而成的框架,至少我自己暂时觉得还是思路挺清晰的

    Panel的切换是使用的Canvas Group的更改透明度,在加上使用

    此方法来更改显示顺序和显隐的

    其中的界面反向切换是使用的栈后入先出的特性

    反向切换是指一个窗口弹出小窗口后,父窗口的Raycast需要取消,下面贴上核心代码

    using System.Collections;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class UIManager :MonoBehaviour {
    
        /// <summary>
        /// 单例模式:
        /// 1.定义一个静态的对象 在外界访问 内部构造
        /// 2.构造方法私有化
        /// </summary>
        #region 单例模式
    
        private static UIManager _instance;
    
        public static UIManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    UIManager[] instances = FindObjectsOfType<UIManager>();
                    if (instances != null)
                    {
                        _instance = instances[0];
                        for (var i = 1; i < instances.Length; i++)
                        {
                            Destroy(instances[i].gameObject);
                        }
                    }
                }
                return _instance;
            }
        }
    
        #endregion
    
    
        private Transform _canvas;
        public Transform Canvas
        {
            get
            {
                if (_canvas == null)
                    _canvas = GameObject.Find("Canvas").transform;
                return _canvas;
            }
        }
        //存储所有面板prefab的地址
        private Dictionary<UIPanelType, string> _DicPanelPath;
        //存储所有实例化的面板身上的BasePanel组件
        private Dictionary<UIPanelType, BasePanel> _DicPanel;
        //存储所有显示的面板上的BasePanel组件
        private Dictionary<UIPanelType, BasePanel> _DicShowPanel;
        //表示显示面板的栈
        private Stack<BasePanel> _StackPanel;
        //全屏显示的节点
        private Transform _TraFullScreen = null;
        //固定显示的节点
        private Transform _TraFixed = null;
        //弹出节点
        private Transform _TraPopUp = null;
        ////脚本节点
        //private Transform _TraScript = null;
    
    
        private void Awake()
        {
            ParseUIPanelPathJson();
            _StackPanel = new Stack<BasePanel>();
            _DicPanel = new Dictionary<UIPanelType, BasePanel>();
            _DicShowPanel = new Dictionary<UIPanelType, BasePanel>();
            _TraFullScreen = Canvas.Find("FullScreen");
            _TraFixed = Canvas.Find("Fixed");
            _TraPopUp = Canvas.Find("PopUp");
            DontDestroyOnLoad(Canvas.gameObject);
            //_TraScript = Canvas.Find("Script");
        }
    
    
    
        /// <summary>
        /// 把类型为反向切换的窗体入栈
        /// </summary>
        /// <param name="type"></param>
        private void PushPanel(UIPanelType type)
        {
            BasePanel basePanel = GetPanel(type);
            if (_StackPanel.Count >= 1)
            {
                BasePanel topPanel = _StackPanel.Peek();
                topPanel.OnPause();
            }
            _StackPanel.Push(basePanel);
            basePanel.OnEnter();
        }
    
        /// <summary>
        /// 把类型为反向切换的窗体出栈
        /// </summary>
        /// <param name="type"></param>
        private void PopPanel(UIPanelType type)
        {
            //如果栈里面为空,则返回
            if (_StackPanel.Count <= 0) return;
            BasePanel basePanel = GetPanel(type);
            BasePanel topPanel = _StackPanel.Peek();
            //如果当前要出栈的界面跟栈顶界面相同时 才会出栈
            if(basePanel.gameObject == topPanel.gameObject)
            {
                _StackPanel.Pop();
                topPanel.OnExit();
                //如果出栈后栈里面还有界面则恢复
                if(_StackPanel.Count >= 1)
                {
                    _StackPanel.Peek().OnResume();
                }
            }
            else
            {
                Debug.LogError("栈顶界面不是此界面,请检查进出栈!!");
            }
            
        }
    
        /// <summary>
        /// 显示界面
        /// </summary>
        /// <param name="type"></param>
        public void ShowUIForms(UIPanelType type)
        {
            BasePanel basePanel = GetPanel(type);
            switch (basePanel.uiType.uiFormShowMode)
            {
                case UIFormShowMode.Normal:
                    EnterUIFormsCache(type);
                    break;
                //如果是反切换类型则入栈
                case UIFormShowMode.ReverseChange:
                    PushPanel(type);
                    break;
                //如果是隐藏其他的类型,清空显示的界面
                case UIFormShowMode.HideOther:
                    HideOtherPanel(type);
                    break;
            }
        }
    
        /// <summary>
        /// 隐藏界面 如果显示界面字典里面存在此界面
        /// </summary>
        /// <param name="type"></param>
        public void HideUIForms(UIPanelType type)
        {
            BasePanel basePanel = GetPanel(type);
    
            switch (basePanel.uiType.uiFormShowMode)
            {
                case UIFormShowMode.Normal:
                    HideUIFormsCache(type);
                    break;
                //如果是反切换类型则出栈
                case UIFormShowMode.ReverseChange:
                    PopPanel(type);
                    break;
                case UIFormShowMode.HideOther:
                    HideUIFormsCache(type);
                    break;
            }
        }
    
        /// <summary>
        /// 隐藏除此之外的Panel,如果不赋值则清空所有
        /// </summary>
        /// <param name="type"></param>
        public void HideOtherPanel(UIPanelType type = UIPanelType.Null)
        {
            foreach (KeyValuePair<UIPanelType,BasePanel> item in _DicShowPanel)
            {
                item.Value.OnExit();
            }
            _DicShowPanel.Clear();
            if (type != UIPanelType.Null)
            {
                BasePanel basePanel = GetPanel(type);
                basePanel.OnEnter();
                _DicShowPanel.Add(type, GetPanel(type));
            }
                
        }
    
        /// <summary>
        /// 将UI加入到已显示面板的字典中
        /// </summary>
        /// <param name="type"></param>
        private void EnterUIFormsCache(UIPanelType type)
        {
            //如果显示面板字典里面有当前面板则返回
            if (_DicShowPanel.ContainsKey(type)) return;
            BasePanel basePanel = GetPanel(type);
            _DicShowPanel.Add(type, basePanel);
            basePanel.OnEnter();
        }
    
        /// <summary>
        /// 将UI隐藏
        /// </summary>
        /// <param name="type"></param>
        private void HideUIFormsCache(UIPanelType type)
        {
            //如果显示面板字典里面没有当前面板则返回
            if (!_DicShowPanel.ContainsKey(type)) return;
            BasePanel basePanel = GetPanel(type);
            _DicShowPanel.Remove(type);
            basePanel.OnExit();
        }
    
        /// <summary>
        /// 根据面板类型得到实例化的面板
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private BasePanel GetPanel(UIPanelType type)
        {
            if (_DicPanel.ContainsKey(type))
            {
                return _DicPanel[type];
            }
            else
            {
                //如果找不到就找这个面板的prefab的路径,然后根据prefab去实例化面板
                BasePanel panel;
                string panelPath = _DicPanelPath.GetValue(type);
                GameObject insGo = GameObject.Instantiate(Resources.Load(panelPath),Canvas) as GameObject;
                insGo.name = type.ToString();
                panel = insGo.GetComponent<BasePanel>();
                switch (panel.uiType.uIFormParentType)
                {
                    case UIFormParentType.FUllScreen:
                        insGo.transform.SetParent(_TraFullScreen);
                        break;
                    case UIFormParentType.Fixed:
                        insGo.transform.SetParent(_TraFixed);
                        break;
                    case UIFormParentType.PopUp:
                        insGo.transform.SetParent(_TraPopUp);
                        break;
                }
                _DicPanel.Add(type, panel);
                return panel;
            }
        }
    
        #region 解析地址json 并存储到字典中
        
        private void ParseUIPanelPathJson()
        {
            TextAsset text = Resources.Load<TextAsset>("UIPanelType");
            JSONObject jSONObject = new JSONObject(text.text);
            if (_DicPanelPath == null)
                _DicPanelPath = new Dictionary<UIPanelType, string>();
            foreach (JSONObject item in jSONObject.list)
            {
                string panelTypeString = item["panelTypeString"].str;
                string path = item["path"].str;
                UIPanelType uIPanelType = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
                _DicPanelPath.Add(uIPanelType, path);
            }
        }
    
        #endregion
    
    }
    
    using System.Collections;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    
    public class BasePanel : MonoBehaviour {
    
    
        protected CanvasGroup cg;
        public UIType uiType;
    
    
        private void Awake()
        {
            InitData();
        }
    
    
        public virtual void OnCloseBtnClick()
        {
            UIManager.Instance.HideUIForms(uiType.uiPanelType);
        }
    
    
        public virtual void InitData()
        {
            if (cg == null)
                cg = this.gameObject.GetAndAddComponent<CanvasGroup>();
            if (uiType == null)
                uiType = new UIType();
        }
    
    
    
    
        /// <summary>
        /// 界面被显示出来
        /// </summary>
        public virtual void OnEnter()
        {
            cg.alpha = 1;
            cg.blocksRaycasts = true;
            this.gameObject.transform.SetAsLastSibling();
        }
    
    
        /// <summary>
        /// 界面暂停 指的是当其他界面在此界面之上,此界面不是主界面的时候,此界面不接受鼠标检测,如果不需要此功能可以适当更改
        /// </summary>
        public virtual void OnPause()
        {
            cg.blocksRaycasts = false;
        }
    
    
        /// <summary>
        /// 界面继续
        /// </summary>
        public virtual void OnResume()
        {
            cg.blocksRaycasts = true;
        }
    
    
        /// <summary>
        /// 界面不显示,退出这个界面
        /// </summary>
        public virtual void OnExit()
        {
            cg.alpha = 0;
            cg.blocksRaycasts = false;
        }
    	
    }
    

     

    using System.Collections;
    using System;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class UIType  {
    
        /// <summary>
        /// panel类别
        /// </summary>
        public UIPanelType uiPanelType;
        /// <summary>
        /// Panel的显示方式,有正常显示,隐藏其他和反向切换
        /// </summary>
        public UIFormShowMode uiFormShowMode = UIFormShowMode.Normal;
        /// <summary>
        /// 这个透明度暂时没用
        /// </summary>
        public UIFormLucencyType uIFormLucencyType = UIFormLucencyType.Lucency;
        /// <summary>
        /// Panel的父物体类型
        /// </summary>
        public UIFormParentType uIFormParentType = UIFormParentType.PopUp;
    

     

    代码比较多,不过都有贴心的注释

     

    -------------------------------加了一个代码自动生成的工具-----------------------------------

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.Threading.Tasks;
    using System;
    using UnityEditor;
    using System.IO;
    using System.Text;
    using UnityEngine.UI;
    
    public class UIElement
    {
        public string Name;
        public string Path;
        public string ComponentName;
        public UIElement(string name,string path,string componentName)
        {
            Name = name;
            Path = path;
            ComponentName = componentName;
        }
    
        public override string ToString()
        {
            string str = string.Format("Name={0} || Path={1} || ComponentName={2}", Name, Path, ComponentName);
            return str;
        }
    }
    
    public class UICodeGenerator
    {
        [MenuItem ( "Assets/CreateCodeDeleteComponent" )]
        public static void CreateCodeDeleteComponent ()
        {
            GetPath ( true );
        }
        
        [MenuItem ( "Assets/OnlyCreateCode" )]
        public static void OnlyCreateCode ()
        {
            GetPath ( false );
        }
        
        public static void GetPath (bool isDeleteComponent)
        {
            var objs =
                Selection.GetFiltered ( typeof ( GameObject ), SelectionMode.Assets | SelectionMode.TopLevel );
            GameObject obj = objs[ 0 ] as GameObject;
            elements = new List<UIElement> ();
            GetPathAs ( obj.transform, isDeleteComponent );
    
            foreach ( var item in elements )
            {
                Debug.Log ( item );
            }
    
            GeneratePane ( "Assets/" + obj.name + ".cs", obj.name, elements );
            GenerateCtrl ( "Assets/" + obj.name + "Ctrl.cs", obj.name, elements );
    
        }
    
        public static List<UIElement> elements;
    
        static void GetPathAs ( Transform transform ,bool isDeleteComponent)
        {
            foreach ( Transform child in transform )
            {
                if ( child.gameObject.GetComponent<UIMark> () )
                {
                    elements.Add ( new UIElement ( child.name, GetPath ( child ),
                                                   child.gameObject.GetComponent<UIMark> ().ComponentName ) );
                    if ( isDeleteComponent )
                        GameObject.DestroyImmediate ( child.gameObject.GetComponent<UIMark> (), true );
                }
    
                if ( child.childCount != 0 )
                {
                    GetPathAs ( child, isDeleteComponent );
                }
            }
        }
    
    
        public static void GeneratePane ( string generateFilePath, string behaviourName, List<UIElement> elements )
        {
            var sw         = new StreamWriter ( generateFilePath, false, Encoding.UTF8 );
            var strBuilder = new StringBuilder ();
    
            strBuilder.AppendLine ( "using UnityEngine;" );
            strBuilder.AppendLine ( "using UnityEngine.UI;" );
            strBuilder.AppendLine ();
            strBuilder.AppendFormat ( "public class {0} : BasePanel ", behaviourName );
            strBuilder.AppendLine ();
            strBuilder.AppendLine ( "{" );
            foreach ( var item in elements )
            {
                strBuilder.AppendLine ( "\tpublic " + item.ComponentName + " " + item.Name + " { get; private set; }" );
            }
    
            strBuilder.AppendLine ();
            strBuilder.AppendLine ( "\tpublic override void InitData()" );
            strBuilder.AppendLine ( "\t{" );
            foreach ( var item in elements )
            {
                strBuilder.AppendFormat ( "\t\t{0} = transform.Find(\"{1}\").GetComponent<{2}>();", item.Name,
                                          item.Path.Replace ( behaviourName + "/", "" ), item.ComponentName );
                strBuilder.AppendLine ();
            }
    
            strBuilder.AppendLine ();
            strBuilder.AppendLine ( "\t\tuiType.uIFormParentType = UIFormParentType.PopUp;" );
            strBuilder.AppendLine ( "\t\tuiType.uiFormShowMode = UIFormShowMode.Normal;" );
            strBuilder.AppendLine ( "\t\tuiType.uiPanelType = UIPanelType.BoxPanel;" );
            strBuilder.AppendLine ( "\t}" );
            strBuilder.AppendLine ( "}" );
            sw.Write ( strBuilder );
            sw.Flush ();
            sw.Close ();
    
            AssetDatabase.SaveAssets ();
            AssetDatabase.Refresh ();
        }
    
        public static void GenerateCtrl ( string generateFilePath, string behaviourName , List<UIElement> elements)
        {
            var sw         = new StreamWriter ( generateFilePath, false, Encoding.UTF8 );
            var strBuilder = new StringBuilder ();
    
            List<UIElement> temp = new List<UIElement> ();
            
            foreach ( UIElement element in elements )
            {
                if(element.ComponentName.Equals("Button"))
                    temp.Add(element);
            }
            
            strBuilder.AppendLine ( "using UnityEngine;" );
            strBuilder.AppendLine ( "using UnityEngine.UI;" );
            strBuilder.AppendLine ();
            strBuilder.AppendFormat ( "public class {0}Ctrl : BaseCtrl ", behaviourName );
            strBuilder.AppendLine ( "{" );
            strBuilder.AppendLine ();
            strBuilder.AppendFormat ( "\tprivate {0} panel;", behaviourName );
            strBuilder.AppendLine ();
            strBuilder.AppendLine ();
            strBuilder.AppendLine ( "\tpublic override void InitPanel()" );
            strBuilder.AppendLine ( "\t{" );
            strBuilder.AppendFormat ( "\t\tpanel = GetComponent<{0}>();", behaviourName );
            strBuilder.AppendLine ();
            foreach ( UIElement element in temp )
            {
                strBuilder.AppendFormat ( "\t\tpanel.{0}.AddListenerGracefully( {1}Click );", element.Name, element.Name );
                strBuilder.AppendLine ();
            }
            strBuilder.AppendLine ( "\t}" );
            strBuilder.AppendLine ();
            foreach ( UIElement element in temp )
            {
                strBuilder.AppendFormat ("\tvoid {0}Click()",element.Name);
                strBuilder.AppendLine ();
                strBuilder.AppendLine ( "\t{" );
                strBuilder.AppendLine ();
                strBuilder.AppendLine ( "\t}" );
                strBuilder.AppendLine ();
            }
    
            strBuilder.AppendLine ( "}" );
            sw.Write ( strBuilder );
            sw.Flush ();
            sw.Close ();
    
            AssetDatabase.SaveAssets ();
            AssetDatabase.Refresh ();
        }
    
        public static string GetPath ( Transform transform )
        {
            var sb = new System.Text.StringBuilder ();
            var t  = transform;
            while ( true )
            {
                sb.Insert ( 0, t.name );
                t = t.parent;
                if ( t )
                {
                    sb.Insert ( 0, "/" );
                }
                else
                {
                    return sb.ToString ();
                }
            }
        }
    }
    

     

     

    using UnityEngine.UI;
    using UnityEngine;
    
    /// <inheritdoc />
    /// <summary>
    /// UI的标记
    /// </summary>
    public class UIMark : MonoBehaviour
    {
        //public UIMarkType MarkType = UIMarkType.DefaultUnityElement;
    
        public Transform Transform
        {
            get { return transform; }
        }
    
        public string CustomComponentName = "";
        
        public virtual string ComponentName
        {
            get
            {
                if ( !string.IsNullOrEmpty ( CustomComponentName ) )
                    return CustomComponentName;
                if (null != GetComponent("SkeletonAnimation"))
                    return "SkeletonAnimation";
                if (null != GetComponent<ScrollRect>())
                    return "ScrollRect";
                if (null != GetComponent<InputField>())
                    return "InputField";
                if (null != GetComponent<Text>())
                    return "Text";
                if (null != GetComponent("TMP.TextMeshProUGUI"))
                    return "TextMeshProUGUI";
                if (null != GetComponent<Button>())
                    return "Button";
                if (null != GetComponent<RawImage>())
                    return "RawImage";
                if (null != GetComponent<Toggle>())
                    return "Toggle";
                if (null != GetComponent<Slider>())
                    return "Slider";
                if (null != GetComponent<Scrollbar>())
                    return "Scrollbar";
                if (null != GetComponent<Image>())
                    return "Image";
                if (null != GetComponent<ToggleGroup>())
                    return "ToggleGroup";
                if (null != GetComponent<Animator>())
                    return "Animator";
                if (null != GetComponent<Canvas>())
                    return "Canvas";
                if (null != GetComponent("Empty4Raycast"))
                    return "Empty4Raycast";
                if (null != GetComponent<RectTransform>())
                    return "RectTransform";
    
                return "Transform";
    
    
            }
        }
    }
    

    使用方法,把需要使用的组件加上UIMark标记,拖成预设体,右键点击自动生成代码,就会自动生成视图和控制层

    -------------------------------弃用-------------------------------------

    思想可以借鉴,但是使用起来不是很方便

    最新版框架借鉴QFramework,喜欢的可以去看看

    展开全文
  • Unity3d 5 官方教程:UI

    2017-03-29 23:30:12
    本篇是对UnityUI系统主要特性的介绍。 相关教程:用户界面CanvasCanvas是所有UI元素应当存放于内的区域。Canvas是一个有Canvas组件的游戏对象,并且所有UI对象必须是这样一个Canvas对象的子对象。
  • unity3D项目中很多需要配置文件,对于一些简单的配置,我们可以用text文件来保存我们的配置数据。下面是我的解析代码: using System; using System.Collections.Generic; using UnityEngine; using System.IO; ...
  • 笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀...CSDN视频网址:http://edu.csdn.net/lecturer/144 在利用Unity3D引擎开发程序时,UI资源的加载,卸载,隐藏以及UI渐变动画等功能是UI架构设计
  • unity UItext and image

    2018-05-31 19:27:28
    新建后默认在下边的中间在Transform的小三角那儿右击,选reset position,text会中画布的中间字号太大看不见了是因为放置text的框太小了,放大就好了字体修改,默认只有一种直接把windows自带的字体复制到unity即可...
  • unity3D大作业素材,包括大量UI界面包:Cartoon Game GUI 3 1.0.
  • Unity3D中使用3D Text(Text Mesh)时,如何让场景中的物体和3D Text有正确的遮挡关系。由于3D Text默认材质是(Font Material),他的shader是GUI/Text Shader,自然也就有了GUI的穿透特性,所以我们使用自定义材质就...
  • 一、两个基本组件 Rect Transform:用于控制UI物体的基本属性 Canvas Renderer:画板UI渲染器,必备且不可编辑 ...Text文本除了上述的两个基础组件外,还有一个必备的Text组件 一些基础属性: Font:字体...
  • 一、搭建场景注意:UICamera需要包含整个场景二、编写关于Text显示...* 主题:实现UIText作为标签显示在物体上面 * Description: * 功能:XXX * Date:2017 * Version:0.1版本 * Author:Coffee * Modify Reco...
  • unity3d中添加场景UI

    2019-09-18 11:14:48
    修改canvas的renderMode改成worldSpace,去掉其他组件,修改位置,缩放值改为0.01(可根据项目需求自行修改)从图中可以看出:player是一个单独的draw call,canvas整体是一个draw call,里面text和图片button分别...
  • tempQie2.transform.SetParent(GameObject.Find("Canvas").transform); Text text = tempQie2.GetComponent(); text.text = contentStr; text.fontSize = fontSize;
  • Unity 中的 Rich Text Unity中的富文本格式,通过类似html的标签语言来实现,支持常见的加粗,倾斜,颜色等,可以应用于GUI、UGUI和Debug日志输出。 Rich Text 的使用 原文: We are definitely not amused ...
  • 开发版本:Unity 2017.1.1f1、VS 2017 适合人群:适合有一定基础的童鞋!一、什么是UI框架UI框架用于管理场景中的所有面板,控制面板之间的切换,可以加快开发进度、提高代码质量。二、实现思路根据用户界面调用情况...
  • 已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。CSDN视频网址:http://edu.csdn.net/lecturer/144 在快速实现UI架构设计一中已经实现了两个类,这两个类都...
  • unity3d UI性能优化。

    2018-04-28 18:00:13
    UI(UGUI)上面的优化,我想从三个方向来分析:drawcall的优化动静分离的优化文本,图片预选项的优化三个优化,性能占比,从高到底。一. drawcall的优化 drawcall可以通过点开Stats和Window-&gt;Frame ...
  • 记录一下,以防忘记 ...uiMenu.GetComponent&lt;InterfaceAnimManager&gt;().startAppear(); //打开   uiMenu.GetComponent&lt;InterfaceAnimManager&gt;().startDisappear();//关闭
1 2 3 4 5 ... 20
收藏数 5,075
精华内容 2,030
关键字:

text ui unity3d