|
| 1 | +using UnityEngine; |
| 2 | +using UnityEngine.UI; |
| 3 | +using UnityEngine.EventSystems; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +public class TouchTest : MonoBehaviour, |
| 7 | + IPointerDownHandler, |
| 8 | + IDragHandler, |
| 9 | + IPointerUpHandler, |
| 10 | + IBeginDragHandler, |
| 11 | + IEndDragHandler |
| 12 | +{ |
| 13 | + [SerializeField] private Canvas canvas; // 通过Inspector面板拖拽赋值 |
| 14 | + private Dictionary<int, List<GameObject>> touchCircles = new Dictionary<int, List<GameObject>>(); |
| 15 | + // 移除currentPointerId字段,改为完全依赖touchCircles字典管理所有触点 |
| 16 | + |
| 17 | + void Start() |
| 18 | + { |
| 19 | + Debug.Log("TouchTest脚本初始化开始"); |
| 20 | + |
| 21 | + // 确保Canvas设置正确 |
| 22 | + if (canvas == null) |
| 23 | + { |
| 24 | + Debug.Log("未指定Canvas,尝试自动查找..."); |
| 25 | + canvas = FindObjectOfType<Canvas>(); |
| 26 | + if (canvas == null) |
| 27 | + { |
| 28 | + Debug.LogError("未找到Canvas,请在场景中创建Canvas并拖拽赋值"); |
| 29 | + enabled = false; |
| 30 | + return; |
| 31 | + } |
| 32 | + Debug.Log($"已自动找到Canvas: {canvas.name}"); |
| 33 | + } |
| 34 | + |
| 35 | + // 确保Canvas有GraphicRaycaster组件 |
| 36 | + if (!canvas.TryGetComponent<GraphicRaycaster>(out var raycaster)) |
| 37 | + { |
| 38 | + Debug.Log("Canvas缺少GraphicRaycaster,正在添加..."); |
| 39 | + raycaster = canvas.gameObject.AddComponent<GraphicRaycaster>(); |
| 40 | + Debug.Log($"已添加GraphicRaycaster: {raycaster != null}"); |
| 41 | + } |
| 42 | + |
| 43 | + // 确保EventSystem设置正确 |
| 44 | + EventSystem eventSystem = FindObjectOfType<EventSystem>(); |
| 45 | + if (eventSystem == null) |
| 46 | + { |
| 47 | + Debug.Log("未找到EventSystem,正在创建..."); |
| 48 | + eventSystem = new GameObject("EventSystem").AddComponent<EventSystem>(); |
| 49 | + var inputModule = eventSystem.gameObject.AddComponent<StandaloneInputModule>(); |
| 50 | + Debug.Log($"已创建EventSystem和StandaloneInputModule: {inputModule != null}"); |
| 51 | + } |
| 52 | + else if (eventSystem.GetComponent<StandaloneInputModule>() == null) |
| 53 | + { |
| 54 | + Debug.Log("EventSystem缺少StandaloneInputModule,正在添加..."); |
| 55 | + var inputModule = eventSystem.gameObject.AddComponent<StandaloneInputModule>(); |
| 56 | + Debug.Log($"已添加StandaloneInputModule: {inputModule != null}"); |
| 57 | + } |
| 58 | + |
| 59 | + // 确保脚本挂载在可接收事件的UI对象上 |
| 60 | + if (!TryGetComponent<Graphic>(out var graphic)) |
| 61 | + { |
| 62 | + Debug.Log("当前对象缺少Graphic组件,正在添加透明Image..."); |
| 63 | + var image = gameObject.AddComponent<Image>(); |
| 64 | + // image.color = new Color(0, 0, 0, 0.01f); |
| 65 | + image.raycastTarget = true; // 确保能接收事件 |
| 66 | + Debug.Log($"已添加Image组件,raycastTarget: {image.raycastTarget}"); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + graphic.raycastTarget = true; // 确保现有Graphic能接收事件 |
| 71 | + Debug.Log($"已确保Graphic组件raycastTarget为true: {graphic.raycastTarget}"); |
| 72 | + } |
| 73 | + |
| 74 | + // 确保RectTransform覆盖整个屏幕 |
| 75 | + var rectTransform = GetComponent<RectTransform>(); |
| 76 | + if (rectTransform != null) |
| 77 | + { |
| 78 | + rectTransform.anchorMin = Vector2.zero; |
| 79 | + rectTransform.anchorMax = Vector2.one; |
| 80 | + rectTransform.sizeDelta = Vector2.zero; |
| 81 | + Debug.Log("已设置RectTransform为全屏覆盖"); |
| 82 | + } |
| 83 | + |
| 84 | + Debug.Log("TouchTest脚本初始化完成"); |
| 85 | + } |
| 86 | + |
| 87 | + public void OnPointerDown(PointerEventData eventData) |
| 88 | + { |
| 89 | + Debug.Log($"OnPointerDown - 指针ID: {eventData.pointerId}, 位置: {eventData.position}, 按下按钮: {eventData.button}"); |
| 90 | + HandlePointerEvent(eventData, 1.0f); // 初始点使用完整大小 |
| 91 | + } |
| 92 | + |
| 93 | + public void OnBeginDrag(PointerEventData eventData) |
| 94 | + { |
| 95 | + // 拖动开始时创建新圆点 |
| 96 | + HandlePointerEvent(eventData, 0.5f); // 拖动点使用半大小 |
| 97 | + } |
| 98 | + |
| 99 | + public void OnDrag(PointerEventData eventData) |
| 100 | + { |
| 101 | + // 拖动过程中持续创建新圆点 |
| 102 | + HandlePointerEvent(eventData, 0.5f); |
| 103 | + } |
| 104 | + |
| 105 | + public void OnPointerUp(PointerEventData eventData) |
| 106 | + { |
| 107 | + // 指针抬起时清除对应圆点 |
| 108 | + HandlePointerEnd(eventData); |
| 109 | + } |
| 110 | + |
| 111 | + public void OnEndDrag(PointerEventData eventData) |
| 112 | + { |
| 113 | + // 拖动结束时清除对应圆点 |
| 114 | + HandlePointerEnd(eventData); |
| 115 | + } |
| 116 | + |
| 117 | + void HandlePointerEvent(PointerEventData eventData, float scale) |
| 118 | + { |
| 119 | + Vector2 localPoint; |
| 120 | + RectTransformUtility.ScreenPointToLocalPointInRectangle( |
| 121 | + canvas.transform as RectTransform, |
| 122 | + eventData.position, |
| 123 | + canvas.worldCamera, |
| 124 | + out localPoint); |
| 125 | + |
| 126 | + // 如果是新触点,创建新颜色圆点 |
| 127 | + if (!touchCircles.ContainsKey(eventData.pointerId)) |
| 128 | + { |
| 129 | + touchCircles[eventData.pointerId] = new List<GameObject>(); |
| 130 | + GameObject newCircle = CreateCircle(localPoint, GetRandomColor(), 1.0f); |
| 131 | + touchCircles[eventData.pointerId].Add(newCircle); |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + // 已有触点则使用相同颜色创建新圆点 |
| 136 | + Color circleColor = touchCircles[eventData.pointerId][0].GetComponent<Image>().color; |
| 137 | + GameObject newCircle = CreateCircle(localPoint, circleColor, scale); |
| 138 | + touchCircles[eventData.pointerId].Add(newCircle); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + void HandlePointerEnd(PointerEventData eventData) |
| 143 | + { |
| 144 | + if (touchCircles.ContainsKey(eventData.pointerId)) |
| 145 | + { |
| 146 | + foreach (GameObject circle in touchCircles[eventData.pointerId]) |
| 147 | + { |
| 148 | + Destroy(circle); |
| 149 | + } |
| 150 | + touchCircles.Remove(eventData.pointerId); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + GameObject CreateCircle(Vector2 position, Color color, float scale) |
| 155 | + { |
| 156 | + GameObject circle = new GameObject("Circle"); |
| 157 | + circle.transform.SetParent(canvas.transform, false); |
| 158 | + |
| 159 | + Image image = circle.AddComponent<Image>(); |
| 160 | + image.color = color; |
| 161 | + |
| 162 | + RectTransform rectTransform = circle.GetComponent<RectTransform>(); |
| 163 | + rectTransform.sizeDelta = new Vector2(100, 100) * scale; |
| 164 | + rectTransform.anchoredPosition = position; |
| 165 | + |
| 166 | + return circle; |
| 167 | + } |
| 168 | + |
| 169 | + // 使用静态种子确保跨平台一致性 |
| 170 | + private System.Random _random = new System.Random(); |
| 171 | + |
| 172 | + Color GetRandomColor() |
| 173 | + { |
| 174 | + // 使用System.Random替代Unity的Random.value |
| 175 | + float h = (float)_random.NextDouble(); |
| 176 | + float s = 0.7f + (float)_random.NextDouble() * 0.3f; // 饱和度0.7-1.0 |
| 177 | + float v = 0.8f + (float)_random.NextDouble() * 0.2f; // 明度0.8-1.0 |
| 178 | + |
| 179 | + Color color = Color.HSVToRGB(h, s, v); |
| 180 | + color.a = 0.8f; |
| 181 | + |
| 182 | + // WebGL下强制刷新颜色 |
| 183 | + #if UNITY_WEBGL |
| 184 | + color = new Color(color.r, color.g, color.b, color.a); |
| 185 | + #endif |
| 186 | + |
| 187 | + return color; |
| 188 | + } |
| 189 | +} |
0 commit comments