Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

整理onShareAppMessage示例 #949

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/GUI/Menu/MenuSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ MonoBehaviour:
- buttonText: "\u83B7\u53D6\u83DC\u5355\u6309\u94AE\u5E03\u5C40\u4F4D\u7F6E\u4FE1\u606F"
- buttonText: "\u8BBE\u7F6E\u72B6\u6001\u680F\u6837\u5F0F"
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Media/CameraAPI/CameraAPISO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ MonoBehaviour:
initialButtonText: "\u5F00\u59CB\u76D1\u542C\u5E27\u6570\u636E"
extraButtonList: []
initialResultList: []
entryOrder: 0
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ MonoBehaviour:
- buttonText: "\u8DF3\u8F6C"
- buttonText: "\u83B7\u5F97\u4E0B\u4E00\u5E27"
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/Download/DownloadSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
initialButtonText: "\u4E0B\u8F7D"
extraButtonList: []
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/TCPSocket/TCPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ MonoBehaviour:
- buttonText: "\u53D1\u9001"
- buttonText: "\u5173\u95ED"
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/UDPSocket/UDPSocketSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ MonoBehaviour:
- buttonText: Send
- buttonText: "\u5173\u95ED"
initialResultList: []
entryOrder: 0
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ MonoBehaviour:
- buttonText: Post
- buttonText: Get
initialResultList: []
entryOrder: 0
1 change: 1 addition & 0 deletions Demo/API_V2/Assets/API/Network/Upload/UploadSO.asset
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ MonoBehaviour:
initialButtonText: "\u4E0A\u4F20\u6587\u4EF6"
extraButtonList: []
initialResultList: []
entryOrder: 0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions Demo/API_V2/Assets/API/Share/Favorites/Favorites.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using WeChatWASM;
using System;
using UnityEngine;

