#if UNITY_WEBGL || WEIXINMINIGAME || UNITY_EDITOR using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.UI; using WeChatWASM; using Touch = UnityEngine.Touch; internal class TouchData { public Touch touch; public long timeStamp; } /** * 由于Unity WebGL发布的多点触控存在问题, 导致在微信中多点触控存在粘连的情况 * 所以需要使用WX的触控接口重新覆盖Unity的BaseInput关于触控方面的接口 * 通过设置StandaloneInputModule.inputOverride的方式来实现 */ [RequireComponent(typeof(StandaloneInputModule))] public class WXTouchInputOverride : BaseInput { private bool _isInitWechatSDK; private readonly List _touches = new List(); private StandaloneInputModule _standaloneInputModule = null; protected override void Awake() { base.Awake(); _standaloneInputModule = GetComponent(); } protected override void OnEnable() { base.OnEnable(); if (string.IsNullOrEmpty(WeChatWASM.WX.GetSystemInfoSync().platform)) return; InitWechatTouchEvents(); if (_standaloneInputModule) { _standaloneInputModule.inputOverride = this; } } protected override void OnDisable() { base.OnDisable(); UnregisterWechatTouchEvents(); if (_standaloneInputModule) { _standaloneInputModule.inputOverride = null; } } private void InitWechatTouchEvents() { if (!_isInitWechatSDK) { WX.InitSDK((code) => { _isInitWechatSDK = true; RegisterWechatTouchEvents(); }); } else { RegisterWechatTouchEvents(); } } private void RegisterWechatTouchEvents() { WX.OnTouchStart(OnWxTouchStart); WX.OnTouchMove(OnWxTouchMove); WX.OnTouchEnd(OnWxTouchEnd); WX.OnTouchCancel(OnWxTouchCancel); } private void UnregisterWechatTouchEvents() { WX.OffTouchStart(OnWxTouchStart); WX.OffTouchMove(OnWxTouchMove); WX.OffTouchEnd(OnWxTouchEnd); WX.OffTouchCancel(OnWxTouchCancel); } private void OnWxTouchStart(OnTouchStartListenerResult touchEvent) { foreach (var wxTouch in touchEvent.changedTouches) { var data = FindOrCreateTouchData(wxTouch.identifier); data.touch.phase = TouchPhase.Began; data.touch.position = new Vector2(wxTouch.clientX, wxTouch.clientY); data.touch.rawPosition = data.touch.position; data.timeStamp = touchEvent.timeStamp; // Debug.Log($"OnWxTouchStart:{wxTouch.identifier}, {data.touch.phase}"); } } private void OnWxTouchMove(OnTouchStartListenerResult touchEvent) { foreach (var wxTouch in touchEvent.changedTouches) { var data = FindOrCreateTouchData(wxTouch.identifier); UpdateTouchData(data, new Vector2(wxTouch.clientX, wxTouch.clientY), touchEvent.timeStamp, TouchPhase.Moved); } } private void OnWxTouchEnd(OnTouchStartListenerResult touchEvent) { foreach (var wxTouch in touchEvent.changedTouches) { TouchData data = FindTouchData(wxTouch.identifier); if (data == null) { Debug.LogError($"OnWxTouchEnd, error identifier:{wxTouch.identifier}"); return; } if (data.touch.phase == TouchPhase.Canceled || data.touch.phase == TouchPhase.Ended) { Debug.LogWarning($"OnWxTouchEnd, error phase:{wxTouch.identifier}, phase:{data.touch.phase}"); } // Debug.Log($"OnWxTouchEnd:{wxTouch.identifier}"); UpdateTouchData(data, new Vector2(wxTouch.clientX, wxTouch.clientY), touchEvent.timeStamp, TouchPhase.Ended); } GameObject selectedObject = EventSystem.current.currentSelectedGameObject; if (selectedObject != null) { Button button = selectedObject.GetComponent