본문 바로가기
유니티 심화

UI Chest Shop(수정)

by 노재두내 2023. 9. 8.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test05Main : MonoBehaviour
{
    [SerializeField]
    private UIChestScrollView chestScrollView;
    // Start is called before the first frame update
    void Start()
    {
        DataManager.instance.LoadChestData();
        this.chestScrollView.Init();
    }

}​
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChestData
{
    public int id;
    public string name;
    public int type;
    public int price;
    public string sprite_name;
}​
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCellAd : MonoBehaviour
{
    [SerializeField]
    private Button btnAd;

    // Start is called before the first frame update
    void Start()
    {
        this.btnAd.onClick.AddListener(() =>
        {
            Debug.LogFormat("광고보기");
        });
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCell : MonoBehaviour
{
    public enum eChestType
    {
        Wooden,Silver,Golden,Epic,Legendary
    }
    [SerializeField]
    private Button btnPrice;
    [SerializeField]
    private eChestType chestType;
    [SerializeField]
    private int price;
    // Start is called before the first frame update
    void Start()
    {
        this.btnPrice.onClick.AddListener(() =>
        {
            Debug.LogFormat("상자타입:{0},가격:{1}",this.chestType,this.price);
        });
    }
 }

 

상속 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private UIChestCell[] cells;
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < cells.Length; i++)
        {
            UIChestCell cell = cells[i];
            cell.onClickPrice = () =>
            {
                Debug.LogFormat("상자타입:{0},가격:{1}", cell.ChestType, cell.Price);
            };

            UIChestCellAd cellAd = cell as UIChestCellAd;
            if (cellAd != null)
            {
                cellAd.onClickAd = () =>
                {
                    Debug.LogFormat("{0}, 광고보기", cell.ChestType);
                };
            }
            cell.Init();
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCellAd : UIChestCell
{
    [SerializeField]
    private Button btnAd;
    public System.Action onClickAd;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public override void Init()
    {
        base.Init();
        
        this.btnAd.onClick.AddListener(() =>
        {
            //Debug.LogFormat("{0}: 광고보기", this.chestType);
            this.onClickAd();
        });
    }
    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCell : MonoBehaviour
{
    public enum eChestType
    {
        Wooden,Silver,Golden,Epic,Legendary
    }
    [SerializeField]
    protected Button btnPrice;
    [SerializeField]
    protected eChestType chestType;
    [SerializeField]
    protected int price;
    public System.Action onClickPrice;

    public eChestType ChestType
    {
        get
        {
            return this.chestType;
        }
    }
    public int Price
    {
        get
        {
            return this.price;
        }
    }
    //[SerializeField]
    //private UIChestCellAd uiChestCellAd;
    // Start is called before the first frame update
  
    public virtual void Init()
    {
        this.btnPrice.onClick.AddListener(() =>
        {
            //Debug.LogFormat("상자타입:{0},가격:{1}", this.chestType, this.price);
            onClickPrice();
        });
    }
    // Update is called once per frame
    
}

 

 

 

<데이터 연동>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
//using System.Linq;

public class DataManager : MonoBehaviour
{
    public static readonly DataManager instance = new DataManager();
    // Start is called before the first frame update
    public void LoadChestData()
    {
        TextAsset asset = Resources.Load<TextAsset>("Resources/chest_data");
        Debug.Log(asset.text);
    }

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

public class ChestData : MonoBehaviour
{
    public int id;
    public string name;
    public int type;
    public int price;
    public string sprite_name;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test05Main : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        DataManager.instance.LoadChestData();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

->

오류남 

 

해결됨

 

<역직렬화>

DataManager.cs

ChestData[] chestDatas = JsonConvert.DeserializeObject<ChestData[]>(asset.text);
        foreach(ChestData data in chestDatas)
        {
            Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.type, data.price, data.sprite_name);
        }

 

Init 추가

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test05Main : MonoBehaviour
{
    [SerializeField]
    private UIChestScrollView chestScrollView;
    // Start is called before the first frame update
    void Start()
    {
        DataManager.instance.LoadChestData();
        this.chestScrollView.Init();
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Runtime.InteropServices.WindowsRuntime;
using Newtonsoft.Json.Bson;
using System.Linq;


public class DataManager :MonoBehaviour
{
    public static readonly DataManager instance = new DataManager();
    private Dictionary<int, ChestData> dicChestDatas = new Dictionary<int, ChestData>();
    // Start is called before the first frame update
    private DataManager()
    {

    }
    public void LoadChestData()
    {
        TextAsset asset = Resources.Load<TextAsset>("chest_data");
        Debug.Log(asset.text);

        ChestData[] chestDatas = JsonConvert.DeserializeObject<ChestData[]>(asset.text);
        foreach (ChestData data in chestDatas)
        {
            this.dicChestDatas.Add(data.id, data);
        }
        Debug.LogFormat("dicchestDatas:{0}", dicChestDatas.Count); 
    }

    public List<ChestData> GetChestDatas()
    {
        return this.dicChestDatas.Values.ToList();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private UIChestCell[] cells;
    
    // Start is called before the first frame update
    void Start()
    {
        //foreach (ChestData data in chestDatas)
        //{
        //    Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.type, data.price, data.sprite_name);
        //}

        for (int i = 0; i < cells.Length; i++)
        {
            UIChestCell cell = cells[i];
            cell.onClickPrice = () =>
            {
                Debug.LogFormat("상자타입:{0},가격:{1}", cell.ChestType, cell.Price);
            };

            UIChestCellAd cellAd = cell as UIChestCellAd;
            if (cellAd != null)
            {
                cellAd.onClickAd = () =>
                {
                    Debug.LogFormat("{0}, 광고보기", cell.ChestType);
                };
            }
            cell.Init();
        }
    }

    public void Init()
    {
        List<ChestData> chestDatas = DataManager.instance.GetChestDatas();
        foreach (ChestData data in chestDatas)
        {
            Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.type, data.price, data.sprite_name);
        }
    }

}

 

동적으로 생성은 되는데 문제가 많음 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class UIChestScrollView : MonoBehaviour
{
    [SerializeField]
    private UIChestCell[] cells;
    [SerializeField]
    private GameObject uiChestCellAdPrefab;
    [SerializeField]
    private GameObject uiChestCellPrefab;
    [SerializeField]
    private Transform content;
    [SerializeField]
    private SpriteAtlas atlas;

   // private List<UIChestCell> cellList = new List<UIChestCell>();
    
    // Start is called before the first frame update
   

    public void Init()
    {
        List<ChestData> chestDatas = DataManager.instance.GetChestDatas();
        foreach (ChestData data in chestDatas)
        { 
            
            Debug.LogFormat("{0} {1} {2} {3} {4}", data.id, data.name, data.type, data.price, data.sprite_name);
            Sprite sprite = this.atlas.GetSprite(data.sprite_name);
            if(data.type==0)
            {
                GameObject uiChestCellGo = Instantiate(this.uiChestCellAdPrefab, this.content);
                UIChestCell cell = uiChestCellGo.GetComponent<UIChestCell>();
                cell.Init(data, sprite);
            }
            else
            {
                GameObject uiChestCellGo = Instantiate(this.uiChestCellPrefab, this.content);
                UIChestCell cell = uiChestCellGo.GetComponent<UIChestCell>();
                cell.Init(data, sprite);
            }
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Runtime.InteropServices.WindowsRuntime;
using Newtonsoft.Json.Bson;
using System.Linq;


public class DataManager :MonoBehaviour
{
    public static readonly DataManager instance = new DataManager();
    private Dictionary<int, ChestData> dicChestDatas = new Dictionary<int, ChestData>();
    
    // Start is called before the first frame update
    private DataManager()
    {

    }
    public void LoadChestData()
    {
        TextAsset asset = Resources.Load<TextAsset>("chest_data");
        Debug.Log(asset.text);

        ChestData[] chestDatas = JsonConvert.DeserializeObject<ChestData[]>(asset.text);
        foreach (ChestData data in chestDatas)
        {
            this.dicChestDatas.Add(data.id, data);
        }
        Debug.LogFormat("dicchestDatas:{0}", dicChestDatas.Count); 
    }

    public List<ChestData> GetChestDatas()
    {
        return this.dicChestDatas.Values.ToList();
    }

    private Dictionary<int, MissionData> dicMissionDatas;
    public void LoadMissionData()
    {
        //Resources 폴더에서 mossion_data에셋(Test asset)을 로드
        TextAsset asset = Resources.Load<TextAsset>("mission_data");
        //가져오기
        //string json = asset.text;
        Debug.Log(asset.text);
        //역직렬화
        MissionData[] arrMissionDatas = JsonConvert.DeserializeObject<MissionData[]>(asset.text);
        Debug.LogFormat("arrMissionDatas:{0}", arrMissionDatas.Length);
        //사전에 넣기
        this.dicMissionDatas = arrMissionDatas.ToDictionary(x => x.id);
    }
    public List<MissionData> GetMissionDatas()
    {
        return this.dicMissionDatas.Values.ToList();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIChestCellAd : UIChestCell
{
    [SerializeField]
    private Button btnAd;
    public System.Action onClickAd;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public override void Init(ChestData data,Sprite sprite)
    {
        
        this.btnAd.onClick.AddListener(() =>
        {
            Debug.LogFormat("{0}: 광고보기", this.chestType);
            this.onClickAd();
        });
    }
    
}
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UIChestCell : MonoBehaviour
{
    public enum eChestType
    {
        Wooden,Silver,Golden,Epic,Legendary
    }
    [SerializeField]
    protected Button btnPrice;
    [SerializeField]
    protected eChestType chestType;
    [SerializeField]
    protected int price;
    [SerializeField]
    private TMP_Text txtPrice;
    [SerializeField]
    private TMP_Text txtName;
    [SerializeField]
    private Image icon;

    public System.Action onClickPrice;

    public eChestType ChestType
    {
        get
        {
            return this.chestType;
        }
    }
    public int Price
    {
        get
        {
            return this.price;
        }
    }
    
    // Start is called before the first frame update
  
    public virtual void Init(ChestData data,Sprite sprite)
    {
        //this.price = data.price;
        this.txtName.text = data.name;
        this.txtPrice.text = this.price.ToString();
        //this.chestType = (eChestType)data.type;
        this.icon.sprite = sprite;
        this.btnPrice.onClick.AddListener(() =>
        {
            Debug.LogFormat("상자타입:{0},가격:{1}", this.chestType, this.price);
            onClickPrice();
        });
    }
   
    
}

 

--> 아이콘을 잘못넣었음 

 

-->TextmeshPro 가 한글이라 못읽었던것? font asset creator로 만들고  font asset변경 후 해결 

 

 

'유니티 심화' 카테고리의 다른 글

유니티 절대강좌 챕터3  (0) 2023.09.25
UIPlayResult  (0) 2023.09.12
mission  (0) 2023.09.11
주말과제(Goldshop)  (0) 2023.09.11
ui 스테이지  (0) 2023.09.06