Pre-arrangements
The following tasks must be completed in advance to implement coupon functions.
- A chart for using the mail
- Logic of completed login method
- Logic of completed game information management method
- Create a script exclusive to the mail
1. A chart for using the mail
The information from a chart that allows you to use mail functions is required to implement the items provided after claiming the mail.
Go to BACKND Console, and in BACKND Base > DataTable Management
, change the Mail Use status to Enabled for the chart that holds the item to be provided as a coupon reward.
If there are no registered charts, first proceed with 4. Implementing DataTable Functions Step 2. Registering DataTables to the Console.
Click the Edit button on the right of the chart.
Set the Mail Use status to Enabled.
After the change, the mail function is displayed as Enabled.
2. Logic of completed login method
All BACKND functions except login and sign-up require a login process to successfully call their methods.
If the login logic has not been implemented, please fully implement it according to the guidelines in 1. Implementing Login/Sign-up.
3. Logic of completed game information management method
This example also shows the process leading up to the logic that saves items returned after claiming the mail.
Therefore, 2. Implementing Game Information Functions, which is used to save mail data, must first be implemented.
If the game information function logic has not been implemented, please fully implement it according to 2. Implementing Game Information Functions.
4. Create a script exclusive to the mail
Create a new script and change the name to BackndMail.
Then, open the BackndMail.cs script and change the content according to the following:
BackndMail.cs
using System.Collections.Generic;
using System.Text;
using UnityEngine;
// Adds BACKND SDK namespace
using BACKND;
public class Mail {
public bool isCanReceive = false;
public string title; // Mail title
public string content; // Mail content
public string inDate; // Mail inDate
// The string is the name of the mail item and the int is the count
public Dictionary<string, int> postReward = new Dictionary<string, int>();
public override string ToString() {
string result = string.Empty;
result += $"title : {title}\n";
result += $"content : {content}\n";
result += $"inDate : {inDate}\n";
if(isCanReceive) {
result += "Mail Item\n";
foreach(string itemKey in postReward.Keys) {
result += $"| {itemKey} : {postReward[itemKey]}ea\n";
}
}
else {
result += "Unsupported mail item.";
}
return result;
}
}
public class BackndMail {
private static BackndMail _instance = null;
public static BackndMail Instance {
get {
if(_instance == null) {
_instance = new BackndMail();
}
return _instance;
}
}
private List<Post> _mailList = new List<Post>();
public void SavePostToLocal(BACKND.LitJson.JsonData item) {
foreach(BACKND.LitJson.JsonData itemJson in item) {
if(itemJson["item"].ContainsKey("itemType")) {
int itemId = int.Parse(itemJson["item"]["itemId"].ToString());
string itemType = itemJson["item"]["itemType"].ToString();
string itemName = itemJson["item"]["itemName"].ToString();
int itemCount = int.Parse(itemJson["itemCount"].ToString());
if(BackndGameData.userData.inventory.ContainsKey(itemName)) {
BackndGameData.userData.inventory[itemName] += itemCount;
}
else {
BackndGameData.userData.inventory.Add(itemName, itemCount);
}
Debug.Log($"Item received. : {itemName} - {itemCount}ea");
}
else {
Debug.LogError("Unsupported item.");
}
}
}
public void MailListGet(MailType mailType) {
// Step 3. Loading Mail
}
public void MailReceive(MailType mailType, int index) {
// Step 4. Claiming and Saving Individual Pieces of Mail
}
public void MaiReceivelAll(MailType mailType) {
// Step 5. Claiming and Saving All Mail
}
}
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"); // BACKND login method
// Adds mail logic
Debug.Log("Test complete.");
});
}
}