思路
定义一个IosRequestStoreReviewSys类:在想要弹出的时机,进行条件判断,若满足条件则弹出窗口。
外部在游戏开始时,主动调用其静态方法Init即可。
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 在合适(指检查条件是否满足、且检查后条件满足)的时机,弹出去苹果商店评价的窗口
// 在游戏初始化时调用:IosRequestStoreReviewSys.Init();
public class IosRequestStoreReviewSys
{
public static IosRequestStoreReviewSys Instance { get; private set; }
private const string HasRequestedStorageKey = "IosRequestStoreReviewSys" + "HasRequested";
// 到达合适的
private float _initiateRequestDelayTime = 1f;
private Condition _condition;
public static void Init()
{
if (!HasRequested())
{
Instance = new IosRequestStoreReviewSys();
}
bool HasRequested()
{
return PlayerPrefs.GetInt(HasRequestedStorageKey, 0) != 0;
}
}
private IosRequestStoreReviewSys()
{
_condition = new Condition();
}
private void Destroy()
{
_condition?.Destroy();
_condition = null;
Instance = null;
}
private void 在想要弹出窗口的时机调用,若满足条件,则弹出窗口()
{
TryInitiateRequest();
}
private void TryInitiateRequest()
{
MonoSys.Instance.DelayCall(_initiateRequestDelayTime, InitiateRequest, true);
bool CanInitiateRequest()
{
return _condition.IsSatisfy();
}
void Complete()
{
PlayerPrefs.SetInt(HasRequestedStorageKey, 1);
}
void InitiateRequest()
{
if (CanInitiateRequest())
{
#if UNITY_IOS
if(Application.platform == RuntimePlatform.IPhonePlayer)
{
UnityEngine.iOS.Device.RequestStoreReview();
}
#endif
// Debug.Log("!!弹出:去苹果商店评价的窗口。");
Complete();
Destroy();
}
}
}
// 条件:自己记录一些数据。在外部调用IsSatisfy时,判断条件是否满足。
private class Condition
{
public Condition()
{
}
public void Destroy()
{
}
// 外部调用,在此处判断条件是否满足
public bool IsSatisfy()
{
return true;
}
}
}
网友评论