using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// 描述:掉落道具名称 /// 作者:WJ /// public class DropItemName : MonoBehaviour { /// /// 自身Transform /// private RectTransform mRectTransform = null; /// /// 父节点RectTransform /// private RectTransform mRootRectTransform = null; private Text nameText = null; private string itemName = ""; private int quality = 0; private Transform target = null; /// /// 初始化 /// /// /// 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(); RefrashName(); } /// /// 刷新名称 /// 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); } /// /// 世界坐标转成UI坐标 /// /// 世界坐标 /// UI坐标 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); } }