using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using Unity.VisualScripting;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
private Main() { }
private static Main instance;
public static Main Instance
{
get
{
// 인스턴스가 null인 경우 새로 생성
if (instance == null)
{
instance = new Main();
// 기존의 Start 메서드 내용을 Init 메서드로 옮김
instance.Init();
}
return instance;
}
}
public SpriteAtlas equipmentAtlas;
[SerializeField] private Image weapon;
[SerializeField] private Image armor;
[SerializeField] private Image helmet;
[SerializeField] private Image shield;
[SerializeField] private Image accessaries;
public EquipmentData[] equipmentDatas;
private int id;
void Init()
{
TextAsset asset = Resources.Load<TextAsset>("data/equipment_data");
string json = asset.text;
//Debug.Log(json);
//현재 문자열을 객체로 역직렬화
equipmentDatas = JsonConvert.DeserializeObject < EquipmentData[] >(json);
EquipmentData foundData = this.GetDataById(1002);
//foreach (EquipmentData equipment in equipmentDatas)
//{
//JSON 데이터에서 해당 필드가 비어있거나 값이 없다면, 해당 필드는 역직렬화될 때 0으로 초기화
//Debug.LogFormat("{0}, {1}, {2}, {3}, {4}", equipment.id, equipment.name, equipment.type, equipment.damage, equipment.defense);
//}
Debug.LogFormat("{0}, {1}, {2}, {3}", foundData.id, foundData.name, foundData.type, foundData.defense);
}
public EquipmentData GetDataById(int id)
{
EquipmentData foundData = null;
this.id = id;
for(int i = 0; i < this.equipmentDatas.Length; i++)
{
EquipmentData equipmentData = this.equipmentDatas[i];
if(equipmentData.id == id)
{
foundData = equipmentData;
break;
}
}
return foundData;
}
public Sprite GetSpriteImg(string imgName)
{
EquipmentData equipmentData = null;
foreach (var data in equipmentDatas)
{
if (data.name == imgName)
{
equipmentData = data;
break;
}
}
if (equipmentData != null)
{
string imagePath = "img/" + equipmentData.name;
Sprite sprite = Resources.Load<Sprite>(imagePath);
return sprite;
}
else
{
Debug.LogError("Equipment data with name " + imgName + " not found.");
return null;
}
}
public void LoadAndSetImage(Image imageComponent, string imgName)
{
Sprite sprite = GetSpriteImg(imgName);
if (sprite != null)
{
imageComponent.sprite = sprite;
}
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Weapon : MonoBehaviour
{
public TMP_Text idText;
public Image weaponImg;
private void Start()
{
//Sprite weaponSprite = Main.Instance.equipmentAtlas.GetSprite("weaponA");
//weaponImg.sprite = weaponSprite;
//Sprite sprite = Resources.Load<Sprite>("img/weaponA");
//weaponImg.sprite = sprite;
Main.Instance.LoadAndSetImage(weaponImg, "weaponA");
//weaponImg =
idText.text = Main.Instance.GetDataById(1000).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Shield : MonoBehaviour
{
public TMP_Text idText;
public Image shieldImg;
private void Start()
{
Main.Instance.LoadAndSetImage(shieldImg, "shieldA");
idText.text = Main.Instance.GetDataById(1001).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Armor : MonoBehaviour
{
public TMP_Text idText;
public Image armorImg;
private void Start()
{
Main.Instance.LoadAndSetImage(armorImg, "armorA");
idText.text = Main.Instance.GetDataById(1003).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Accessaries : MonoBehaviour
{
public TMP_Text idText;
public Image accessoryImg;
private void Start()
{
Main.Instance.LoadAndSetImage(accessoryImg, "accessoryA");
idText.text = Main.Instance.GetDataById(1004).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Helmet : MonoBehaviour
{
public TMP_Text idText;
public Image helmetImg;
private void Start()
{
Main.Instance.LoadAndSetImage(helmetImg, "helmetA");
idText.text = Main.Instance.GetDataById(1002).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentData
{
public int id;
public string name;
public string type;
public int damage;
public int defense;
}
'Study > Unity' 카테고리의 다른 글
[Unity] 클릭시 색상 변경 및 Select버튼 On/Off (0) | 2024.06.09 |
---|---|
[Unity] Random Skill Button & Select Button (0) | 2024.06.08 |
[Unity] 클릭시 json에 있는 파일들 랜덤으로 가져오기 (0) | 2024.06.07 |
[Unity] Coroutine의 null 과 break의 차이 (0) | 2024.06.07 |
[Unity] 스킬 레벨업 창 만들기 (0) | 2024.06.05 |