Unity3D实现通用的给3D物体添加Text文本标签

一、搭建场景

注意:UICamera需要包含整个场景
二、编写关于Text显示标签的脚本(Test_DriftingSpecialEffect)
/***
*	Title:"XXX" 项目
*		主题:实现UIText作为标签显示在物体上面
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

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

namespace SimpleUIFrame
{
	public class Test_DriftingSpecialEffect : MonoBehaviour
	{
        private GameObject _TargetObj;                          //目标对象
        private Camera _WorldCamera;                            //世界坐标系
        private Camera _GUICamera;                              //UI坐标系
        private Text _Text;                                     //显示文本

        public float width=1F;
        public float height=1F;
        public float offset = 1.5F;


        //设置UI文本放置的目标
        public void SetTargetObj(GameObject go)
        {
            _TargetObj = go;
        }

        private void Start()
        {
            //获取文本控件
            _Text = this.gameObject.GetComponent<Text>();
            //世界摄像机
            _WorldCamera = Camera.main.gameObject.GetComponent<Camera>();
            //UI摄像机
            _GUICamera = GameObject.FindGameObjectWithTag("Tag_UICamera").GetComponent<Camera>();
            //指定UI文本显示位置
            Pos();
            //参数检查
            if (_TargetObj==null)
            {
                Debug.LogError(GetType()+ "Start()/_TargetObj is null, Please Check!!!");
                return;
            }
           
        }

        private void Update()
        {
            //_Text.transform.localScale = new Vector3(width,height,0);
        }

      

        /// <summary>
        ///动态指定UI文本的显示位置
        /// </summary>
        private void LateUpdate()
        {
            ////指定UI文本显示位置
            //Pos();
        }


        /// <summary>
        /// 指定UI文本显示位置
        /// </summary>
        private void Pos()
        {
            if (_TargetObj != null)
            {
                //获取目标物体的屏幕坐标
                Vector3 pos = _WorldCamera.WorldToScreenPoint(_TargetObj.transform.position);
                //屏幕坐标转换为UI的世界坐标
                pos = _GUICamera.ScreenToWorldPoint(pos);
                //确定UI最终位置
                pos.z = 0;
                this.transform.position = new Vector3(pos.x, pos.y + offset, pos.z);
                //动态调整UI文本的大小
                _Text.transform.localScale = new Vector3(width, height, 0);
            }
        }

    }
}

三、编写控制Text显示的脚本(Test_UIText)
/***
*	Title:"XXX" 项目
*		主题:XXX
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

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

namespace SimpleUIFrame
{
	public class Test_UIText : MonoBehaviour
	{
        public GameObject Panel;                         //UI文本显示的面板
        public GameObject go;

		void Start()
		{
			
		}

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                //加载UI文本预设
                Object OT = Resources.Load("Prefabs/Text");
                GameObject UIText = Instantiate(OT) as GameObject;
                UIText.GetComponent<Text>().text = "这个一个测试显示";
                //指定该UI文本的父物体
                UIText.transform.parent =Panel.transform;
                //指定UI文本显示的目标物体
                UIText.GetComponent<Test_DriftingSpecialEffect>().SetTargetObj(go);
            }
        }
    }
}

四、将关于Text显示标签的脚本(Test_DriftingSpecialEffect)添加给面板中的Text,然后将该Text作为预制体
五、给面板中的_ScriptsMgr添加控制Text显示的脚本(Test_UIText)
六、运行场景,然后开启键盘大写,按下“A”键,即可看到该Text标签显示在Capsule物体上面

猜你喜欢

转载自blog.csdn.net/xiaochenXIHUA/article/details/80597489
今日推荐