123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// 描述:掉落道具名称
- /// 作者:WJ
- /// </summary>
- public class DropItemName : MonoBehaviour
- {
- /// <summary>
- /// 自身Transform
- /// </summary>
- private RectTransform mRectTransform = null;
- /// <summary>
- /// 父节点RectTransform
- /// </summary>
- private RectTransform mRootRectTransform = null;
- private Text nameText = null;
- private string itemName = "";
- private int quality = 0;
- private Transform target = null;
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="role"></param>
- /// <param name="val"></param>
- public void Init(RectTransform rect, Transform _target, string _name, int _quality)
- {
- mRootRectTransform = rect;
- mRectTransform = transform as RectTransform;
- itemName = _name;
- quality = _quality;
- target = _target;
- nameText = transform.Find("Text").GetComponent<Text>();
- RefrashName();
- }
- /// <summary>
- /// 刷新名称
- /// </summary>
- private void RefrashName()
- {
- Color col = Color.white;
- switch (quality)
- {
- case 0:
- case 1:
- break;
- case 2:
- col = new Color(0x1B / 255.0f, 0xFF / 255.0f, 0x00 / 255.0f);
- break;
- case 3:
- col = new Color(0x00 / 255.0f, 0x35 / 255.0f, 0xFF / 255.0f);
- break;
- case 4:
- col = new Color(0xC2 / 255.0f, 0x00 / 255.0f, 0xFF / 255.0f);
- break;
- case 5:
- col = new Color(0xFF / 255.0f, 0x8D / 255.0f, 0x00 / 255.0f);
- break;
- }
- nameText.text = itemName;
- nameText.color = col;
- }
- // Update is called once per frame
- void Update()
- {
- if (target == null)
- {
- Destroy(gameObject);
- return;
- }
- mRectTransform.anchoredPosition = World2UI(target.position + Vector3.up * 0.5f);
- }
- /// <summary>
- /// 世界坐标转成UI坐标
- /// </summary>
- /// <param name="wpos">世界坐标</param>
- /// <returns>UI坐标</returns>
- public Vector2 World2UI(Vector3 wpos)
- {
- if (Camera.main == null)
- {
- return Vector2.zero;
- }
- //世界坐标 -> ViewPort坐标
- Vector2 viewPos = Camera.main.WorldToViewportPoint(wpos);
- //ViewPort坐标 -> UGUI坐标
- return new Vector2(mRootRectTransform.rect.width * viewPos.x, mRootRectTransform.rect.height * viewPos.y);
- }
- }
|