Unity/UI

uGUI 세팅 팝업 만들기

s0002 2023. 2. 20. 01:42

UserID 버튼 클릭하면 아이디 팝업이 뜬다

아이디 팝업창에서 아이디를 볼 수 있고 아이디 카피 버튼을 클릭하면

아이디를 클립보드에 복사할 수 있다

x 버튼 클릭하면 팝업이 닫힌다

 

Logout 버튼 클릭하면 로그아웃 되어서

다시 UserID 버튼을 클릭했을 때

아이디 팝업창에서 아이디와 아이디 카피 버튼이 사라지고

로그인하라는 메세지가 나타난다

 

나머지 항목들(스위치나 슬라이더)을 만들 때는 특이사항이 없었다

using UnityEngine;

public class GridScrollViewMain : MonoBehaviour
{
    public UIGridScrollViewDirector director;

    private void Start()
    {
        this.director.Init();
    }
}
using UnityEngine;
using UnityEngine.UI;

public class UISettingsDirector : MonoBehaviour
{
    public UISettingAlarm alarm;
    public UISettingSoundFx soundFx;
    public UISettingMusic music;
    public UISettingVibration vibration;

    public Button[] arrBtns;

    public UserIDPopup popup;

    public void Init()
    {
        Debug.Log("director Init");

        this.InitButtons();
        this.alarm.Init();
        this.vibration.Init();
        this.soundFx.Init();
        this.music.Init();

        this.popup.Init();
    }
    private void InitButtons()
    {
        int i = 0;
        foreach (var data in DataManager.instance.GetButtonDatas())
        {
            this.arrBtns[i].GetComponent<UISettingButtons>().Init(data);
            i++;
        }
    }
}
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class UISettingButtons : MonoBehaviour
{
    public TMP_Text txtName;
    public int id;

    public Button btn;
    public GameObject popup;    

    public void Init(ButtonData data)
    {        
        this.id = data.id;
        this.txtName.text = data.name;
        this.btn.onClick.AddListener(() => {
            Debug.LogFormat("click {0}", data.name);

            if (data.id == 104)
            {
                this.popup.SetActive(true);
            }
            if (data.id == 102)
            {
                this.popup.GetComponent<UserIDPopup>().userID = null;
            }
        });
    }
}
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public static class ClipBoardExtension
{
    public static void CopyToClipBoard(this string str)
    {
        GUIUtility.systemCopyBuffer = str;
    }
}

public class UserIDPopup : MonoBehaviour
{
    public TMP_Text txtUserID;
    public Button btnCopy;
    public string userID = "idanjfhgkfRk";
    public Button btnClose;
    public GameObject txtLoginPlease;   

    public void Init()
    {
        this.txtUserID.text = this.userID;
        this.btnCopy.onClick.AddListener(() => {
            ClipBoardExtension.CopyToClipBoard(this.userID);
        });
        this.btnClose.onClick.AddListener(() => {
            this.gameObject.SetActive(false);
        });
    }
    private void Update()
    {
        if (this.userID == null)
        {
            this.Logout();
        }
        else
        {
            this.Login();
        }           
    }

    private void Logout()
    {
        this.txtUserID.gameObject.SetActive(false);
        this.btnCopy.gameObject.SetActive(false);
        this.txtLoginPlease.SetActive(true);
    }
    private void Login()
    {
        this.txtLoginPlease.SetActive(false);
        this.txtUserID.gameObject.SetActive(true);
        this.btnCopy.gameObject.SetActive(true);
    }
}

 

참고

https://thatfrenchgamedev.com/785/unity-2018-how-to-copy-string-to-clipboard/

 

Unity - How to copy a string to Clipboard – That French Game Dev

Cross-platform way to copy a string to Clipboard in Unity. Using the GUIUtility class I'm going to show you how to copy a string into the user's Clipboard. This should work in Unity 2017 and beyond.

thatfrenchgamedev.com