GameManager.cs 소스입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
using UnityEngine.UI;
[System.Serializable]
public class Item
{
public Item(string _Type, string _Name, string _Explain, string _Number, bool _isUsing, string _Index)
{ Type = _Type; Name = _Name; Explain = _Explain; Number = _Number; isUsing = _isUsing; Index = _Index; }
public string Type, Name, Explain, Number, Index;
public bool isUsing;
}
public class GameManager : MonoBehaviour
{
public TextAsset ItemDatabase;
public List<Item> AllItemList, MyItemList, CurItemList;
public string curType = "Character";
public GameObject[] Slot, UsingImage;
public Image[] TabImage, ItemImage;
public Sprite TabIdleSprite, TabSelectSprite;
public Sprite[] ItemSprite;
public GameObject ExplainPanel;
public RectTransform[] SlotPos;
public RectTransform CanvasRect;
public InputField ItemNameInput, ItemNumberInput;
IEnumerator PointerCoroutine;
RectTransform ExplainRect;
void Start()
{
// 전체 아이템 리스트 불러오기
string[] line = ItemDatabase.text.Substring(0, ItemDatabase.text.Length - 1).Split('\n');
for (int i = 0; i < line.Length; i++)
{
string[] row = line[i].Split('\t');
AllItemList.Add(new Item(row[0], row[1], row[2], row[3], row[4] == "TRUE", row[5]));
}
Load();
ExplainRect = ExplainPanel.GetComponent<RectTransform>();
}
void Update()
{
// 마우스 위치를 캔버스 위치로 변환해 설명창을 띄움
RectTransformUtility.ScreenPointToLocalPointInRectangle(CanvasRect, Input.mousePosition, Camera.main, out Vector2 anchoredPos);
ExplainRect.anchoredPosition = anchoredPos + new Vector2(-180, -165);
}
public void GetItemClick()
{
Item curItem = MyItemList.Find(x => x.Name == ItemNameInput.text);
ItemNumberInput.text = ItemNumberInput.text == "" ? "1" : ItemNumberInput.text;
if (curItem != null) curItem.Number = (int.Parse(curItem.Number) + int.Parse(ItemNumberInput.text)).ToString();
else
{
// 전체에서 얻을 아이템을 찾아 내 아이템에 추가
Item curAllItem = AllItemList.Find(x => x.Name == ItemNameInput.text);
if (curAllItem != null)
{
curAllItem.Number = ItemNumberInput.text;
MyItemList.Add(curAllItem);
}
}
MyItemList.Sort((p1, p2) => p1.Index.CompareTo(p2.Index));
Save();
}
public void RemoveItemClick()
{
Item curItem = MyItemList.Find(x => x.Name == ItemNameInput.text);
if (curItem != null)
{
int curNumber = int.Parse(curItem.Number) - int.Parse(ItemNumberInput.text == "" ? "1" : ItemNumberInput.text);
if (curNumber <= 0) MyItemList.Remove(curItem);
else curItem.Number = curNumber.ToString();
}
MyItemList.Sort((p1, p2) => p1.Index.CompareTo(p2.Index));
Save();
}
public void ResetItemClick()
{
Item BasicItem = AllItemList.Find(x => x.Name == "Pig");
MyItemList = new List<Item>() { BasicItem };
Save();
}
public void SlotClick(int slotNum)
{
Item CurItem = CurItemList[slotNum];
Item UsingItem = CurItemList.Find(x => x.isUsing == true);
if (curType == "Character")
{
if (UsingItem != null) UsingItem.isUsing = false;
CurItem.isUsing = true;
}
else
{
CurItem.isUsing = !CurItem.isUsing;
if (UsingItem != null) UsingItem.isUsing = false;
}
Save();
}
public void TabClick(string tabName)
{
// 현재 아이템 리스트에 클릭한 타입만 추가
curType = tabName;
CurItemList = MyItemList.FindAll(x => x.Type == tabName);
for (int i = 0; i < Slot.Length; i++)
{
// 슬롯과 텍스트 보이기
bool isExist = i < CurItemList.Count;
Slot[i].SetActive(isExist);
Slot[i].GetComponentInChildren<Text>().text = isExist ? CurItemList[i].Name : "";
// 아이템 이미지와 사용중인지 보이기
if (isExist)
{
ItemImage[i].sprite = ItemSprite[AllItemList.FindIndex(x => x.Name == CurItemList[i].Name)];
UsingImage[i].SetActive(CurItemList[i].isUsing);
}
}
// 탭 이미지
int tabNum = 0;
switch (tabName)
{
case "Character": tabNum = 0; break;
case "Balloon": tabNum = 1; break;
}
for (int i = 0; i < TabImage.Length; i++)
TabImage[i].sprite = i == tabNum ? TabSelectSprite : TabIdleSprite;
}
public void PointerEnter(int slotNum)
{
// 슬롯에 마우스를 올리면 0.5초후에 설명창 띄움
PointerCoroutine = PointerEnterDelay(slotNum);
StartCoroutine(PointerCoroutine);
// 설명창에 이름, 이미지, 개수, 설명 나타내기
ExplainPanel.GetComponentInChildren<Text>().text = CurItemList[slotNum].Name;
ExplainPanel.transform.GetChild(2).GetComponent<Image>().sprite = Slot[slotNum].transform.GetChild(1).GetComponent<Image>().sprite;
ExplainPanel.transform.GetChild(3).GetComponent<Text>().text = CurItemList[slotNum].Number + "개";
ExplainPanel.transform.GetChild(4).GetComponent<Text>().text = CurItemList[slotNum].Explain;
}
IEnumerator PointerEnterDelay(int slotNum)
{
yield return new WaitForSeconds(0.5f);
ExplainPanel.SetActive(true);
}
public void PointerExit(int slotNum)
{
StopCoroutine(PointerCoroutine);
ExplainPanel.SetActive(false);
}
void Save()
{
string jdata = JsonConvert.SerializeObject(MyItemList);
File.WriteAllText(Application.dataPath + "/Resources/MyItemText.txt", jdata);
TabClick(curType);
}
void Load()
{
string jdata = File.ReadAllText(Application.dataPath + "/Resources/MyItemText.txt");
MyItemList = JsonConvert.DeserializeObject<List<Item>>(jdata);
TabClick(curType);
}
}