Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - loop
- Algorithm
- 백준
- Unity
- DP
- Material
- dfs
- Tutorial
- Basic
- String
- UE5
- 오류
- guide
- Class
- github
- parameter
- 재귀
- dynamic
- Programming
- W3Schools
- Unreal Engine 5
- 기초
- w3school
- 파이썬
- 문제풀이
- c++
- 시작해요 언리얼 2022
- python
- C#
- 프로그래밍
                            Archives
                            
                        
                          
                          - Today
- Total
행복한 개구리
Unity UGUI 21.05.02. 과제 겸 복습 본문
UI로비창 - 로그인 - 회원가입
ㄴ 상점
ㄴ 골드
ㄴ 젬
ㄴ 소울젬
식으로 구현했고 프로필의 슬라이더와 인포매니저에서 정보불러오기는 구현하지 못했다.
인포매니저에서 정보를 불러오려했지만 처음에 로그인할 때 정보불러오려는 생각이 머릿속에서 꼬여서 다른 부분 먼저 구현했다.

계획대로라면 회원가입 창에서 받은 이메일 이름 패스워드를 info에 바로 넣어 저장시킨 뒤 로그인을 구현하려 했지만 못했다.
하지만 자원들이나 상점의 상품들은 데이터값에 따라 잘 연동이 됐다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class UIShopItemData
{
    public int id;
    public string sp_name;
    public int amount;
    public float price;
    public string price_icon;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UISignUp : MonoBehaviour
{
    public InputField email;
    public InputField userName;
    public InputField pw;
    public Button signUp;
    public Button close;
    public Button signIn;
    void Start()
    {
        
    }
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
using System.IO;
public class InfoManager 
{
    public Dictionary<int, UserInfo> dicUserInfo;
    public Dictionary<int, BudgetInfo> dicBudgetInfo;
    public List<UserInfo> listUserInfos = new List<UserInfo>();
    public List<BudgetInfo> listBudgetInfos = new List<BudgetInfo>();
    static InfoManager instance;
    public static InfoManager GetInstance()
    {
        if(instance == null)
        {
            instance = new InfoManager();
        }
        return instance;
    }
    public void LoadInfos() 
    {
        DataManager.GetInstance().LoadDatas();
        var userPath = string.Format("{0}/user_info.json", Application.persistentDataPath);
        var budgetPath = string.Format("{0}/budget_info.json", Application.persistentDataPath);
        if (File.Exists(userPath))
        {
            var json = File.ReadAllText(userPath);
            this.dicUserInfo = JsonConvert.DeserializeObject<UserInfo[]>(json).ToDictionary(x => x.id);
            var budgetJson = File.ReadAllText(budgetPath);
            this.dicBudgetInfo = JsonConvert.DeserializeObject<BudgetInfo[]>(budgetJson).ToDictionary(x => x.id);
        }
        else
        {
            var data = DataManager.GetInstance().dicUserData[1];
            var newbie = new UserInfo(data.id, null, null, null, data.lvl, data.hp, data.exp, data.budget_id);
            
            this.listUserInfos.Add(newbie);
            var json = JsonConvert.SerializeObject(this.listUserInfos);
            File.WriteAllText(userPath, json);
            var budgetData = DataManager.GetInstance().dicBudgetData[data.budget_id];
            var budget = new BudgetInfo(budgetData.id, budgetData.h_amount, budgetData.c_amount, budgetData.g_amount);
            this.listBudgetInfos.Add(budget);
            var budgetJson = JsonConvert.SerializeObject(this.listBudgetInfos);
            File.WriteAllText(budgetPath, budgetJson);
            LoadInfos();
        }
    }
}
else의 마지막 부분처럼 데이터를 받아와 아이티를 생성 한 뒤에 다시 정보를 불러오려 했지만 미구현..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class UIShopItem : MonoBehaviour
{
    public SpriteAtlas atlas;
    public Button buy;
    public Text price;
    public GameObject hotGo;
    public GameObject bestGo;
    public Image icon;
    public Text amount;
    public Image price_icon;
    public void Init(UIShopItemData data)
    {
        this.icon.sprite = this.atlas.GetSprite(data.sp_name);
        this.icon.SetNativeSize();
        this.price.text = data.price.ToString();
        this.amount.text = data.amount.ToString();
        this.price_icon.sprite = this.atlas.GetSprite(data.price_icon);
        this.price_icon.SetNativeSize();
        this.buy.onClick.AddListener(() => { Debug.Log(this.price.text); });
    }
}
SetNativeSize를 해주어야 알맞은 그림이 배치됐다.(꼭 그런것 만은 아님 이번에 쓴 소스가 그랬던 것일 뿐)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour
{
    public UILobby lobby;
    public UILogin login;
    public UIShop shop;
    public UISignUp signUp;
    UserInfo userInfo;
    public void Init()
    {
        DataManager.GetInstance().LoadDatas();
        InfoManager.GetInstance().LoadInfos();
        this.lobby.Init();
        this.shop.Init();
        var dicUserInfos = InfoManager.GetInstance().dicUserInfo;
        this.lobby.login.onClick.AddListener(() => this.login.gameObject.SetActive(true));
        this.lobby.menus[(int)UILobby.eMenuType.Shop].btn.onClick.AddListener(() => { this.shop.gameObject.SetActive(true); });
        this.shop.close.onClick.AddListener(() => this.shop.gameObject.SetActive(false));
        this.login.close.onClick.AddListener(() => this.login.gameObject.SetActive(false));
        this.login.signup.onClick.AddListener(() => this.signUp.gameObject.SetActive(true));
        this.signUp.close.onClick.AddListener(() =>
        {
            this.signUp.gameObject.SetActive(false);
            this.login.gameObject.SetActive(false);
        });
        this.signUp.signIn.onClick.AddListener(() => this.signUp.gameObject.SetActive(false));
    }
    void Start()
    {
        Init();
    }
}
UIManager를 둬서 여기서 창을 닫고 끄는 작업을 해줬다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UILobby : MonoBehaviour
{
    public UIItemBudget[] budgets;
    public UIMenuItem[] menus;
    public Button login;
    public Button fb;
    public Button profile;
    public enum eMenuType
    {
        Item, Shop, Message, Mission, Ranking, Setting
    }
    public enum eBudgetType
    {
        Heart, Coin, Gem
    }
    public void Init()
    {
        DataManager.GetInstance().LoadDatas();
        /*var budgetId = info.budget_id;*/
        var budgetId = DataManager.GetInstance().dicUserData[1].budget_id;
        
        var budgetData = DataManager.GetInstance().dicBudgetData[budgetId];
        Debug.Log(budgetData.c_amount);
        for (int i = 0; i < this.menus.Length; i++)
        {
            var btn = this.menus[i].btn;
            var idx = i;
            btn.onClick.AddListener(() => { Debug.Log((eMenuType)idx); });
        }          
        var heartBudget = (UIItemHeartBudget)GetUIBudget(eBudgetType.Heart);
        heartBudget.Max = 5;
        heartBudget.Init(budgetData.h_amount);
        var coinBudget = GetUIBudget(eBudgetType.Coin);
        coinBudget.Init(budgetData.c_amount);
        var gemBudget = GetUIBudget(eBudgetType.Gem);
        gemBudget.Init(budgetData.g_amount);
    }
    public UIItemBudget GetUIBudget(eBudgetType type)
    {
        return this.budgets[(int)type];
    }
}
로비에서 각 자원들의 수량을 데이터값에 맞게 대입해주었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class UIShop : MonoBehaviour
{
    public Transform contents;
    public GameObject uiItemGo;
    public Button close;
    public GameObject[] onGo;
    public Button[] tabs;
    GameObject[] goods = new GameObject[4];
    int tabNum;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void Init()
    {
        this.onGo[0].SetActive(true);
        for (int i = 0; i < 4; i++)
        {
            var go = Instantiate(this.uiItemGo, this.contents);
            var uiItem = go.GetComponent<UIShopItem>();
            var data = DataManager.GetInstance().dicShopItemData[i + 1];
            uiItem.Init(data);            
            this.goods[i] = uiItem.gameObject;
        }
        foreach(var tab in this.tabs)
        {            
            tab.onClick.AddListener(()=> 
            {
                if (tab == this.tabs[0]) this.tabNum = 0;
                else if (tab == this.tabs[1]) this.tabNum = 1;
                else if (tab == this.tabs[2]) this.tabNum = 2;
                foreach(var on in this.onGo)
                {
                    on.SetActive(false);
                }
                this.onGo[this.tabNum].SetActive(true);
                
                
                for (int i = 0; i < 4; i++)
                {                    
                    var uiItem = this.goods[i].GetComponent<UIShopItem>();
                    var data = DataManager.GetInstance().dicShopItemData[(4 * this.tabNum) + i + 1];
                    uiItem.Init(data);                    
                }
            });
        }
    }
}
처음에 4개의 상품 오브젝트를 만들고 그 곳에 탭을 넘어갈 때마다 다른 데이터를 덧씌워 주었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIItemHeartBudget : UIItemBudget
{
    public Text time;
    public int Max { get; set; }
    public override void Init(int amount)
    {
        if (amount >= this.Max)
        {
            this.amount.text = this.Max.ToString();
            this.time.text = "FULL";
            base.Init(amount);
        }
        else
        {
            base.Init(amount);            
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class UIItemBudget : MonoBehaviour
{   
    public Text amount;
    public Button btn;
       
    public virtual void Init(int amount)
    {        
        this.amount.text = amount.ToString();
        this.btn.onClick.AddListener(() => { Debug.Log(amount); });
    }    
}상속을 사용했다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UILogin : MonoBehaviour
{
    public InputField email;
    public InputField pw;
    public Button login;
    public Button signup;
    public Button close;
        
    public void Init(UserInfo info)
    {
        this.login.onClick.AddListener(() => { Debug.Log("login"); });
        this.signup.onClick.AddListener(() => { Debug.Log("signUp"); });
    }    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIProfile : MonoBehaviour
{
    public Button btn;
    public Text lvl;
    public Text userName;
    public Slider hp;
    public Slider exp;
    
    public void Init(UserInfo info)
    {
        this.userName.text = info.name;
        this.lvl.text = info.lvl.ToString();
        var maxHp = info.lvl * 50;
        this.hp.value = 32 / maxHp;
        Debug.Log(this.hp.value);
        var maxExp = info.exp * 100;
        this.exp.value = info.exp / maxExp;
    }
}
구현하다 만 프로필이다..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
public class DataManager
{
    public Dictionary<int, BudgetData> dicBudgetData;
    public Dictionary<int, UserData> dicUserData;
    public Dictionary<int, UIShopItemData> dicShopItemData;
    static DataManager instance;    
    public static DataManager GetInstance()
    {
        if(instance == null)
        {
            instance = new DataManager();
        }
        return instance;
    }
    
    public void LoadDatas()
    {
        var budgetTa = Resources.Load<TextAsset>("Datas/budget_data");
        var budgetJson = budgetTa.text;
        this.dicBudgetData = JsonConvert.DeserializeObject<BudgetData[]>(budgetJson).ToDictionary(x => x.id);
        var userTa = Resources.Load<TextAsset>("Datas/user_data");
        var userJson = userTa.text;
        this.dicUserData = JsonConvert.DeserializeObject<UserData[]>(userJson).ToDictionary(x => x.id);
        var shopTa = Resources.Load<TextAsset>("Datas/shop_item_data");
        var shopJson = shopTa.text;
        this.dicShopItemData = JsonConvert.DeserializeObject<UIShopItemData[]>(shopJson).ToDictionary(x => x.id);
    }
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
public class UIMenuItem : MonoBehaviour
{    
    public Button btn;    
}
Simple-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BudgetData
{
    public int id;
    public int h_amount;
    public int c_amount;
    public int g_amount;
}
--------------------------------------
아쉬운게 너무 많다. 과정내에 다시 볼 수 있을 기회가 있을 진 모르겠지만 꼭 다시 해보고싶다. 특히 데이터불러오기.
'Unity > 복습' 카테고리의 다른 글
| Unity 복습 21.05.04. UGUI Daily Reward 구현 (0) | 2021.05.05 | 
|---|---|
| Unity 복습 21.05.03. 데이터 저장&불러오기 // 쉐이더 (0) | 2021.05.03 | 
| Unity UGUI 21.05.01 과제 겸 복습 (0) | 2021.05.02 | 
| Unity 21.04.28. 개념복습 (0) | 2021.04.28 | 
| Unity 개념복습 21.04.27 (0) | 2021.04.27 | 
 
           
                   
                   
                  