Full Code
BackndCoupon.cs
using System.Collections.Generic;
using System.Text;
using UnityEngine;
// Adds BACKND SDK namespace
using BACKND;
public class BackndCoupon {
private static BackndCoupon _instance = null;
public static BackndCoupon Instance {
get {
if(_instance == null) {
_instance = new BackndCoupon();
}
return _instance;
}
}
public void CouponUse(string couponNumber) {
var bro = Backnd.Coupon.UseCoupon(couponNumber);
if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while using the coupon. : " + bro);
return;
}
Debug.Log("Coupon successfully used. : " + bro);
if(BackndGameData.userData == null) {
BackndGameData.Instance.PlayerTableGet();
}
if(BackndGameData.userData == null) {
BackndGameData.Instance.PlayerTableInsert();
}
if(BackndGameData.userData == null) {
Debug.LogError("userData does not exist.");
return;
}
foreach(BACKND.LitJson.JsonData item in bro.GetFlattenJSON()["itemObject"]) {
if(item["item"].ContainsKey("itemType")) {
int itemId = int.Parse(item["item"]["itemId"].ToString());
string itemType = item["item"]["itemType"].ToString();
string itemName = item["item"]["itemName"].ToString();
int itemCount = int.Parse(item["itemCount"].ToString());
if(BackndGameData.userData.inventory.ContainsKey(itemName)) {
BackndGameData.userData.inventory[itemName] += itemCount;
} else {
BackndGameData.userData.inventory.Add(itemName, itemCount);
}
} else {
Debug.LogError("Unsupported item.");
}
}
BackndGameData.Instance.PlayerTableUpdate();
}
}
BackndManager.cs
using UnityEngine;
using System.Threading.Tasks;
// Adds BACKND SDK namespace
using BACKND;
public class BackndManager : MonoBehaviour {
void Start() {
var bro = Backnd.Initialize(); // Initialize BACKND
// Response value for BACKND initialization
if(bro.IsSuccess()) {
Debug.Log("Initialization successful : " + bro); // If successful, 'statusCode 204 Success'
} else {
Debug.LogError("Initialization failed : " + bro); // If failed, a 4xx statusCode error occurs
}
Test();
}
// A method that allows synchronous methods to be called from asynchronous methods(cannot be accessed by the Unity UI)
async void Test() {
await Task.Run(() => {
BackndLogin.Instance.CustomSignIn("user1", "1234");
BackndCoupon.Instance.CouponUse("f9ffeed2c882bc1418"); // [Addition] Uses a coupon whose coupon code is couponNumber
Debug.Log("Test complete.");
});
}
}