Files
2026-06-29 21:18:33 +08:00

405 lines
13 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TapjoyUnity;
using TMPro;
using System.Text.RegularExpressions;
#if UNITY_IOS_AD_SUPPORT
#if UNITY_IOS
using Unity.Advertisement.IosSupport;
#endif
#endif
public class MainScreen : MonoBehaviour
{
string sdkKey = "";
public TMP_Text statusMessageText;
public Button connectButton;
public Button showOfferwallButton;
public TMP_InputField currencyAmountInput;
public Button getCurrencyButton;
public Button spendCurrencyButton;
public Button awardCurrencyButton;
public TMP_InputField examplePlacementInput;
public Button requestButton;
public Button showButton;
public TMP_Dropdown entryPointDropdown;
public TMP_InputField currencyIdInput;
public TMP_InputField currencyBalanceInput;
public TMP_InputField currencyRequiredAmountInput;
public TMP_InputField purchaseCodeInput;
public TMP_InputField purchasePriceInput;
public Button purchaseButton;
public TMP_Text versionLabel;
public ToggleGroup loggingLevelToggleGroup;
public List<Toggle> loggingLevelToggles;
TJPlacement offerwallPlacement;
TJPlacement examplePlacement;
TJPlacement.OnContentReadyHandler readyHandler;
TJPlacement.OnContentDismissHandler dismissHandler;
// Start is called before the first frame update
void Start()
{
#if UNITY_IOS
#if UNITY_IOS_AD_SUPPORT
//Show ATT before connection, Only applicable to IOS
if(ATTrackingStatusBinding.GetAuthorizationTrackingStatus() ==
ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED)
{
ATTrackingStatusBinding.RequestAuthorizationTracking();
}
#endif
// This sdk key will be only used when the auto connect mode disabled in Tapjoy window.
sdkKey = "E7CuaoUWRAWdz_5OUmSGsgEBXHdOwPa8de7p4aseeYP01mecluf-GfNgtXlF-I2Bc2MrTiSBFeAGwa_q4AECOpWoNcElJCEUylQ6fWzuIbuRwyFd0nMBdbia";
#elif UNITY_ANDROID
// sdkKey = "u6SfEbh_TA-WMiGqgQ3W8QECyiQIURFEeKm0zbOggubusy-o5ZfXp33sTXaD";
sdkKey = "-I2Bc2MrTiSBFeAGwa_q4AECOpWoNcElJCEUylQ6fWzuIbuRwyFd0nMBdbia";
#endif
//init state of the components
examplePlacementInput.text = "offerwall_unit";
showButton.interactable = false;
versionLabel.text = $"SDK Version : {Tapjoy.Version}";
// Connect delegates
Tapjoy.OnConnectSuccess += HandleConnectSuccess;
Tapjoy.OnConnectFailed += HandleConnectFailed;
Tapjoy.OnConnectWarning += HandleConnectWarning;
// Placement delegates
TJPlacement.OnRequestSuccess += HandlePlacementRequestSuccess;
TJPlacement.OnRequestFailure += HandlePlacementRequestFailure;
TJPlacement.OnContentShow += HandlePlacementContentShow;
TJPlacement.OnSetCurrencyBalanceSuccess += HandleSetCurrencyBalanceSuccess;
TJPlacement.OnSetCurrencyBalanceFailure += HandleSetCurrencyBalanceFailure;
TJPlacement.OnSetCurrencyAmountRequiredSuccess += HandleSetRequiredAmountSuccess;
TJPlacement.OnSetCurrencyAmountRequiredFailure += HandleSetRequiredAmountFailure;
// Currency delegates
Tapjoy.OnAwardCurrencyResponse += HandleAwardCurrencyResponse;
Tapjoy.OnAwardCurrencyResponseFailure += HandleAwardCurrencyResponseFailure;
Tapjoy.OnSpendCurrencyResponse += HandleSpendCurrencyResponse;
Tapjoy.OnSpendCurrencyResponseFailure += HandleSpendCurrencyResponseFailure;
Tapjoy.OnGetCurrencyBalanceResponse += HandleGetCurrencyBalanceResponse;
Tapjoy.OnGetCurrencyBalanceResponseFailure += HandleGetCurrencyBalanceResponseFailure;
Tapjoy.OnEarnedCurrency += HandleEarnedCurrency;
// Set up logging level toggle group
loggingLevelToggleGroup.allowSwitchOff = false;
updateLoggingLevelToggles();
}
void Update()
{
// Quit app on BACK key.
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
void LoggingLevelToggleValueChanged(Toggle toggle)
{
if (toggle.isOn)
{
LoggingLevel loggingLevel = (LoggingLevel)loggingLevelToggles.IndexOf(toggle);
LoggingLevel currentLoggingLevel = Tapjoy.GetLoggingLevel();
Tapjoy.SetLoggingLevel(loggingLevel);
if (Tapjoy.GetLoggingLevel() != loggingLevel)
{
updateLoggingLevelToggles();
} else {
Debug.Log($"Logging level set to {loggingLevel}");
}
}
}
void updateLoggingLevelToggles() {
removeLoggingLevelListeners();
loggingLevelToggleGroup.SetAllTogglesOff();
// LoggingLevel.Error is the default logging level.
Toggle switchedToggle = loggingLevelToggles[0];
switch (Tapjoy.GetLoggingLevel())
{
case LoggingLevel.Debug:
switchedToggle = loggingLevelToggles[3];
break;
case LoggingLevel.Info:
switchedToggle = loggingLevelToggles[2];
break;
case LoggingLevel.Warning:
switchedToggle = loggingLevelToggles[1];
break;
}
switchedToggle.isOn = true;
// Notify the toggle group that the toggle is on.
loggingLevelToggleGroup.NotifyToggleOn(switchedToggle);
addLoggingLevelListeners();
}
void addLoggingLevelListeners()
{
foreach (var toggle in loggingLevelToggles)
{
toggle.onValueChanged.AddListener(delegate {
LoggingLevelToggleValueChanged(toggle);
});
}
}
void removeLoggingLevelListeners()
{
foreach (var toggle in loggingLevelToggles)
{
toggle.onValueChanged.RemoveAllListeners();
}
}
// Request connect if auto connect is not checked in the Unity Tapjoy editor.
public void RequestConnect()
{
Debug.Log($"SDK Key:{sdkKey}");
Dictionary<string, object> connectFlags = new Dictionary<string, object>();
connectFlags.Add("TJC_OPTION_LOGGING_LEVEL", LoggingLevel.Info);
Tapjoy.Connect(sdkKey, connectFlags);
updateLoggingLevelToggles();
}
public void RequestOfferwall()
{
SetStatusMessage("Requesting offerwall...");
offerwallPlacement = TJPlacement.CreatePlacement("offerwall_unit");
readyHandler = (TJPlacement placement) =>
{
SetStatusMessage("Offerwall content is ready.");
offerwallPlacement.ShowContent();
TJPlacement.OnContentReady -= readyHandler;
};
TJPlacement.OnContentReady += readyHandler;
dismissHandler = (TJPlacement placement) =>
{
SetStatusMessage($"Offerwall has been dismissed.");
TJPlacement.OnContentDismiss -= dismissHandler;
};
TJPlacement.OnContentDismiss += dismissHandler;
offerwallPlacement.RequestContent();
}
public void GetCurrency()
{
SetStatusMessage("Getting Currency...");
Tapjoy.GetCurrencyBalance();
}
public void SpendCurrency()
{
SetStatusMessage("Spending Currency...");
int value = int.TryParse(currencyAmountInput.text, out int result) ? result : 0;
Tapjoy.SpendCurrency(value);
}
public void AwardCurrency()
{
SetStatusMessage("Awarding Currency...");
int value = int.TryParse(currencyAmountInput.text, out int result) ? result : 0;
Tapjoy.AwardCurrency(value);
}
public void RequestExampleContent()
{
SetStatusMessage("Requesting example content...");
examplePlacement = TJPlacement.CreatePlacement(examplePlacementInput.text);
readyHandler = (TJPlacement placement) =>
{
SetStatusMessage($"TJPlacement {placement.GetName()} content is ready.");
showButton.interactable = true;
TJPlacement.OnContentReady -= readyHandler;
};
TJPlacement.OnContentReady += readyHandler;
dismissHandler = (TJPlacement placement) =>
{
SetStatusMessage($"TJPlacement {placement.GetName()} has been dismissed.");
showButton.interactable = false;
TJPlacement.OnContentDismiss -= dismissHandler;
};
TJPlacement.OnContentDismiss += dismissHandler;
examplePlacement.SetEntryPoint((TJEntryPoint)entryPointDropdown.value);
if (currencyIdInput.text != "")
{
if (currencyBalanceInput.text != "" && currencyBalanceInput.text == Regex.Replace(currencyBalanceInput.text, @"[^0-9]", ""))
{
examplePlacement.SetCurrencyBalance(currencyBalanceInput.text, int.Parse(currencyBalanceInput.text));
}
if (currencyRequiredAmountInput.text != "" && currencyRequiredAmountInput.text == Regex.Replace(currencyRequiredAmountInput.text, @"[^0-9]", ""))
{
examplePlacement.SetRequiredAmount(currencyBalanceInput.text, int.Parse(currencyRequiredAmountInput.text));
}
}
examplePlacement.RequestContent();
}
public void ShowExampleContent()
{
SetStatusMessage("Showing example content..");
examplePlacement.ShowContent();
}
public void Purchase()
{
double value = double.TryParse(purchasePriceInput.text, out double result) ? result : 0.0;
string code = !string.IsNullOrEmpty(purchaseCodeInput.text) ? purchaseCodeInput.text : "";
Tapjoy.TrackPurchase(code, value);
SetStatusMessage($"Sent track purchase {code} - {value}");
}
private string getDummySkuDetails()
{
return "{\"title\":\"TITLE\",\"price\":\"$3.33\",\"type\":\"inapp\",\"description\":\"DESC\",\"price_amount_micros\":3330000,\"price_currency_code\":\"USD\",\"productId\":\"3\"}";
}
void SetStatusMessage(string message)
{
Debug.Log(message);
statusMessageText.text = message;
}
#region Tapjoy Delegate Handlers
#region Connect Delegate Handlers
void HandleConnectSuccess()
{
SetStatusMessage("Tapjoy Connected");
connectButton.gameObject.SetActive(false);
updateLoggingLevelToggles();
}
void HandleConnectFailed(int code, string message)
{
SetStatusMessage($"Tapjoy SDK failed to connect. Code: {code} Message: {message}");
}
void HandleConnectWarning(int code, string message)
{
SetStatusMessage($"Tapjoy SDK connect succeeded with warning Code: {code} Message: {message}");
}
#endregion
#region Placement Delegate Handlers
public void HandlePlacementRequestSuccess(TJPlacement placement)
{
if (placement.IsContentAvailable())
{
SetStatusMessage($"Content available for {placement.GetName()}");
}
else
{
SetStatusMessage($"No content available for {placement.GetName()}");
}
}
void HandlePlacementRequestFailure(TJPlacement placement, string error)
{
SetStatusMessage($"Request for {placement.GetName()} has failed because: {error}");
}
void HandlePlacementContentShow(TJPlacement placement)
{
SetStatusMessage("Handle placement content show");
}
void HandleSetCurrencyBalanceSuccess(TJPlacement placement)
{
SetStatusMessage($"SetCurrencyBalance for {placement.GetName()} has succeeded. Balance: {placement.GetCurrencyBalance(currencyIdInput.text)}");
}
void HandleSetCurrencyBalanceFailure(TJPlacement placement, int code, string error)
{
SetStatusMessage($"SetCurrencyBalance for {placement.GetName()} has failed because: {error}(code: {code})");
}
void HandleSetRequiredAmountSuccess(TJPlacement placement)
{
SetStatusMessage($"SetRequiredAmount for {placement.GetName()} has succeeded. Balance: {placement.GetRequiredAmount(currencyIdInput.text)}");
}
void HandleSetRequiredAmountFailure(TJPlacement placement, int code, string error)
{
SetStatusMessage($"SetRequiredAmount for {placement.GetName()} has failed because: {error}(code: {code})");
}
#endregion
#region Currency Delegate Handlers
void HandleAwardCurrencyResponse(string currencyName, int balance)
{
SetStatusMessage($"Awarded currency -- {currencyName} balance: {balance}");
}
void HandleAwardCurrencyResponseFailure(string error)
{
SetStatusMessage($"AwardCurrency failed: {error}");
}
void HandleGetCurrencyBalanceResponse(string currencyName, int balance)
{
SetStatusMessage($"{currencyName} balance: {balance}");
}
void HandleGetCurrencyBalanceResponseFailure(string error)
{
SetStatusMessage($"GetCurrencyBalance failed: {error}");
}
void HandleSpendCurrencyResponse(string currencyName, int balance)
{
SetStatusMessage($"{currencyName} balance: {balance}");
}
void HandleSpendCurrencyResponseFailure(string error)
{
SetStatusMessage($"SpendCurrency failed: {error}");
}
void HandleEarnedCurrency(string currencyName, int amount)
{
SetStatusMessage($"{currencyName} earned: {amount}");
Tapjoy.ShowDefaultEarnedCurrencyAlert();
}
#endregion
#endregion
}