Unity 接入微信小游戏后常用的功能
1. 前言
近期项目需要将游戏投放到国内各个小游戏平台,微信小游戏,抖音小游戏,快手小游戏,支付宝小游戏等,各种小游戏平台,之前有处理过国内安卓 谷歌 IOS 平台的打包,后边会一一说明各小游戏平台用到的不同的功能。
关于游戏上架微信的博客有很多,此篇主要提一下我上到微信后用到的微信方面相关的功能,分享,邀请,订阅,朋友圈,振动,好友排行等,后台等相关应用,具体还要根据大家项目自行修改。
2.微信相关文档
普通的Unity版本可以参考下方链接接入
3.关于打包微信的应用
1.处理掉烦人的LOGO
- 代码处理
[Preserve]
public class SkipUnityLogo
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void BeforeSplashScreen()
{
#if UNITY_WEBGL
Application.focusChanged += Application_focusChanged;
#else
System.Threading.Tasks.Task.Run(AsyncSkip);
#endif
}
#if UNITY_WEBGL
private static void Application_focusChanged(bool obj)
{
Application.focusChanged -= Application_focusChanged;
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#else
private static void AsyncSkip()
{
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
}
#endif
}
#endif
- 插件处理
如果不是团结引擎的话,可以考虑用些工具处理 ( ̄︶ ̄)↗
类似下边这个 - https://github.com/tylearymf/UniHacker
2.微信分享或邀请
微信当中可以自定义参数,来获取来进行分享或邀请
GetShareInfo(type, WeChatConfig.ShareLocationInvite, (info) =>
{
LY.Analytics.ShareEvent(WeChatConfig.ShareLocationMenu, type, DataAnalytics.ShareAction.show, info.combineShareId);
string str = $"sharelocation={
info.location}&combineShareId={
info.combineShareId}&action=normal"; //分享
string str = $"sharelocation={
info.location}&combineShareId={
info.combineShareId}&inviter={
LY.SDK.GetOpenId()}&action=invite"; //邀请
_inviteCallback = cb;
_shareStartTime = DateTime.Now;
ShareAppMessageOption shareAppMessageOption = new ShareAppMessageOption();
shareAppMessageOption.title = info.text;
shareAppMessageOption.imageUrl = info.imageUrl; //自己CDN配置的图片地址
shareAppMessageOption.query = str;
WX.ShareAppMessage(shareAppMessageOption); //调用
}, (error) =>
{
cb?.Invoke(false);
});
微信也会返回受邀新用户数量,可以用来做邀请有礼等游戏内容
3.游戏圈
游戏圈的难点,基本就在入口的位置计算上,另外就是我们UI还是用的美术出的效果图,但是向微信上传的是一张空白图
- 第一步计算生成位置
public static void CreateClubBtn(Transform tra, Camera camera = null)
{
#if !UNITY_EDITOR
if (camera == null)
{
camera = Camera.main;
}
WeChatWASM.SystemInfo systemInfo = WX.GetSystemInfoSync(); ;
float ux = Screen.width;
float uy = Screen.height;
float wx = (float)systemInfo.screenWidth;
float wy = (float)systemInfo.screenHeight;
float sx = wx / ux;
float sy = wy / uy;
Vector3 screenPoint = RectTransformUtility.WorldToScreenPoint(camera, tra.position);
Rect r = Utility.UGUI.GetSize(tra.gameObject);
float bw = r.width * sx;
float bh = r.height * sy;
float bx = screenPoint.x * sx - bw * 0.5f;
float by = (Screen.height - screenPoint.y) * sy - bh * 0.5f;
Debug.Log($"CreateClubBtn++++ux:{
ux} uy:{
uy} wx:{
wx} wy:{
wy} sx:{
sx} sy:{
sy} bw:{
bw} bh:{
bh} bx:{
bx} by:{
by} screenX:{
screenPoint.x} screenY:{
screenPoint.y}");
CreateClubBtn(new Rect(bx,by,bw,bh));
#endif
}
- 生成出对应功能
public static void