|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using LitJson; |
| 4 | +using UnityEngine; |
| 5 | +using WeChatWASM; |
| 6 | + |
| 7 | +public class Listen : Details |
| 8 | +{ |
| 9 | + |
| 10 | + private bool _isListeningAccelerometer = false; |
| 11 | + private bool _isListeningCompass = false; |
| 12 | + private bool _isListeningDeviceMotion = false; |
| 13 | + private bool _isPortrait = true; |
| 14 | + private bool _isListeningGyroscope = false; |
| 15 | + |
| 16 | + private readonly Action<OnAccelerometerChangeListenerResult> _onAccelerometerChange = (res) => { |
| 17 | + var result = "onAccelerometerChange\n" + JsonMapper.ToJson(res); |
| 18 | + GameManager.Instance.detailsController.AddResult(new ResultData() |
| 19 | + { |
| 20 | + initialContentText = result |
| 21 | + }); |
| 22 | + }; |
| 23 | + |
| 24 | + private readonly Action<OnCompassChangeListenerResult> _onCompassChange = (res) => { |
| 25 | + var result = "onCompassChange\n" + JsonMapper.ToJson(res); |
| 26 | + GameManager.Instance.detailsController.AddResult(new ResultData() |
| 27 | + { |
| 28 | + initialContentText = result |
| 29 | + }); |
| 30 | + }; |
| 31 | + |
| 32 | + private readonly Action<OnDeviceMotionChangeListenerResult> _onDeviceMotionChange = (res) => { |
| 33 | + var result = "onDeviceMotionChange\n" + JsonMapper.ToJson(res); |
| 34 | + GameManager.Instance.detailsController.AddResult(new ResultData() |
| 35 | + { |
| 36 | + initialContentText = result |
| 37 | + }); |
| 38 | + }; |
| 39 | + |
| 40 | + private readonly Action<OnDeviceOrientationChangeListenerResult> _onDeviceOrientationChange = (res) => { |
| 41 | + var result = "onDeviceOrientationChange\n" + JsonMapper.ToJson(res); |
| 42 | + GameManager.Instance.detailsController.AddResult(new ResultData() |
| 43 | + { |
| 44 | + initialContentText = result |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + private readonly Action<OnGyroscopeChangeListenerResult> _onGyroscopeChange = (res) => { |
| 49 | + var result = "onGyroscopeChange\n" + JsonMapper.ToJson(res); |
| 50 | + GameManager.Instance.detailsController.AddResult(new ResultData() |
| 51 | + { |
| 52 | + initialContentText = result |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + private void Start() |
| 57 | + { |
| 58 | + // 监听转屏事件 |
| 59 | + WX.OnDeviceOrientationChange(_onDeviceOrientationChange); |
| 60 | + |
| 61 | + GameManager.Instance.detailsController.BindExtraButtonAction(0, Compass); |
| 62 | + GameManager.Instance.detailsController.BindExtraButtonAction(1, DeviceMotion); |
| 63 | + GameManager.Instance.detailsController.BindExtraButtonAction(2, DeviceOrientation); |
| 64 | + GameManager.Instance.detailsController.BindExtraButtonAction(3, Gyroscope); |
| 65 | + } |
| 66 | + |
| 67 | + // 测试 API |
| 68 | + protected override void TestAPI(string[] args) |
| 69 | + { |
| 70 | + Accelerometer(); |
| 71 | + } |
| 72 | + |
| 73 | + public void Accelerometer() { |
| 74 | + if (!_isListeningAccelerometer) { |
| 75 | + WX.StartAccelerometer(new StartAccelerometerOption |
| 76 | + { |
| 77 | + interval = "normal", |
| 78 | + success = (res) => { |
| 79 | + WX.ShowToast(new ShowToastOption() |
| 80 | + { |
| 81 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 82 | + }); |
| 83 | + }, |
| 84 | + fail = (res) => { |
| 85 | + Debug.Log("fail" + res.errMsg); |
| 86 | + }, |
| 87 | + complete = (res) => { |
| 88 | + Debug.Log("complete"); |
| 89 | + } |
| 90 | + }); |
| 91 | + WX.OnAccelerometerChange(_onAccelerometerChange); |
| 92 | + } else { |
| 93 | + WX.StopAccelerometer(new StopAccelerometerOption |
| 94 | + { |
| 95 | + success = (res) => { |
| 96 | + WX.ShowToast(new ShowToastOption() |
| 97 | + { |
| 98 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 99 | + }); |
| 100 | + }, |
| 101 | + fail = (res) => { |
| 102 | + Debug.Log("fail" + res.errMsg); |
| 103 | + }, |
| 104 | + complete = (res) => { |
| 105 | + Debug.Log("complete"); |
| 106 | + } |
| 107 | + }); |
| 108 | + WX.OffAccelerometerChange(_onAccelerometerChange); |
| 109 | + } |
| 110 | + _isListeningAccelerometer = !_isListeningAccelerometer; |
| 111 | + GameManager.Instance.detailsController.ChangeInitialButtonText(_isListeningAccelerometer ? "取消监听加速度" : "开始监听加速度"); |
| 112 | + } |
| 113 | + |
| 114 | + public void Compass() { |
| 115 | + if (!_isListeningCompass) { |
| 116 | + WX.StartCompass(new StartCompassOption |
| 117 | + { |
| 118 | + success = (res) => { |
| 119 | + WX.ShowToast(new ShowToastOption() |
| 120 | + { |
| 121 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 122 | + }); |
| 123 | + }, |
| 124 | + fail = (res) => { |
| 125 | + Debug.Log("fail" + res.errMsg); |
| 126 | + }, |
| 127 | + complete = (res) => { |
| 128 | + Debug.Log("complete"); |
| 129 | + } |
| 130 | + }); |
| 131 | + WX.OnCompassChange(_onCompassChange); |
| 132 | + } else { |
| 133 | + WX.StopCompass(new StopCompassOption |
| 134 | + { |
| 135 | + success = (res) => { |
| 136 | + WX.ShowToast(new ShowToastOption() |
| 137 | + { |
| 138 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 139 | + }); |
| 140 | + }, |
| 141 | + fail = (res) => { |
| 142 | + Debug.Log("fail" + res.errMsg); |
| 143 | + }, |
| 144 | + complete = (res) => { |
| 145 | + Debug.Log("complete"); |
| 146 | + } |
| 147 | + }); |
| 148 | + WX.OffCompassChange(_onCompassChange); |
| 149 | + } |
| 150 | + _isListeningCompass = !_isListeningCompass; |
| 151 | + GameManager.Instance.detailsController.ChangeExtraButtonText(0, _isListeningCompass ? "取消监听罗盘" : "开始监听罗盘"); |
| 152 | + } |
| 153 | + |
| 154 | + public void DeviceMotion() { |
| 155 | + if (!_isListeningDeviceMotion) { |
| 156 | + WX.StartDeviceMotionListening(new StartDeviceMotionListeningOption |
| 157 | + { |
| 158 | + interval = "normal", |
| 159 | + success = (res) => { |
| 160 | + WX.ShowToast(new ShowToastOption() |
| 161 | + { |
| 162 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 163 | + }); |
| 164 | + }, |
| 165 | + fail = (res) => { |
| 166 | + Debug.Log("fail" + res.errMsg); |
| 167 | + }, |
| 168 | + complete = (res) => { |
| 169 | + Debug.Log("complete"); |
| 170 | + } |
| 171 | + }); |
| 172 | + WX.OnDeviceMotionChange(_onDeviceMotionChange); |
| 173 | + } else { |
| 174 | + WX.StopDeviceMotionListening(new StopDeviceMotionListeningOption |
| 175 | + { |
| 176 | + success = (res) => { |
| 177 | + WX.ShowToast(new ShowToastOption() |
| 178 | + { |
| 179 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 180 | + }); |
| 181 | + }, |
| 182 | + fail = (res) => { |
| 183 | + Debug.Log("fail" + res.errMsg); |
| 184 | + }, |
| 185 | + complete = (res) => { |
| 186 | + Debug.Log("complete"); |
| 187 | + } |
| 188 | + }); |
| 189 | + WX.OffDeviceMotionChange(_onDeviceMotionChange); |
| 190 | + } |
| 191 | + _isListeningDeviceMotion = !_isListeningDeviceMotion; |
| 192 | + GameManager.Instance.detailsController.ChangeExtraButtonText(1, _isListeningDeviceMotion ? "取消监听设备方向" : "开始监听设备方向"); |
| 193 | + } |
| 194 | + |
| 195 | + public void DeviceOrientation() { |
| 196 | + WX.SetDeviceOrientation(new SetDeviceOrientationOption |
| 197 | + { |
| 198 | + value = !_isPortrait ? "portrait" : "landscape", |
| 199 | + success = (res) => { |
| 200 | + Debug.Log("success"); |
| 201 | + }, |
| 202 | + fail = (res) => { |
| 203 | + Debug.Log("fail" + res.errMsg); |
| 204 | + }, |
| 205 | + complete = (res) => { |
| 206 | + Debug.Log("complete"); |
| 207 | + } |
| 208 | + }); |
| 209 | + _isPortrait = !_isPortrait; |
| 210 | + } |
| 211 | + |
| 212 | + public void Gyroscope() { |
| 213 | + if (!_isListeningGyroscope) { |
| 214 | + WX.StartGyroscope(new StartGyroscopeOption |
| 215 | + { |
| 216 | + interval = "normal", |
| 217 | + success = (res) => { |
| 218 | + WX.ShowToast(new ShowToastOption() |
| 219 | + { |
| 220 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 221 | + }); |
| 222 | + }, |
| 223 | + fail = (res) => { |
| 224 | + Debug.Log("fail" + res.errMsg); |
| 225 | + }, |
| 226 | + complete = (res) => { |
| 227 | + Debug.Log("complete"); |
| 228 | + } |
| 229 | + }); |
| 230 | + WX.OnGyroscopeChange(_onGyroscopeChange); |
| 231 | + } else { |
| 232 | + WX.StopGyroscope(new StopGyroscopeOption |
| 233 | + { |
| 234 | + success = (res) => { |
| 235 | + WX.ShowToast(new ShowToastOption() |
| 236 | + { |
| 237 | + title = "Access Success, Result: " + JsonMapper.ToJson(res) |
| 238 | + }); |
| 239 | + }, |
| 240 | + fail = (res) => { |
| 241 | + Debug.Log("fail" + res.errMsg); |
| 242 | + }, |
| 243 | + complete = (res) => { |
| 244 | + Debug.Log("complete"); |
| 245 | + } |
| 246 | + }); |
| 247 | + WX.OffGyroscopeChange(_onGyroscopeChange); |
| 248 | + } |
| 249 | + _isListeningGyroscope = !_isListeningGyroscope; |
| 250 | + GameManager.Instance.detailsController.ChangeExtraButtonText(3, _isListeningGyroscope ? "取消监听陀螺仪" : "开始监听陀螺仪"); |
| 251 | + } |
| 252 | +} |
| 253 | + |
0 commit comments