Skip to content

Commit bc72699

Browse files
committed
设备相关api-demo
1 parent 6de7682 commit bc72699

File tree

15 files changed

+1372
-41
lines changed

15 files changed

+1372
-41
lines changed

Demo/API_V2/Assets/API/Facility/BatteryAndClipboard/BatteryAndClipboard.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void getBatteryInfo() {
3232
WX.GetBatteryInfo(new GetBatteryInfoOption
3333
{
3434
success = (res) => {
35-
Debug.Log("success" + res);
35+
Debug.Log("success" + JsonUtility.ToJson(res));
3636
},
3737
fail = (res) => {
3838
Debug.Log("fail" + res.errMsg);
@@ -66,7 +66,7 @@ public void setClipboardData() {
6666
{
6767
data = "123",
6868
success = (res) => {
69-
Debug.Log("success" + res);
69+
Debug.Log(JsonUtility.ToJson(res));
7070
},
7171
fail = (res) => {
7272
Debug.Log("fail" + res.errMsg);

Demo/API_V2/Assets/API/Facility/BatteryAndClipboard/BatteryAndClipboardSO.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MonoBehaviour:
1515
entryScriptTypeName: BatteryAndClipboard
1616
entryName: "\u7535\u91CF\u548C\u526A\u5207\u677F"
1717
entryAPI: "\u7535\u91CF\u548C\u526A\u5207\u677F\u76F8\u5173API"
18-
entryDescription:
18+
entryDescription: "\u526A\u5207\u7248\u9700\u8981\u76F8\u5173\u6743\u9650"
1919
optionList: []
2020
initialButtonText: "\u83B7\u53D6\u8BBE\u5907\u7535\u91CF-\u540C\u6B65"
2121
extraButtonList:

Demo/API_V2/Assets/API/Facility/Beacon/Beacon.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void startBeaconDiscovery() {
5151
{
5252
uuids = new string[] {"xxxxxxxxxx"},
5353
success = (res) => {
54-
Debug.Log(res);
54+
Debug.Log(JsonUtility.ToJson(res));
5555
},
5656
fail = (res) => {
5757
Debug.Log("fail" + res.errMsg);
@@ -66,7 +66,7 @@ public void stopBeaconDiscovery() {
6666
WX.StopBeaconDiscovery(new StopBeaconDiscoveryOption
6767
{
6868
success = (res) => {
69-
Debug.Log(res);
69+
Debug.Log(JsonUtility.ToJson(res));
7070
},
7171
fail = (res) => {
7272
Debug.Log("fail" + res.errMsg);
@@ -81,7 +81,7 @@ public void getBeacons() {
8181
WX.GetBeacons(new GetBeaconsOption
8282
{
8383
success = (res) => {
84-
Debug.Log(res);
84+
Debug.Log(JsonUtility.ToJson(res));
8585
},
8686
fail = (res) => {
8787
Debug.Log("fail" + res.errMsg);

Demo/API_V2/Assets/API/Facility/FacilitySO.asset

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ MonoBehaviour:
1818
- {fileID: 11400000, guid: a24af43ae51914d26b0bf637c283dab2, type: 2}
1919
- {fileID: 11400000, guid: 4eba2c9f2f2064e4081be74302ca4c33, type: 2}
2020
- {fileID: 11400000, guid: 3bf339b994c544db3860ddf0a0dc8e20, type: 2}
21+
- {fileID: 11400000, guid: e92d94924be504167a342e42e1162f21, type: 2}
2122
- {fileID: 11400000, guid: 8b80b0ecb1eb545fc9e1d3e78be71c1c, type: 2}
2223
- {fileID: 11400000, guid: 8d65ca57ec29f4e0181f0a9eea07242c, type: 2}
24+
- {fileID: 11400000, guid: 4cf8b37be958f454fb84e1af3578b3ef, type: 2}
2325
- {fileID: 11400000, guid: 4bf09276222714a97880851f19636612, type: 2}

Demo/API_V2/Assets/API/Facility/Listen.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+

Demo/API_V2/Assets/API/Facility/Listen/Listen.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
13+
m_Name: ListenSO
14+
m_EditorClassIdentifier:
15+
entryScriptTypeName: Listen
16+
entryName: "\u8BBE\u5907\u76D1\u542C"
17+
entryAPI: "\u52A0\u901F\u8BA1\uFF0C\u7F57\u76D8\uFF0C\u8BBE\u5907\u65B9\u5411\uFF0C\u8F6C\u5C4F\u548C\u9640\u87BA\u4EEA\u76F8\u5173api"
18+
entryDescription:
19+
optionList: []
20+
initialButtonText: "\u5F00\u59CB\u76D1\u542C\u52A0\u901F\u5EA6"
21+
extraButtonList:
22+
- buttonText: "\u5F00\u59CB\u76D1\u542C\u7F57\u76D8"
23+
- buttonText: "\u5F00\u59CB\u76D1\u542C\u8BBE\u5907\u65B9\u5411"
24+
- buttonText: "\u6539\u53D8\u8BBE\u5907\u65B9\u5411"
25+
- buttonText: "\u5F00\u59CB\u76D1\u542C\u9640\u87BA\u4EEA"
26+
initialResultList: []

Demo/API_V2/Assets/API/Facility/Listen/ListenSO.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Demo/API_V2/Assets/API/Facility/Screen.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)