318 lines
9.4 KiB
C#
318 lines
9.4 KiB
C#
using System;
|
|
using asap.core;
|
|
using TapjoyUnity;
|
|
using UnityEngine;
|
|
|
|
|
|
public class TapjoyHelper: MonoBehaviour
|
|
{
|
|
public const string TapJoy_Diamonds = "TapJoy_Diamonds";
|
|
public const string TapJoy_Hooks = "TapJoy_Hooks";
|
|
//
|
|
private TJPlacement tjDiamonds; // = TJPlacement.CreatePlacement(TapJoy_Diamonds);
|
|
private TJPlacement tjHooks;// = TJPlacement.CreatePlacement(TapJoy_Hooks);
|
|
//
|
|
private TJPlacement.OnContentReadyHandler readyHandler;
|
|
private TJPlacement.OnContentDismissHandler dismissHandler;
|
|
|
|
// 获得奖励会掉
|
|
private Action<string,int> _onRewardAction;
|
|
|
|
private Action<string,int> _onGetCurrencyBalanceAction;
|
|
|
|
private int _tapJoyCurrencyBalance = 0;
|
|
|
|
#region 周期函数
|
|
private void Awake()
|
|
{
|
|
Log("Awake-> ");
|
|
}
|
|
//
|
|
private void Start()
|
|
{
|
|
Log("Start-> ");
|
|
|
|
tjDiamonds = TJPlacement.CreatePlacement(TapJoy_Diamonds);
|
|
tjHooks = TJPlacement.CreatePlacement(TapJoy_Hooks);
|
|
|
|
|
|
TJPlacement.OnRequestSuccess += HandlePlacementRequestSuccess;
|
|
TJPlacement.OnRequestFailure += HandlePlacementRequestFailure;
|
|
TJPlacement.OnContentReady += HandlePlacementContentReady;
|
|
TJPlacement.OnContentShow += HandlePlacementContentShow;
|
|
TJPlacement.OnContentDismiss += HandlePlacementContentDismiss;
|
|
|
|
// DEPRATED
|
|
// TJPlacement.OnPurchaseRequest += HandleOnPurchaseRequest;
|
|
// TJPlacement.OnRewardRequest += HandleOnRewardRequest;
|
|
// TJPlacement.On
|
|
// tjDiamonds.SetEntryPoint(TJEntryPoint.UNKNOWN);
|
|
// TJPlacement.OnRewardRequest += HandleRewardRequest;
|
|
// TJPlacement.OnSetCurrencyBalanceSuccess += placement =>
|
|
// {
|
|
// Log($"OnSetCurrencyBalanceSuccess -> {placement.GetName()}");
|
|
// };
|
|
// TJPlacement.OnSetCurrencyAmountRequiredSuccess += placement =>
|
|
// {
|
|
// Log($"OnSetCurrencyAmountRequiredSuccess -> {placement.GetName()}");
|
|
// };
|
|
|
|
Tapjoy.OnConnectSuccess += HandleConnectSuccess;
|
|
Tapjoy.OnConnectFailed += HandleConnectFailed;
|
|
Tapjoy.OnConnectWarning += HandleConnectWarning;
|
|
Tapjoy.OnAwardCurrencyResponse += HandleAwardCurrencyResponse;
|
|
Tapjoy.OnAwardCurrencyResponseFailure += HandleAwardCurrencyResponseFailure;
|
|
Tapjoy.OnSpendCurrencyResponse += HandleSpendCurrencyResponse;
|
|
Tapjoy.OnSpendCurrencyResponseFailure += HandleSpendCurrencyResponseFailure;
|
|
Tapjoy.OnGetCurrencyBalanceResponse += HandleGetCurrencyBalanceResponse;
|
|
Tapjoy.OnGetCurrencyBalanceResponseFailure += HandleGetCurrencyBalanceResponseFailure;
|
|
Tapjoy.OnEarnedCurrency += HandleEarnedCurrency;
|
|
|
|
// GContext.OnEvent<TapJoyData>().Subscribe(x => { });
|
|
|
|
// GetCurrencyBalance
|
|
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region SDK 接入请求回调
|
|
private void HandleRewardRequest(TJPlacement placement, TJActionRequest request, string itemId, int quantity)
|
|
{
|
|
Log($"HandleRewardRequest -> {placement.GetName()}, {request.requestID} {itemId} {quantity}");
|
|
}
|
|
// 请求成功
|
|
public void HandlePlacementRequestSuccess(TJPlacement placement)
|
|
{
|
|
Log("HandlePlacementRequestSuccess-> ");
|
|
}
|
|
private void HandlePlacementRequestFailure(TJPlacement placement, string error)
|
|
{
|
|
Log("HandlePlacementRequestFailure-> ");
|
|
// throw new NotImplementedException();
|
|
}
|
|
public void HandlePlacementContentReady(TJPlacement placement)
|
|
{
|
|
Log("HandlePlacementContentReady-> ");
|
|
// This gets called when content is ready to show.
|
|
// TODO:LF 处理一下音频之类的东西
|
|
//
|
|
// GContext.Publish();
|
|
}
|
|
public void HandlePlacementContentShow(TJPlacement placement)
|
|
{
|
|
Log("HandlePlacementContentShow-> ");
|
|
}
|
|
public void HandlePlacementContentDismiss(TJPlacement placement)
|
|
{
|
|
Log($"HandlePlacementContentDismiss-> {placement.GetName()}");
|
|
Tapjoy.GetCurrencyBalance();
|
|
placement.RequestContent();
|
|
}
|
|
|
|
private void HandleConnectSuccess()
|
|
{
|
|
Log("Tapjoy Connected");
|
|
Tapjoy.GetCurrencyBalance();
|
|
}
|
|
|
|
private void HandleConnectFailed(int code, string message)
|
|
{
|
|
Log($"Tapjoy SDK failed to connect. Code: {code} Message: {message}");
|
|
}
|
|
|
|
private void HandleConnectWarning(int code, string message)
|
|
{
|
|
Log($"Tapjoy SDK connect succeeded with warning Code: {code} Message: {message}");
|
|
}
|
|
|
|
// 处理货币金额
|
|
public void HandleAwardCurrencyResponse(string currencyName, int balance)
|
|
{
|
|
Log($"Awarded currency -- {currencyName} balance: {balance}");
|
|
}
|
|
|
|
public void HandleAwardCurrencyResponseFailure(string error)
|
|
{
|
|
Log($"AwardCurrency failed: {error}");
|
|
}
|
|
|
|
public void HandleGetCurrencyBalanceResponse(string currencyName, int balance)
|
|
{
|
|
Log($"HandleGetCurrencyBalanceResponse {currencyName} balance: {balance}");
|
|
_tapJoyCurrencyBalance = balance;
|
|
|
|
_onGetCurrencyBalanceAction.Invoke(currencyName,_tapJoyCurrencyBalance);
|
|
}
|
|
|
|
public void HandleGetCurrencyBalanceResponseFailure(string error)
|
|
{
|
|
Log($"GetCurrencyBalance failed: {error}");
|
|
}
|
|
|
|
public void HandleSpendCurrencyResponse(string currencyName, int balance)
|
|
{
|
|
Log($"HandleSpendCurrencyResponse {currencyName} balance: {balance}");
|
|
|
|
_tapJoyCurrencyBalance = balance;
|
|
}
|
|
|
|
public void HandleSpendCurrencyResponseFailure(string error)
|
|
{
|
|
Log($"SpendCurrency failed: {error}");
|
|
}
|
|
|
|
public void HandleEarnedCurrency(string currencyName, int amount)
|
|
{
|
|
Log($"{currencyName} earned: {amount}");
|
|
// #if DEBUG
|
|
// Tapjoy.ShowDefaultEarnedCurrencyAlert();
|
|
// #endif
|
|
_onRewardAction?.Invoke(currencyName,amount);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Log
|
|
private static void Log(string message)
|
|
{
|
|
Debug.Log($"<color=yellow>TapjoyHelper-> {message} </color>");
|
|
}
|
|
private static void LogWarning(string message)
|
|
{
|
|
Debug.LogWarning($"<color=orange>TapjoyHelper-> {message} </color>");
|
|
}
|
|
#endregion
|
|
|
|
#region 功能类函数
|
|
public void Init(int balance, Action<string,int> action,Action<string,int> action2)
|
|
{
|
|
#if UNITY_EDITOR
|
|
_tapJoyCurrencyBalance = balance;
|
|
#endif
|
|
_onRewardAction += action;
|
|
_onGetCurrencyBalanceAction += action2;
|
|
// 在这里链接一次,已经链接会不起作用
|
|
Tapjoy.Connect();
|
|
}
|
|
public void GetContent(int? id)
|
|
{
|
|
Log($"GetContent-> {id}");
|
|
}
|
|
public void ShowContent(int? shopTokenID)
|
|
{
|
|
_tryConnectTimes = MaxTryConnectTimes;
|
|
StartShowDiamonds();
|
|
|
|
#if UNITY_EDITOR
|
|
// _tapJoyCurrencyBalance += 1;
|
|
// _onGetCurrencyBalanceAction.Invoke("Diamonds",_tapJoyCurrencyBalance);
|
|
_onRewardAction.Invoke("Diamonds",1);
|
|
#endif
|
|
}
|
|
public void OnRequestDiamonds(Action onAction = null)
|
|
{
|
|
Log("OnRequestDiamonds()-> ");
|
|
|
|
if (Tapjoy.IsConnected)
|
|
{
|
|
readyHandler = _ =>
|
|
{
|
|
ShowDiamonds();
|
|
TJPlacement.OnContentReady -= readyHandler;
|
|
};
|
|
TJPlacement.OnContentReady += readyHandler;
|
|
tjDiamonds.RequestContent();
|
|
}
|
|
else
|
|
{
|
|
LogWarning("Tapjoy SDK must be connected before you can request content.");
|
|
}
|
|
}
|
|
public void OnRequestHooks()
|
|
{
|
|
Log("OnRequestHooks()-> ");
|
|
if (Tapjoy.IsConnected)
|
|
{
|
|
readyHandler = _ =>
|
|
{
|
|
ShowHooks();
|
|
TJPlacement.OnContentReady -= readyHandler;
|
|
};
|
|
TJPlacement.OnContentReady += readyHandler;
|
|
tjHooks.RequestContent();
|
|
}
|
|
else
|
|
{
|
|
LogWarning("Tapjoy SDK must be connected before you can request content.");
|
|
}
|
|
}
|
|
|
|
// 重连次数
|
|
private const int MaxTryConnectTimes = 3;
|
|
|
|
private int _tryConnectTimes = MaxTryConnectTimes;
|
|
|
|
private void RetryConnect(int code, string message)
|
|
{
|
|
Log($"Tapjoy SDK Retry to connect. Code: {code} Message: {message}");
|
|
if (_tryConnectTimes-- <= 0)
|
|
{
|
|
return;
|
|
}
|
|
StartShowDiamonds();
|
|
}
|
|
|
|
private void OnConnectSuccessForShowDiamonds()
|
|
{
|
|
Tapjoy.OnConnectFailed -= RetryConnect;
|
|
Tapjoy.OnConnectSuccess -= OnConnectSuccessForShowDiamonds;
|
|
ShowDiamonds();
|
|
}
|
|
|
|
public void StartShowDiamonds()
|
|
{
|
|
if (!Tapjoy.IsConnected)
|
|
{
|
|
Tapjoy.Connect();
|
|
Tapjoy.OnConnectSuccess += OnConnectSuccessForShowDiamonds;
|
|
Tapjoy.OnConnectFailed += RetryConnect;
|
|
return;
|
|
}
|
|
ShowDiamonds();
|
|
}
|
|
|
|
public void ShowDiamonds()
|
|
{
|
|
|
|
if (tjDiamonds.IsContentReady())
|
|
{
|
|
tjDiamonds.ShowContent();
|
|
}
|
|
else
|
|
{
|
|
OnRequestDiamonds();
|
|
// Code to handle situation where content is not ready goes here
|
|
}
|
|
}
|
|
public void ShowHooks()
|
|
{
|
|
if (tjHooks.IsContentReady())
|
|
{
|
|
tjHooks.ShowContent();
|
|
}
|
|
else
|
|
{
|
|
OnRequestHooks();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
} |