201 lines
10 KiB
C#
201 lines
10 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TKS.OutGame.Net
|
|
{
|
|
/// <summary>
|
|
/// 기기의 소셜 로그인(구글 플레이 계정 · 애플 게임센터 계정) — 서버에 보낼 증표를 받아 온다.
|
|
/// 지금은 **개발용**(에디터 · 개발 빌드)만 있다: 입력한 번호를 그 번호의 소셜 계정으로 쓰는 시험용 증표(dev:번호 — 서버가 시험용 소셜 계정을 받는 설정일 때만 통한다).
|
|
/// 실제 기기 연동(구글 플레이 게임즈 · 게임센터 SDK)은 추후 — 그때 SignIn 안만 바꾸면 로그인 · 연동 · 계정 고르기 화면은 그대로다.
|
|
/// </summary>
|
|
public static class SocialSignIn
|
|
{
|
|
public const string Google = "google";
|
|
public const string Apple = "apple";
|
|
|
|
/// <summary>서버 주소의 플랫폼 이름(google · appstore).</summary>
|
|
public static string PlatformPath(string method)
|
|
{
|
|
return method == Apple ? "appstore" : "google";
|
|
}
|
|
|
|
/// <summary>화면에 보여 줄 이름.</summary>
|
|
public static string Label(string method)
|
|
{
|
|
return method == Google ? "Google Play" : method == Apple ? "Game Center" : "게스트";
|
|
}
|
|
|
|
/// <summary>개발용 소셜 로그인을 쓰는가(에디터 · 개발 빌드) — 실제 기기 연동이 들어오기 전까지 출시 빌드에서는 소셜 로그인 버튼이 보이지 않는다.</summary>
|
|
public static bool DevMode
|
|
{
|
|
get { return Application.isEditor || Debug.isDebugBuild; }
|
|
}
|
|
|
|
/// <summary>이 기기에서 그 소셜 로그인을 쓸 수 있는가 — 안드로이드 = 구글 플레이 · iOS = 게임센터 · 에디터 = 둘 다(개발용).</summary>
|
|
public static bool Available(string method)
|
|
{
|
|
if (!DevMode) return false; // 실제 기기 연동 전
|
|
if (Application.isEditor) return true;
|
|
if (Application.platform == RuntimePlatform.Android) return method == Google;
|
|
if (Application.platform == RuntimePlatform.IPhonePlayer) return method == Apple;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>소셜 로그인 — done(증표, 실패 사유). 개발용: 번호(영문 · 숫자 · _ . -)가 비었으면 실패.</summary>
|
|
public static void SignIn(string method, string devId, Action<string, string> done)
|
|
{
|
|
if (!Available(method))
|
|
{
|
|
done(null, Label(method) + " 로그인은 준비 중입니다");
|
|
return;
|
|
}
|
|
string id = (devId ?? "").Trim();
|
|
if (id.Length == 0 || id.Length > DevIdMax || !ValidId(id))
|
|
{
|
|
done(null, "시험용 소셜 계정 번호를 넣어 주세요 (영문 · 숫자 · _ . - · " + DevIdMax + "자까지)");
|
|
return;
|
|
}
|
|
done("dev:" + id, null);
|
|
}
|
|
|
|
public const int DevIdMax = 40;
|
|
|
|
static bool ValidId(string id)
|
|
{
|
|
foreach (char c in id)
|
|
if (!(char.IsLetterOrDigit(c) && c < 128) && c != '_' && c != '.' && c != '-') return false;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 로그인 화면 — 타이틀 위에 뜬다(접속 일꾼 ServerSync 가 띄운다). 게스트로 시작 · 소셜 계정으로 로그인.
|
|
/// 기기에 저장한 로그인이 있으면 이 화면 없이 마지막에 로그인한 계정으로 바로 들어간다 — 이 화면은 처음 실행 · 로그아웃 · 계정 삭제 · 세션이 끊긴 뒤에만 뜬다.
|
|
/// 가끔만 뜨는 화면이라 자기 캔버스 1개(사건 때만 바뀌는 층)에 그린다.
|
|
/// </summary>
|
|
public class LoginPanel : MonoBehaviour
|
|
{
|
|
// 접속 안내(990) 바로 아래 · 타이틀 전환(960) 위
|
|
const int Order = 985;
|
|
const float Plane = 0.32f;
|
|
const float PanelW = 880f;
|
|
const float ButtonW = 720f;
|
|
const float ButtonH = 120f;
|
|
const float Gap = 24f;
|
|
|
|
public static LoginPanel Current { get; private set; }
|
|
|
|
/// <summary>안내 글(로그인 실패 사유 · 세션이 끊긴 이유) — 없으면 "".</summary>
|
|
public string Notice { get; private set; }
|
|
public InputField DevId { get; private set; }
|
|
|
|
Action<string, string> onChoose;
|
|
|
|
/// <summary>로그인 화면을 띄운다. onChoose(방식, 시험용 소셜 계정 번호) — 방식 = guest · google · apple.</summary>
|
|
public static LoginPanel Show(Transform parent, Camera cam, string lastMethod, string lastId, string notice, Action<string, string> onChoose)
|
|
{
|
|
Close();
|
|
var go = new GameObject("LoginPanel");
|
|
go.transform.SetParent(parent, false);
|
|
var p = go.AddComponent<LoginPanel>();
|
|
p.onChoose = onChoose;
|
|
p.Notice = notice ?? "";
|
|
p.Build(cam, lastMethod, lastId);
|
|
Current = p;
|
|
return p;
|
|
}
|
|
|
|
public static void Close()
|
|
{
|
|
if (Current == null) return;
|
|
Destroy(Current.gameObject);
|
|
Current = null;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (Current == this) Current = null;
|
|
}
|
|
|
|
/// <summary>고름(버튼 · 테스트) — devId 를 주지 않으면 입력 칸의 값을 쓴다.</summary>
|
|
public void Choose(string method, string devId = null)
|
|
{
|
|
if (onChoose == null) return;
|
|
var done = onChoose;
|
|
onChoose = null; // 한 번만
|
|
done(method, devId ?? (DevId != null ? DevId.text : ""));
|
|
}
|
|
|
|
void Build(Camera cam, string lastMethod, string lastId)
|
|
{
|
|
UiLayer.Root(gameObject, cam, Order, Plane, OutGameRoot.Reference, UiRate.Event);
|
|
var dim = UiKit.Fill("Dim", transform);
|
|
var dimImg = dim.gameObject.AddComponent<Image>();
|
|
dimImg.color = new Color(0f, 0f, 0f, 0.6f);
|
|
dimImg.raycastTarget = true; // 아래 화면(타이틀) 누름 막기
|
|
|
|
bool google = SocialSignIn.Available(SocialSignIn.Google);
|
|
bool apple = SocialSignIn.Available(SocialSignIn.Apple);
|
|
bool dev = SocialSignIn.DevMode && (google || apple);
|
|
int buttons = 1 + (google ? 1 : 0) + (apple ? 1 : 0);
|
|
float noticeH = Notice.Length > 0 ? 130f : 0f;
|
|
float devH = dev ? 150f : 0f;
|
|
float h = 210f + noticeH + buttons * (ButtonH + Gap) + devH + 90f;
|
|
|
|
var center = new Vector2(0.5f, 0.5f);
|
|
var top = new Vector2(0.5f, 1f);
|
|
var panel = UiKit.Box("Panel", dim, center, center, Vector2.zero, new Vector2(PanelW, h), UiKit.Paper);
|
|
panel.raycastTarget = true;
|
|
UiKit.Border(panel, UiKit.Line, 4f);
|
|
var pt = panel.transform;
|
|
UiKit.Label(pt, "로그인", 52, UiKit.Ink, TextAnchor.MiddleCenter, top, top, new Vector2(0f, -36f), new Vector2(PanelW - 80f, 70f));
|
|
string last = string.IsNullOrEmpty(lastMethod) ? "게임을 시작할 계정을 골라 주세요" : "마지막 로그인: " + SocialSignIn.Label(lastMethod);
|
|
UiKit.Label(pt, last, 28, UiKit.InkSoft, TextAnchor.MiddleCenter, top, top, new Vector2(0f, -116f), new Vector2(PanelW - 80f, 44f), false);
|
|
float y = -190f;
|
|
if (Notice.Length > 0)
|
|
{
|
|
var n = UiKit.Label(pt, Notice, 28, UiKit.Red, TextAnchor.MiddleCenter, top, top, new Vector2(0f, y), new Vector2(PanelW - 100f, noticeH - 20f), false);
|
|
n.horizontalOverflow = HorizontalWrapMode.Wrap;
|
|
y -= noticeH;
|
|
}
|
|
y = AddButton(pt, "Guest", "게스트로 시작", UiKit.Dark, y, () => Choose(ServerClient.MethodGuest));
|
|
if (google) y = AddButton(pt, "Google", "Google Play 로 로그인", UiKit.Jade, y, () => Choose(SocialSignIn.Google));
|
|
if (apple) y = AddButton(pt, "Apple", "Game Center 로 로그인", UiKit.Jade, y, () => Choose(SocialSignIn.Apple));
|
|
if (dev)
|
|
{
|
|
UiKit.Label(pt, "개발용 — 시험용 소셜 계정 번호(같은 번호 = 같은 계정)", 24, UiKit.InkSoft, TextAnchor.MiddleLeft, top, top, new Vector2(0f, y - 4f), new Vector2(ButtonW, 36f), false);
|
|
DevId = Field(pt, "DevId", "예: tester1", top, new Vector2(0f, y - 46f), new Vector2(ButtonW, 84f), SocialSignIn.DevIdMax);
|
|
DevId.text = lastId ?? "";
|
|
y -= devH;
|
|
}
|
|
UiKit.Label(pt, "게스트 계정은 이 기기에서만 이어 할 수 있습니다. 옵션에서 소셜 계정에 연동하면 다른 기기에서도 이어 할 수 있습니다.", 22, UiKit.InkSoft, TextAnchor.UpperCenter, top, top, new Vector2(0f, y - 4f), new Vector2(PanelW - 100f, 70f), false)
|
|
.horizontalOverflow = HorizontalWrapMode.Wrap;
|
|
}
|
|
|
|
static float AddButton(Transform parent, string name, string label, Color color, float y, Action onClick)
|
|
{
|
|
var top = new Vector2(0.5f, 1f);
|
|
UiKit.Btn(name, parent, label, 38, color, UiKit.White, top, top, new Vector2(0f, y), new Vector2(ButtonW, ButtonH), onClick);
|
|
return y - ButtonH - Gap;
|
|
}
|
|
|
|
/// <summary>한 줄 입력 칸(흰 상자 + 테두리 · 우편함의 쿠폰 입력과 같은 모양).</summary>
|
|
internal static InputField Field(Transform parent, string name, string hint, Vector2 anchor, Vector2 pos, Vector2 size, int limit)
|
|
{
|
|
var box = UiKit.Box(name, parent, anchor, anchor, pos, size, UiKit.White);
|
|
box.raycastTarget = true;
|
|
UiKit.Border(box, UiKit.Line, 2f);
|
|
var text = UiKit.FillLabel(box.transform, "", 34, UiKit.Dark, TextAnchor.MiddleLeft, false, 20f); // 흰 칸 위 = 어두운 글자
|
|
text.supportRichText = false;
|
|
var ph = UiKit.FillLabel(box.transform, hint, 30, UiKit.InkSoft, TextAnchor.MiddleLeft, false, 20f);
|
|
var input = box.gameObject.AddComponent<InputField>();
|
|
input.textComponent = text;
|
|
input.placeholder = ph;
|
|
input.characterLimit = limit;
|
|
input.lineType = InputField.LineType.SingleLine;
|
|
return input;
|
|
}
|
|
}
|
|
}
|