카테고리 없음

[UI] 주말과제 상점 인터페이스

송현호 2023. 9. 11. 00:45
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class UIShopUnit : MonoBehaviour
{
    [SerializeField]
    private TMP_Text name;
    [SerializeField]
    private Image item;
    [SerializeField]
    private TMP_Text reward;
    [SerializeField]
    private TMP_Text price;
    [SerializeField]
    private Button purButton;

    public System.Action onPurchase;

    private UnitData unitData;

    
    public void Init(UnitData data, Sprite sprite)
    {
        this.unitData = data;
        this.name.text = data.name;
        this.reward.text = data.reward.ToString();
        this.price.text = data.price.ToString();
        this.item.sprite = sprite;

        purButton.onClick.AddListener(() =>
        {
            onPurchase();
            Debug.LogFormat("{0}원 지불",unitData.price);
        });
    }

    public int GetReward()
    {
        return unitData.reward;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class UIShopGoldScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject[] goldUnit;
    [SerializeField]
    private Transform content;
    [SerializeField]
    private SpriteAtlas goldSprite;

    public System.Action<int> onPurchase;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void Init()
    {
        var goldDatas = DataManager.instance.GetDicGoldDatas();
        var sprite = goldSprite;
        for (int i = 0; i < goldDatas.Count; i++)
        {
            if (i == 0)
            {
                var go = Instantiate(goldUnit[1], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(goldDatas[i + 100], sprite.GetSprite(goldDatas[i+100].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
            else if (i == 2)
            {
                var go = Instantiate(goldUnit[2], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(goldDatas[i + 100], sprite.GetSprite(goldDatas[i+100].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
            else
            {
                var go = Instantiate(goldUnit[0], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(goldDatas[i+ 100], sprite.GetSprite(goldDatas[i+100].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
        }
        
    }

    public int GetReward(UIShopUnit unit)
    {
        return unit.GetReward();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class UIShopGemScrollView : MonoBehaviour
{
    [SerializeField]
    private GameObject[] gemUnit;
    [SerializeField]
    private Transform content;
    [SerializeField]
    private SpriteAtlas gemSprite;

    public System.Action<int> onPurchase;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void Init()
    {
        var gemDatas = DataManager.instance.GetDicGemDatas();
        var sprite = gemSprite;
        for (int i = 0; i < gemDatas.Count; i++)
        {
            if (i == 0)
            {
                var go = Instantiate(gemUnit[1], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(gemDatas[i+1000], sprite.GetSprite(gemDatas[i+1000].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
            else if (i == 2)
            {
                var go = Instantiate(gemUnit[2], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(gemDatas[i+1000], sprite.GetSprite(gemDatas[i+1000].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
            else
            {
                var go = Instantiate(gemUnit[0], content);
                var unit = go.GetComponent<UIShopUnit>();
                unit.Init(gemDatas[i + 1000], sprite.GetSprite(gemDatas[i+1000].sprite_name));
                unit.onPurchase = () =>
                {
                    onPurchase(GetReward(unit));
                };
            }
        }
    }
    public int GetReward(UIShopUnit unit)
    {
        return unit.GetReward();
    }
    

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Newtonsoft.Json;
using System.IO;

public class UIShopMain : MonoBehaviour
{
    [SerializeField]
    private UIShopGemScrollView uIShopGemScrollView;
    [SerializeField]
    private UIShopGoldScrollView uIShopGoldScrollView;
    [SerializeField]
    private Button[] btns;
    [SerializeField]
    private GameObject[] lineFocuses;
    [SerializeField]
    private TMP_Text goldText;
    [SerializeField]
    private TMP_Text gemText;

    private int gold;
    private int gem;


    // Start is called before the first frame update
    void Start()
    {
        this.Init();
        
    }

    private void Init()
    {
        DataManager.instance.OnLoadDatas();
        uIShopGemScrollView.Init();
        uIShopGoldScrollView.Init();
        for(int i =0;  i<btns.Length; i++)
        {
            int j = i;
            btns[j].onClick.AddListener(() =>
            {
                foreach(GameObject go in lineFocuses)
                {
                    go.SetActive(false);
                }
                lineFocuses[j].SetActive(true);
                if(j==0)
                {
                    uIShopGemScrollView.gameObject.SetActive(false);
                    uIShopGoldScrollView.gameObject.SetActive(false);
                }
                else if (j == 1)
                {
                    uIShopGemScrollView.gameObject.SetActive(true);
                    uIShopGoldScrollView.gameObject.SetActive(false);
                }
                else if (j == 2)
                {
                    uIShopGemScrollView.gameObject.SetActive(false);
                    uIShopGoldScrollView.gameObject.SetActive(true);
                }
            });
        }
        if (File.Exists(Application.persistentDataPath+"/Game_Info"))
        {
            //기존 유저이면
            Debug.Log("기존 유저입니다.");
            string json = File.ReadAllText(Application.persistentDataPath + "/Game_Info");
            var gameinfo = JsonConvert.DeserializeObject<GameInfo>(json);
            this.gold = gameinfo.gold;
            this.gem = gameinfo.gem;
        }
        else
        {
            //신규 유저이면
            Debug.Log("신규 유저입니다.");
            this.gold = 0;
            this.gem = 0;
        }

        this.goldText.text = gold.ToString();
        this.gemText.text = gem.ToString();

        uIShopGoldScrollView.onPurchase += HandleGoldPurchase;
        uIShopGemScrollView.onPurchase += HandleGemPurchase;


    }
    private void HandleGoldPurchase(int rewardAmount)
    {
        // 골드 증가
        this.gold += rewardAmount;
        this.goldText.text = gold.ToString();
        this.SaveGameInfo();
    }

    // 젬 구매 이벤트 핸들러
    private void HandleGemPurchase(int rewardAmount)
    {
        // 젬 증가
        this.gem += rewardAmount;
        this.gemText.text = gem.ToString();
        this.SaveGameInfo();
    }

    public void SaveGameInfo()
    {
        GameInfo info = new GameInfo();
        info.gold = this.gold;
        info.gem = this.gem;

        var json = JsonConvert.SerializeObject(info);
        File.WriteAllText(Application.persistentDataPath+"/Game_Info", json);
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

public class DataManager : MonoBehaviour
{
    public static readonly DataManager instance = new DataManager();

    Dictionary<int, UnitData> dicGoldDatas = new Dictionary<int, UnitData>();
    Dictionary<int, UnitData> dicGemDatas = new Dictionary<int, UnitData>();
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void OnLoadDatas()
    {
        var gold = Resources.Load("gold_data");
        var json = gold.ToString();
        var goldText = JsonConvert.DeserializeObject<UnitData[]>(json);
        foreach(UnitData unitData in goldText) {
            dicGoldDatas.Add(unitData.id,unitData);
        }

        var gem = Resources.Load("gem_data");
        var json2 = gem.ToString();
        var gemText = JsonConvert.DeserializeObject<UnitData[]>(json2);
        foreach (UnitData unitData in gemText)
        {
            dicGemDatas.Add(unitData.id, unitData);
        }
    }

    public Dictionary<int, UnitData> GetDicGoldDatas()
    {
        return dicGoldDatas;
    }
    public Dictionary<int, UnitData> GetDicGemDatas()
    {
        return dicGemDatas;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameInfo
{
    public int gold;
    public int gem;
}

 

 

만들면서 조금 아쉬웠던 점은 골드와 젬의 프리펩을 3가지로 나눠서 사용했는데 1개로 사용하고 상단 라벨을 활성 비활성화 하는게 좀 더 코드가 깔끔하게 나왔을것 같다.