Unity3D 转换微信小游戏指引系列(第五期 完结)
广告
在小程序后台页面找到推广->流量主

开通条件如下:

开通之后,需要接入广告组件。
调用创建广告组件的接口时,需要传入参数 adUnitId
,这个是开通流量主之后可以获得的。
注意:广告组件是一个单例,仅需创建一次,监听事件也仅需注册一次,否则会有多次回调。
可以创建一个 WXAdManager
,提供初始化、显示广告的接口,关闭广告时,判断广告是否完整播放,并调用奖励回调。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using WeChatWASM;
public class WXAdManager : MonoBehaviour { static WXAdManager instance; public static WXAdManager Instance { get { return instance; } }
WXRewardedVideoAd ad; Action rewardCallback;
void Awake() { if (instance != null) { Destroy(gameObject); } else { instance = this; } }
public void Init() { ad = WX.CreateRewardedVideoAd(new WXCreateRewardedVideoAdParam() { adUnitId = "..." });
ad.OnLoad((res) => { Debug.Log($"广告加载 = {res.errMsg}"); }); ad.OnError((res) => { Debug.Log($"广告错误 = {res.errMsg}"); }); ad.OnClose((res) => { Debug.Log($"广告关闭 是否看完 = {res.isEnded}"); OnPlayEnd(res.isEnded); }); }
public void ShowAd(Action callback) { Debug.Log("显示广告"); ad.Show(); rewardCallback = callback; }
void OnPlayEnd(bool isEnd) { if (isEnd) { Debug.Log("完整看完广告"); rewardCallback?.Invoke(); } else { Debug.Log("没有看完广告"); } } }
|
使用时,先进行初始化,要在微信 SDK 初始化之后调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| using System.Collections; using System.Collections.Generic; using UnityEngine; using WeChatWASM;
public class WXDemo : MonoBehaviour { void Start() { WX.InitSDK((int code)=> { Debug.Log("微信 SDK 初始化"); WXAdManager.Instance.Init(); GameInit(); }); }
void GameInit() { Debug.Log("游戏主逻辑初始化"); } }
|
在具体逻辑的位置,调用显示广告的接口,并传入奖励回调函数。
1 2 3 4 5
| WXAdManager.Instance.ShowAd(()=> { Debug.Log("广告完整播放,发放奖励"); });
|
内购
在小程序后台页面找到功能->虚拟支付

开通条件如下:

开通之后,可以接入米大师安卓支付,iOS 系统暂时不支持。
可以创建一个 IAPManager
,提供购买的接口,支付成功时,根据产品 ID 发放奖励,也可以调用奖励回调。
这里需要判断运行平台 platform
是安卓,如何获取 platform
参数,可以参考之前的文章《Unity3D 转换微信小游戏指引 03 微信SDK》里面的获取系统参数部分。
在调用支付接口时,参数 offerId
是开通虚拟支付后可以获得的。
需要特别注意的是,参数 buyQuantity
是有限制的,具体限制可以参考 buyQuantity 限制说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using WeChatWASM;
public class IAPManager : MonoBehaviour { static IAPManager instance; public static IAPManager Instance { get { return instance; } }
public string platform; public int buyQuantityMul = 10; Action rewardCallback;
void Awake() { if (instance != null) { Destroy(gameObject); } else { instance = this; } }
public void Purchase(string productID, Action callback) { rewardCallback = callback;
if ("android".Equals(platform)) { double price = 1;
double quantity = price * buyQuantityMul;
WX.RequestMidasPayment(new RequestMidasPaymentOption() { currencyType = "CNY", mode = "game", offerId = "...", buyQuantity = quantity, env = 0, success = (res) => { PurchaseSuccessful(productID); }, fail = (res) => { Debug.Log($"购买失败 = {res.errMsg}"); } }); } else { Debug.Log("暂不支持内购"); } }
void PurchaseSuccessful(string productID) { Debug.Log($"购买 {productID} 成功,发放奖励"); rewardCallback?.Invoke(); } }
|
在具体逻辑的位置,调用发起支付的接口,并传入支付成功的回调函数。
1 2 3 4 5
| IAPManager.Instance.Purchase("1001", ()=> { Debug.Log("支付成功,发放奖励"); });
|