GoogleSheetManager.cs 소스입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
[System.Serializable]
public class GoogleData
{
public string order, result, msg, value;
}
public class GoogleSheetManager : MonoBehaviour
{
const string URL = "https://script.google.com/macros/s/AKfycbysyqyeh2ely2XqXuD4-MDw1-1vfOSJxKkwQ_HfmvQ1v00xo0M/exec";
public GoogleData GD;
public InputField IDInput, PassInput, ValueInput;
string id, pass;
bool SetIDPass()
{
id = IDInput.text.Trim();
pass = PassInput.text.Trim();
if (id == "" || pass == "") return false;
else return true;
}
public void Register()
{
if (!SetIDPass())
{
print("아이디 또는 비밀번호가 비어있습니다");
return;
}
WWWForm form = new WWWForm();
form.AddField("order", "register");
form.AddField("id", id);
form.AddField("pass", pass);
StartCoroutine(Post(form));
}
public void Login()
{
if (!SetIDPass())
{
print("아이디 또는 비밀번호가 비어있습니다");
return;
}
WWWForm form = new WWWForm();
form.AddField("order", "login");
form.AddField("id", id);
form.AddField("pass", pass);
StartCoroutine(Post(form));
}
void OnApplicationQuit()
{
WWWForm form = new WWWForm();
form.AddField("order", "logout");
StartCoroutine(Post(form));
}
public void SetValue()
{
WWWForm form = new WWWForm();
form.AddField("order", "setValue");
form.AddField("value", ValueInput.text);
StartCoroutine(Post(form));
}
public void GetValue()
{
WWWForm form = new WWWForm();
form.AddField("order", "getValue");
StartCoroutine(Post(form));
}
IEnumerator Post(WWWForm form)
{
using (UnityWebRequest www = UnityWebRequest.Post(URL, form)) // 반드시 using을 써야한다
{
yield return www.SendWebRequest();
if (www.isDone) Response(www.downloadHandler.text);
else print("웹의 응답이 없습니다.");
}
}
void Response(string json)
{
if (string.IsNullOrEmpty(json)) return;
GD = JsonUtility.FromJson<GoogleData>(json);
if (GD.result == "ERROR")
{
print(GD.order + "을 실행할 수 없습니다. 에러 메시지 : " + GD.msg);
return;
}
print(GD.order + "을 실행했습니다. 메시지 : " + GD.msg);
if (GD.order == "getValue")
{
ValueInput.text = GD.value;
}
}
}
GoogleSheetTestScript.gs 소스입니다
var sheetId = SpreadsheetApp.openById("1jGYyGFAGbcJHFPALaFO21Yj07kYCNi-DZm_TIPs4jEo");
var sheet = sheetId.getSheets()[0];
var logSheet = sheetId.getSheets()[1];
var cache = CacheService.getUserCache();
var p;
var result, msg, value;
function removeCache()
{
cache.removeAll(["id", "row"]);
}
function response()
{
var json = {};
json.order = p.order;
json.result = result;
json.msg = msg;
json.value = value;
jsonData = JSON.stringify(json);
logSheet.appendRow([cache.get("id") + ", " + jsonData.toString() + ", " + new Date().toString()]);
return ContentService.createTextOutput(jsonData);
}
function regCheck(val)
{
var regExp = /[\{\}\[\]\/?.,;"|\)*~`!^\-+<>@\#$%&\\\=\(\'\"]/gi;
if(regExp.test(val)) return false;
else return true;
}
function doPost(e)
{
p = e.parameter;
if(!regCheck(p.order) || !regCheck(p.id) || !regCheck(p.pass) || !regCheck(p.value))
return ContentService.createTextOutput("");
switch(p.order)
{
case "register": removeCache(); register(); break;
case "login": removeCache(); login(); break;
case "logout": removeCache(); break;
case "setValue": setValue(); break;
case "getValue": getValue(); break;
}
return response();
}
function setResult(_result, _msg)
{
result = _result;
msg = _msg;
}
function register()
{
var cell = sheet.getRange(2, 1, sheet.getLastRow() - 1, 1).getValues();
if(cell.some(row => row == p.id))
{
setResult("ERROR", "이미 존재하는 닉네임입니다");
return;
}
sheet.appendRow([p.id, p.pass]);
setResult("OK", "회원가입 완료");
}
function getProfile()
{
var cell = sheet.getRange(2, 1, sheet.getLastRow() - 1 , 2).getValues();
for(i = 0; i < cell.length; i++)
{
if(cell[i][0] != p.id || cell[i][1] != p.pass)
continue;
cache.put("id", cell[i][0]);
cache.put("row", (i + 2).toString());
return true;
}
return false;
}
function login()
{
if(!getProfile())
{
setResult("ERROR", "로그인 실패");
return;
}
setResult("OK", "로그인 완료");
}
function setValue()
{
if(cache.get("row") == null)
{
setResult("ERROR", "다시 로그인 해주세요");
return;
}
sheet.getRange(cache.get("row"), 3).setValue(p.value);
setResult("OK", cache.get("id") + "님이 " + p.value + "값 저장 완료");
}
function getValue()
{
if(cache.get("row") == null)
{
setResult("ERROR", "다시 로그인 해주세요");
return;
}
value = sheet.getRange(cache.get("row"), 3).getValue();
setResult("OK", cache.get("id") + "님이 값 불러오기 완료");
}