C#/수업 과제
Unity) [유니티 UI] 로그인 UI 구현
HSH12345
2023. 2. 10. 02:43
App
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
private string version = "0.0.1";
private void Awake()
{
Object.DontDestroyOnLoad(this.gameObject);
}
void Start()
{
AsyncOperation oper = SceneManager.LoadSceneAsync("LoginScene");
oper.completed += (obj) =>
{
UILogin uiLogin = GameObject.FindObjectOfType<UILogin>();
uiLogin.Init(this.version);
};
}
}
- 다음 씬으로 버전정보(문자열)를 넘겨주는 기능을 구현하고 있으며 해당 오브젝트는 다른 씬에서도 파괴되지 않도록 하였다.
UILogin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class UILogin : MonoBehaviour
{
public Button[] loginBtns;
public Button guestLoginBtn;
public GameObject popupLoginError;
public TMP_Text txtVersion;
public GameObject popupGuest;
private GameObject remainedPopup;
void Start()
{
for(int i = 0; i < loginBtns.Length; i++)
{
int idx = i;
this.loginBtns[idx].onClick.AddListener(() =>
{
string platform = null;
if (idx == 0) platform = "Game center";
if (idx == 1) platform = "FaceBook";
if (idx == 2) platform = "Google";
if (this.remainedPopup == null)
{
var go = Instantiate(this.popupLoginError);
this.remainedPopup = go;
var txtGo = go.transform.Find("errorlogin").transform.Find("Text (TMP)").GetComponent<TMP_Text>();
txtGo.text = string.Format("You can't sign in with {0}", platform);
}
else
{
Destroy(this.remainedPopup.gameObject);
var go = Instantiate(this.popupLoginError);
this.remainedPopup = go;
var txtGo = go.transform.Find("errorlogin").transform.Find("Text (TMP)").GetComponent<TMP_Text>();
txtGo.text = string.Format("You can't sign in with {0}", platform);
}
});
}
this.guestLoginBtn.onClick.AddListener(() =>
{
Instantiate(this.popupGuest);
});
var popupGuestGo = popupGuest.GetComponent<UIPopupGuest>();
//popupGuestGo.toNextScene = () =>
//{
// SceneManager.LoadScene("TitleScene");
//};
}
public void Init(string nowVersion)
{
this.txtVersion.text = nowVersion;
}
}
- 클릭되는 버튼에따라 출력되는 메시지와 기능을 다르게 할 수 있도록 코드를 구성하였다.
- 팝업 메시지 오브젝트는 현재 새로 인스턴스화 된 오브젝트가 있다면 기존 인스턴스를 파괴하는 식으로 구현하였다.
- 해당 오브젝트가 생성되면 이전 스크립트에서 실행시킨 Init 메서드가 실행되면서 버전 정보가 저장된다.
UIPopupMessage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIPopupMessage : MonoBehaviour
{
public float existTime = 1.0f;
void Start()
{
StartCoroutine(WatiForTime());
}
IEnumerator WatiForTime()
{
yield return new WaitForSeconds(existTime);
Destroy(this.gameObject);
}
}
- 생성된 인스턴스들은 1초 후 제거된다.
UIPopupGuest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIPopupGuest : MonoBehaviour
{
public Button btnYes;
public Button btnNo;
public System.Action toNextScene;
void Start()
{
this.btnNo.onClick.AddListener(() =>
{
Destroy(this.gameObject);
});
this.btnYes.onClick.AddListener(() =>
{
SceneManager.LoadScene("TitleScene");
//this.toNextScene();
});
}
}
- 버튼 기능을 구현하였다.
아쉬웠던 점은 로그인씬에서 로딩씬으로 넘어가기 위해 팝업 메시지를 사용하고 해당 팝업의 버튼이 클릭되었을 때 UILogin 클래스에서 정의된 무명메서드를 UIPopupGuest 클래스의 액션대리자에 저장하여 다음 씬으로 넘어가는 기능을 구현하고 싶었지만 실패하였고 UIPopupGuest 클래스에서 SceneManagement를 새로 선언하여 기능을 구현하였다. 구조적으로 마음에 들지 않는다.