ItemNameByUI.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ItemNameByUI : MonoBehaviour
  6. {
  7. GameObject nameObj = null;
  8. Text nameText = null;
  9. string name = "";
  10. int quality = 0;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. nameObj = this.transform.Find("UIName/Name").gameObject;
  15. nameText = nameObj.transform.Find("Text").GetComponent<Text>();
  16. RefrashName();
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. if(nameObj == null)
  22. {
  23. return;
  24. }
  25. nameObj.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + Vector3.up * 0.6f);
  26. }
  27. public void SetName(string _name,int _quality)
  28. {
  29. name = _name;
  30. quality = _quality;
  31. }
  32. private void RefrashName()
  33. {
  34. Color col = Color.white;
  35. switch (quality)
  36. {
  37. case 0:
  38. case 1:
  39. break;
  40. case 2:
  41. col = new Color(0x1B / 255.0f, 0xFF / 255.0f, 0x00 / 255.0f);
  42. break;
  43. case 3:
  44. col = new Color(0x00 / 255.0f, 0x35 / 255.0f, 0xFF / 255.0f);
  45. break;
  46. case 4:
  47. col = new Color(0xC2 / 255.0f, 0x00 / 255.0f, 0xFF / 255.0f);
  48. break;
  49. case 5:
  50. col = new Color(0xFF / 255.0f, 0x8D / 255.0f, 0x00 / 255.0f);
  51. break;
  52. }
  53. nameText.text = name;
  54. nameText.color = col;
  55. }
  56. public Vector2 CalPositionAt(RectTransform from, RectTransform at)
  57. {
  58. Camera uiCamera = Camera.main;
  59. //将from转换到屏幕坐标
  60. Vector2 V2fromInScreen = RectTransformUtility.WorldToScreenPoint(uiCamera, from.transform.position);
  61. //将屏幕坐标转换到at的局部坐标中
  62. Vector2 V2InAt;
  63. RectTransformUtility.ScreenPointToLocalPointInRectangle(at, V2fromInScreen, uiCamera, out V2InAt);
  64. return V2InAt;
  65. }
  66. }