public class Favorites : Details
{
private bool _isListeningAddToFavorites = false;
private Action<Action<OnAddToFavoritesListenerResult>> _onAddToFavorites;
private string localImagePath;

protected override void TestAPI(string[] args)
{
// 如果已经在监听中,只执行切换监听的操作,避免重复下载
if (_isListeningAddToFavorites)
{
onAddToFavorites();
return;
}

// 根据对应参数,执行DownloadFileImage()下载图片
if (GetOptionString(1, "") == "本地图片文件路径")
{
DownloadFileImage();
}
else
{
InitializeFavoritesCallback();
onAddToFavorites();
}
}

private void ShowLoading()
{
WX.ShowLoading(new ShowLoadingOption()
{
title = "正在下载图片...",
mask = true
});
}

private void HideLoading()
{
WX.HideLoading(new HideLoadingOption());
}

private void DownloadFileImage()
{
ShowLoading();
WX.DownloadFile(new DownloadFileOption()
{
url = "https://picsum.photos/400/400?random=1",
success = (res) =>
{
Debug.Log("WX.DownloadFile success");
if (res.statusCode == 200)
{
Debug.Log(res.tempFilePath);
var fs = WX.GetFileSystemManager();
// 将临时文件保存为本地缓存文件
localImagePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/favoriteImage.jpg");
Debug.Log($"本地缓存文件保存路径: {localImagePath}");
InitializeFavoritesCallback();
onAddToFavorites();
}
},
fail = (res) =>
{
Debug.Log("WX.DownloadFile fail");
},
complete = (res) =>
{
Debug.Log("WX.DownloadFile complete");
HideLoading();
}
});
}

//设置收藏回调函数
private void InitializeFavoritesCallback()
{
string title = GetOptionString(0, "");
string imageUrl = GetOptionString(1, "");
bool disableForward = !GetOptionBool(2, false);

if (imageUrl == "本地图片文件路径")
{
imageUrl = localImagePath;
}

_onAddToFavorites = (callback) =>
{
callback(
new OnAddToFavoritesListenerResult
{
title = title,
imageUrl = imageUrl,
disableForward = disableForward
}
);
Debug.Log($"收藏回调参数 - 标题: {title}, 图片URL: {imageUrl}, 禁止转发: {disableForward}");
};
}

//切换收藏监听状态
public void onAddToFavorites()
{
if (!_isListeningAddToFavorites)
{
WX.OnAddToFavorites(_onAddToFavorites);
Debug.Log("开始监听收藏");
// 添加开始监听时的提示
WX.ShowToast(new ShowToastOption()
{
title = "已开启收藏监听",
icon = "none",
duration = 1500
});
}
else
{
WX.OffAddToFavorites(_onAddToFavorites);
Debug.Log("取消监听收藏");
// 添加取消监听时的提示
WX.ShowToast(new ShowToastOption()
{
title = "已取消收藏监听",
icon = "none",
duration = 1500
});
}
_isListeningAddToFavorites = !_isListeningAddToFavorites;
GameManager.Instance.detailsController.ChangeInitialButtonText(
_isListeningAddToFavorites ? "取消监听收藏" : "开始监听收藏"
);
}

private void OnDestroy()
{
if (_isListeningAddToFavorites)
{
WX.OffAddToFavorites(_onAddToFavorites);
Debug.Log("清理收藏监听");
}

// 清理本地文件
if (!string.IsNullOrEmpty(localImagePath))
{
var fs = WX.GetFileSystemManager();
fs.UnlinkSync(localImagePath);
Debug.Log("清理本地图片成功");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Demo/API_V2/Assets/API/Share/Favorites/FavoritesSO.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
m_Name: FavoritesSO
m_EditorClassIdentifier:
entryScriptTypeName: Favorites
entryName: "\u6536\u85CF\u76D1\u542C"
entryAPI: wx.onAddToFavorites | wx.offAddToFavorites
entryDescription: "\u76D1\u542C\u7528\u6237\u70B9\u51FB\u83DC\u5355\u300C\u6536\u85CF\u300D\u6309\u94AE\u65F6\u89E6\u53D1\u7684\u4E8B\u4EF6\n\uFF08\u5B89\u53537.0.15\u8D77\u652F\u6301\uFF0CiOS
\u6682\u4E0D\u652F\u6301\uFF09\n\u6CE8\u610F\uFF1A\u7A7A\u9009\u9879\u5219\u4F7F\u7528\u9ED8\u8BA4\u503C"
optionList:
- optionName: title
availableOptions:
- "\u6536\u85CF\u6807\u9898"
-
- optionName: imageUrl
availableOptions:
- https://mmocgame.qpic.cn/wechatgame/QgsibLsZPBy1uwWJnNNPeRXjBMKm327We8xrXWxGMM4cFKUZaqy0SU8TcudBf72sE/0
- "\u672C\u5730\u56FE\u7247\u6587\u4EF6\u8DEF\u5F84"
-
- optionName: disableForward
availableOptions:
- false
- true
initialButtonText: "\u5F00\u59CB\u76D1\u542C\u6536\u85CF"
extraButtonList: []
initialResultList: []
entryOrder: 0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Demo/API_V2/Assets/API/Share/ShareAppMessage.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 83 additions & 6 deletions Demo/API_V2/Assets/API/Share/ShareAppMessage/ShareAppMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,92 @@

public class ShareAppMessage : Details
{
// 测试 API
private string localImagePath;
protected override void TestAPI(string[] args)
{
Share();
if (GetOptionString(1, "") == "本地图片文件路径")
{
DownloadFileImage();
}
else
{
shareAppMessage();
}
}
private void ShowLoading()
{
WX.ShowLoading(new ShowLoadingOption()
{
title = "正在下载图片...",
mask = true
});
}
private void HideLoading()
{
WX.HideLoading(new HideLoadingOption());
}

private void DownloadFileImage()
{
ShowLoading();
WX.DownloadFile(new DownloadFileOption()
{
url = "https://picsum.photos/400/400?random=1",
success = (res) =>
{
Debug.Log("WX.DownloadFile success");
if (res.statusCode == 200)
{
Debug.Log(res.tempFilePath);
var fs = WX.GetFileSystemManager();
// 将临时文件保存为本地缓存文件
localImagePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/shareImage.jpg");
Debug.Log($"本地缓存文件保存路径: {localImagePath}");
shareAppMessage();
}
},
fail = (res) =>
{
Debug.Log("WX.DownloadFile fail");
},
complete = (res) =>
{
Debug.Log("WX.DownloadFile complete");
HideLoading();
}
});
}

private void shareAppMessage()
{
string title = GetOptionString(0, "");
string imageUrl = GetOptionString(1, "");
string imageUrlId = GetOptionString(2, "");
bool toCurrentGroupValue = GetOptionBool(3, true);

// 如果选择了本地图片文件路径,使用下载保存到本地的文件路径
if (imageUrl == "本地图片文件路径")
{
imageUrl = localImagePath;
}

WX.ShareAppMessage(new ShareAppMessageOption()
{
title = title,
imageUrl = imageUrl,
imageUrlId = imageUrlId,
toCurrentGroup = toCurrentGroupValue
});
}

// 分享
private void Share()
private void OnDestroy()
{
WX.ShareAppMessage(new ShareAppMessageOption() { title = "小游戏分享" });
// 清理文件
if (!string.IsNullOrEmpty(localImagePath))
{
var fs = WX.GetFileSystemManager();
fs.UnlinkSync(localImagePath);
Debug.Log("清理本地图片成功");
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading