GuideMask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using LuaInterface;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. [ExecuteInEditMode]
  7. public class GuideMask : MonoBehaviour
  8. {
  9. public bool startMask = false;
  10. public float defaultDiameter = 10000;
  11. public float maskTime = .3f;
  12. public Transform target;
  13. public Camera camera;
  14. private Vector4 center;
  15. private Material material;
  16. private Vector2 rectSize;
  17. private Vector2 curSize = Vector2.zero;
  18. private Vector2 tmpSize;
  19. private float curHeight = 0f;
  20. private float curTime = 0;
  21. bool isClose = false;
  22. Image image;
  23. RectTransform maskRect;
  24. LuaTable guideView;
  25. LuaFunction maskCompeletedCB;
  26. void Awake()
  27. {
  28. QualitySettings.vSyncCount = 0;
  29. Application.targetFrameRate = 60;
  30. image = GetComponent<Image>();
  31. material = Object.Instantiate(image.material);
  32. image.material = material;
  33. }
  34. private void OnDestroy()
  35. {
  36. GameObject.Destroy(material);
  37. material = null;
  38. }
  39. public void Init(LuaTable view, LuaFunction callback, RectTransform mask)
  40. {
  41. if (camera == null)
  42. {
  43. camera = CameraMgr.Instance.UICamera;
  44. }
  45. guideView = view;
  46. maskCompeletedCB = callback;
  47. maskRect = mask;
  48. }
  49. public void SetDefaultState(bool defaultState)
  50. {
  51. material.SetInt("_DefaultState", defaultState ? 1 : 0);
  52. }
  53. public void SetTargetInfoClose(Vector3 worldPos, Vector2 size, float maskTime)
  54. {
  55. isClose = true;
  56. this.maskTime = maskTime;
  57. rectSize = size * 0.5f;
  58. Vector3 position = camera.WorldToScreenPoint(worldPos);
  59. position = camera.ScreenToViewportPoint(position);
  60. position.x = (position.x - .5f) * UIMgr.SCREEN_WIDTH;
  61. position.y = (position.y - .5f) * UIMgr.SCREEN_HEIGHT;
  62. center = new Vector4(position.x, position.y, 0f, 0f);
  63. material.SetVector("_Center", center);
  64. curSize.x = defaultDiameter;
  65. curSize.y = defaultDiameter;
  66. material.SetVector("_RectSize", curSize);
  67. material.SetInt("_IsClose", isClose ? 1 :0);
  68. material.SetInt("_DefaultState", 0);
  69. image.SetMaterialDirty();
  70. curTime = 0;
  71. startMask = true;
  72. }
  73. public void SetTargetInfoOpen(Vector3 worldPos, Vector2 size, float maskTime)
  74. {
  75. isClose = false;
  76. this.maskTime = maskTime;
  77. rectSize = size*0.5f;
  78. Vector3 position = camera.WorldToScreenPoint(worldPos);
  79. position = camera.ScreenToViewportPoint(position);
  80. position.x = (position.x - .5f) * UIMgr.SCREEN_WIDTH;
  81. position.y = (position.y - .5f) * UIMgr.SCREEN_HEIGHT;
  82. center = new Vector4(position.x, position.y, 0f, 0f);
  83. material.SetVector("_Center", center);
  84. curSize = rectSize;
  85. material.SetVector("_RectSize", curSize);
  86. material.SetInt("_IsClose", isClose ? 1 : 0);
  87. material.SetInt("_DefaultState", 0);
  88. image.SetMaterialDirty();
  89. curTime = 0;
  90. startMask = true;
  91. }
  92. void Update()
  93. {
  94. if (!startMask) return;
  95. curTime += Time.deltaTime;
  96. float process = (float)curTime / maskTime;
  97. tmpSize.x = isClose ? Mathf.Lerp(curSize.x, rectSize.x, process) : Mathf.Lerp(curSize.x, defaultDiameter, process);
  98. tmpSize.y = isClose ? Mathf.Lerp(curSize.y, rectSize.y, process) : Mathf.Lerp(curSize.y, defaultDiameter, process);
  99. if (maskRect)
  100. {
  101. maskRect.sizeDelta = tmpSize;
  102. }
  103. material.SetVector("_RectSize", tmpSize);
  104. image.SetMaterialDirty();
  105. if (process >= 1 && startMask)
  106. {
  107. startMask = false;
  108. if (maskCompeletedCB != null)
  109. {
  110. if (guideView != null)
  111. maskCompeletedCB.Call(guideView, isClose);
  112. else
  113. maskCompeletedCB.Call(isClose);
  114. }
  115. }
  116. }
  117. public void Dispose()
  118. {
  119. guideView = null;
  120. maskCompeletedCB = null;
  121. maskRect = null;
  122. }
  123. }