Skip to content

Commit f1add64

Browse files
committed
数据缓存-demo
1 parent 7f81dbe commit f1add64

File tree

9 files changed

+1311
-91
lines changed

9 files changed

+1311
-91
lines changed

Demo/API_V2/Assets/API/DataStorage.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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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: 4a9e19ed69c041c408533b50f5572d0f, type: 3}
13+
m_Name: DataStorageSO
14+
m_EditorClassIdentifier:
15+
categoryName: "\u6570\u636E\u7F13\u5B58"
16+
categorySprite: {fileID: 0}
17+
entryList:
18+
- {fileID: 11400000, guid: a587889226c1f491fa6c783db1e12837, type: 2}

Demo/API_V2/Assets/API/DataStorage/DataStorageSO.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/DataStorage/Storage.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: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
using LitJson;
2+
using UnityEngine;
3+
using WeChatWASM;
4+
using System;
5+
// 缓存数据类型
6+
[System.Serializable]
7+
public class Data
8+
{
9+
public string data1;
10+
public int data2;
11+
}
12+
13+
public class Storage : Details
14+
{
15+
private readonly Action<OnBackgroundFetchDataListenerResult> _onBackgroundFetchData = (res) => {
16+
var result = "onBackgroundFetchData\n" + JsonMapper.ToJson(res);
17+
GameManager.Instance.detailsController.AddResult(new ResultData()
18+
{
19+
initialContentText = result
20+
});
21+
};
22+
23+
private void Start()
24+
{
25+
// 监听收到 backgroundFetch 数据事件。
26+
WX.OnBackgroundFetchData(_onBackgroundFetchData);
27+
28+
29+
GameManager.Instance.detailsController.BindExtraButtonAction(0, getStorage);
30+
GameManager.Instance.detailsController.BindExtraButtonAction(1, removeStorage);
31+
GameManager.Instance.detailsController.BindExtraButtonAction(2, removeStorageSync);
32+
GameManager.Instance.detailsController.BindExtraButtonAction(3, getStorageInfoSync);
33+
GameManager.Instance.detailsController.BindExtraButtonAction(4, getStorageInfo);
34+
GameManager.Instance.detailsController.BindExtraButtonAction(5, setBackgroundFetchToken);
35+
GameManager.Instance.detailsController.BindExtraButtonAction(6, getBackgroundFetchToken);
36+
GameManager.Instance.detailsController.BindExtraButtonAction(7, getBackgroundFetchData);
37+
}
38+
39+
// 测试 API
40+
protected override void TestAPI(string[] args)
41+
{
42+
setStorageSync();
43+
}
44+
45+
public void setStorageSync() {
46+
var d = new Data {
47+
data1 = "test",
48+
data2 = 1
49+
};
50+
PlayerPrefs.SetString("test1", JsonUtility.ToJson(d));
51+
PlayerPrefs.Save();
52+
WX.ShowToast(new ShowToastOption
53+
{
54+
title = "设置成功"
55+
});
56+
}
57+
58+
59+
public void removeStorageSync() {
60+
WX.RemoveStorageSync("test1");
61+
WX.ShowToast(new ShowToastOption
62+
{
63+
title = "删除test1成功"
64+
});
65+
}
66+
67+
public void removeStorage() {
68+
WX.RemoveStorage(new RemoveStorageOption
69+
{
70+
key = "test2",
71+
success = (res) => {
72+
WX.ShowToast(new ShowToastOption
73+
{
74+
title = "删除test2成功"
75+
});
76+
},
77+
fail = (res) => {
78+
Debug.Log("fail: " + res.errMsg);
79+
},
80+
complete = (res) => {
81+
Debug.Log("complete");
82+
}
83+
});
84+
}
85+
86+
public void getStorageInfoSync() {
87+
GetStorageInfoSyncOption res = WX.GetStorageInfoSync();
88+
WX.ShowModal(new ShowModalOption
89+
{
90+
content = JsonMapper.ToJson(res)
91+
});
92+
}
93+
94+
public void getStorageInfo() {
95+
WX.GetStorageInfo(new GetStorageInfoOption
96+
{
97+
success = (res) => {
98+
WX.ShowModal(new ShowModalOption
99+
{
100+
content = JsonMapper.ToJson(res)
101+
});
102+
},
103+
fail = (res) => {
104+
Debug.Log("fail: " + res.errMsg);
105+
},
106+
complete = (res) => {
107+
Debug.Log("complete");
108+
}
109+
});
110+
}
111+
112+
public void getStorage() {
113+
var res = PlayerPrefs.GetString("test1");
114+
Debug.Log("playerperfs: " + res);
115+
116+
WX.ShowModal(new ShowModalOption
117+
{
118+
content = res
119+
});
120+
}
121+
122+
public void setBackgroundFetchToken() {
123+
WX.SetBackgroundFetchToken(new SetBackgroundFetchTokenOption
124+
{
125+
token = "abcdefghijklmn",
126+
success = (res) => {
127+
WX.ShowToast(new ShowToastOption
128+
{
129+
title = "设置成功"
130+
});
131+
},
132+
fail = (res) => {
133+
Debug.Log("fail: " + res.errMsg);
134+
},
135+
complete = (res) => {
136+
Debug.Log("complete");
137+
}
138+
});
139+
}
140+
141+
public void getBackgroundFetchToken() {
142+
WX.GetBackgroundFetchToken(new GetBackgroundFetchTokenOption
143+
{
144+
success = (res) => {
145+
WX.ShowModal(new ShowModalOption
146+
{
147+
content = JsonMapper.ToJson(res)
148+
});
149+
},
150+
fail = (res) => {
151+
Debug.Log("fail: " + res.errMsg);
152+
},
153+
complete = (res) => {
154+
Debug.Log("complete");
155+
}
156+
});
157+
}
158+
159+
public void getBackgroundFetchData() {
160+
WX.GetBackgroundFetchData(new GetBackgroundFetchDataOption
161+
{
162+
fetchType = "pre",
163+
success = (res) => {
164+
WX.ShowModal(new ShowModalOption
165+
{
166+
content = JsonMapper.ToJson(res)
167+
});
168+
},
169+
fail = (res) => {
170+
Debug.Log("fail: " + res.errMsg);
171+
},
172+
complete = (res) => {
173+
Debug.Log("complete");
174+
}
175+
});
176+
}
177+
}
178+

