using System; using System.Runtime.InteropServices; using System.Collections.Generic; namespace WeChatWASM { public class WXUserInfoButton { private readonly string instanceId; private Action onTopCallback; public static Dictionary Dict = new Dictionary(); #region C#调用JS桥接方法 #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXUserInfoButtonDestroy(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXUserInfoButtonHide(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXUserInfoButtonOffTap(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXUserInfoButtonOnTap(string id); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void WXUserInfoButtonShow(string id); #endregion public WXUserInfoButton(string id) { instanceId = id; } public void InvokeCallback(WXUserInfoResponse res) { onTopCallback?.Invoke(res); } #region 调用方法 /// /// 销毁用户信息按钮 /// public void Destroy() { WXUserInfoButtonDestroy(instanceId); if (Dict.ContainsKey(instanceId)) { Dict.Remove(instanceId); } } /// /// 隐藏用户信息按钮。 /// public void Hide() { WXUserInfoButtonHide(instanceId); } /// /// 取消监听用户信息按钮的点击事件 /// public void OffTap() { WXUserInfoButtonOffTap(instanceId); onTopCallback = null; } /// /// 监听用户信息按钮的点击事件 /// /// public void OnTap(Action action) { onTopCallback = action; WXUserInfoButtonOnTap(instanceId); } /// /// 显示用户信息按钮 /// public void Show() { WXUserInfoButtonShow(instanceId); } #endregion } }