using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ItemNameByUI : MonoBehaviour { GameObject nameObj = null; Text nameText = null; string name = ""; int quality = 0; // Start is called before the first frame update void Start() { nameObj = this.transform.Find("UIName/Name").gameObject; nameText = nameObj.transform.Find("Text").GetComponent(); RefrashName(); } // Update is called once per frame void Update() { if(nameObj == null) { return; } nameObj.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + Vector3.up * 0.6f); } public void SetName(string _name,int _quality) { name = _name; quality = _quality; } 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 = name; nameText.color = col; } public Vector2 CalPositionAt(RectTransform from, RectTransform at) { Camera uiCamera = Camera.main; //将from转换到屏幕坐标 Vector2 V2fromInScreen = RectTransformUtility.WorldToScreenPoint(uiCamera, from.transform.position); //将屏幕坐标转换到at的局部坐标中 Vector2 V2InAt; RectTransformUtility.ScreenPointToLocalPointInRectangle(at, V2fromInScreen, uiCamera, out V2InAt); return V2InAt; } }