Demo/API_V2/Assets/API/DataStorage/Storage/Storage.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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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: StorageSO
14+
m_EditorClassIdentifier:
15+
entryScriptTypeName: Storage
16+
entryName: "\u6570\u636E\u7F13\u5B58"
17+
entryAPI: "\u6570\u636E\u7F13\u5B58\u76F8\u5173API"
18+
entryDescription:
19+
optionList: []
20+
initialButtonText: "\u6570\u636E\u7F13\u5B58"
21+
extraButtonList:
22+
- buttonText: "\u67E5\u770B\u7F13\u5B58\u6570\u636E"
23+
- buttonText: "\u79FB\u9664\u6307\u5B9A key-\u5F02\u6B65"
24+
- buttonText: "\u79FB\u9664\u6307\u5B9A key-\u540C\u6B65"
25+
- buttonText: "\u83B7\u53D6\u5F53\u524Dstorage\u4FE1\u606F-\u540C\u6B65"
26+
- buttonText: "\u83B7\u53D6\u5F53\u524Dstorage\u4FE1\u606F-\u5F02\u6B65"
27+
- buttonText: "\u8BBE\u7F6E\u81EA\u5B9A\u4E49\u767B\u5F55\u6001"
28+
- buttonText: "\u83B7\u53D6\u81EA\u5B9A\u4E49\u767B\u5F55\u6001"
29+
- buttonText: "\u62C9\u53D6 backgroundFetch \u5BA2\u6237\u7AEF\u7F13\u5B58\u6570\u636E"
30+
initialResultList: []

Demo/API_V2/Assets/API/DataStorage/Storage/StorageSO.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.

0 commit comments

Comments
 (0)