ModelTopUI.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class ModelTopUI
  5. {
  6. public GameObject TopUI;
  7. public GameObject target;
  8. private string mAssetName;
  9. private Transform TopPos = null;
  10. private Vector3 vL = new Vector3((-1.0f * (float)Screen.width / (float)Screen.height) + 0.13f, 0.0f, 0.0f);
  11. private Vector3 vR = new Vector3(((float)Screen.width / (float)Screen.height) - 0.13f, 0.0f, 0.0f);
  12. private Vector3 linkPos = new Vector3(0,0,0);
  13. private RectTransform mItemIt = null;
  14. public ModelTopUI(GameObject _target, Camera cam,string assetName)
  15. {
  16. mAssetName = assetName;
  17. target = _target;
  18. TopPos = UnityEngineUtils.RecurisiveFindTransformChild(target.transform, "ui_point");
  19. TopUI = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, mAssetName);
  20. TopUI.SetSafeActive(true);
  21. SetTopUI(cam);
  22. SetBattleFlag(false);
  23. }
  24. public void SetLocation(Vector3 offsetPos_)
  25. {
  26. linkPos = offsetPos_;
  27. }
  28. public void SetNameAndLevel(string name,int level)
  29. {
  30. if (TopUI == null) return;
  31. Transform nameTrans = TopUI.transform.Find("Item/name");
  32. if(nameTrans!=null)
  33. {
  34. Text nameLbl = nameTrans.GetComponent<Text>();
  35. if (nameLbl != null)
  36. nameLbl.text = I18N.T(name);
  37. }
  38. Transform lvTrans = TopUI.transform.Find("Item/Lv/val");
  39. if(lvTrans!=null)
  40. {
  41. Text lvLbl = lvTrans.GetComponent<Text>();
  42. if (lvLbl != null)
  43. lvLbl.text = level.ToString();
  44. }
  45. TopUI.name = name;
  46. }
  47. public void SetBattleFlag(bool inBattle)
  48. {
  49. if (TopUI == null) return;
  50. Transform battleNode = TopUI.transform.Find("Item/Battle");
  51. if (battleNode != null)
  52. {
  53. battleNode.gameObject.SetActive(inBattle);
  54. }
  55. }
  56. //头顶事件图标
  57. public void SetTopUI(Camera cam)
  58. {
  59. //处理UI跟随
  60. if (TopUI)
  61. {
  62. Transform trans = TopUI.transform;
  63. trans.SetParent(BattleFlyWordMgr.Instance.HudRootTrans, true);
  64. trans.localScale = Vector3.one;
  65. trans.localRotation = Quaternion.identity;
  66. RectTransform rt = trans.GetComponent<RectTransform>();
  67. rt.anchorMin = Vector2.zero;
  68. rt.anchorMax = Vector2.one;
  69. rt.anchoredPosition3D = Vector3.zero;
  70. rt.offsetMin = rt.offsetMax = Vector2.zero;
  71. Transform item = trans.Find("Item");
  72. if(item!=null)
  73. {
  74. mItemIt = item.GetComponent<RectTransform>();
  75. }
  76. }
  77. }
  78. public void Update(Camera cam)
  79. {
  80. //处理UI跟随
  81. if (TopPos == null || mItemIt == null) return;
  82. Vector3 m_vec3Head = TopPos.position;
  83. m_vec3Head.x = TopPos.position.x + linkPos.x;
  84. m_vec3Head.y = TopPos.position.y + linkPos.y;
  85. m_vec3Head.z = TopPos.position.z + linkPos.z;
  86. Vector3 pos = cam.WorldToViewportPoint(m_vec3Head);
  87. mItemIt.anchoredPosition3D = new Vector3((pos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (pos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
  88. }
  89. public void SetVisible(bool vis)
  90. {
  91. if (TopUI != null)
  92. TopUI.SetSafeActive(vis);
  93. }
  94. public void Dispose()
  95. {
  96. target = null;
  97. if (TopUI)
  98. {
  99. GameObject.Destroy(TopUI);
  100. }
  101. TopUI = null;
  102. }
  103. }