한 것

2개 이하 클릭시 해제 하고 다른 스킬 선택가능

2개가 초과될 시 다른 스킬 버튼 interactable = fasle;

shuffle시 클릭 된 것들 초기화

 

+ 클릭 된 것들의 데이터를 매핑할 인포 클래스를 만들어 저장하여

지정된 경로에 저장이 된다.

 

해야 할 것

인포에 들어간 데이터를 위에 만들어논 슬롯에 들어가게 하며

슬롯에는 이미지

클릭 or 호버 시에는 각 스킬에 대한 정보가 나와야 한다.

 

 

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class SkillManager : MonoBehaviour
{
    public static SkillManager instance; // 싱글톤

    public SpriteAtlas skillsAtlas;
    public Button levelUpBtn;
    public Button closeBtn;
    public GameObject selectSkillPopUp;
    public SkillData[] skillDatas;
    private List<int> rememberRandomIndex = new List<int>();

    private void Awake() // 싱글톤
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    public void Start()
    {
        levelUpBtn.onClick.RemoveAllListeners();
        OnClickLevelUpBtn();
        OnClickCloseBtn();
        TextAsset asset = Resources.Load<TextAsset>("data/skills_data");
        string json = asset.text;

        skillDatas = JsonConvert.DeserializeObject<SkillData[]>(json);
    }

    public SkillData GetDataById(int id)
    {
        SkillData foundSkillData = null;
        foreach (SkillData skillData in skillDatas)
        {
            if (skillData.id == id)
            {
                foundSkillData = skillData;
                break;
            }
        }
        return foundSkillData;
    }

    public int GetRandomAtlasImgId(int randomIndex)
    {
        return skillDatas[randomIndex].id;
    }

    public string GetRandomAtlasName(int randomIndex)
    {
        return skillDatas[randomIndex].name;
    }

    public string GetRandomAtlasDesc(int randomIndex)
    {
        return skillDatas[randomIndex].desc;
    }

    public Sprite GetRandomAtlasImg(int randomIndex)
    {
        return skillsAtlas.GetSprite(skillDatas[randomIndex].name);
    }

    public int GetSkillsRandomNum()
    {
        int randomIndex;
        do
        {
            randomIndex = Random.Range(0, skillDatas.Length);
        } while (rememberRandomIndex.Contains(randomIndex));

        rememberRandomIndex.Add(randomIndex);

        Debug.Log("랜덤으로 선택된 스킬 인덱스: " + randomIndex);
        return randomIndex;
    }

    public void OnClickLevelUpBtn()
    {
        levelUpBtn.onClick.AddListener(() =>
        {
            selectSkillPopUp.SetActive(true);

            int randomIndex = GetSkillsRandomNum();

            GetRandomAtlasImg(randomIndex);
            GetRandomAtlasName(randomIndex);
            GetRandomAtlasDesc(randomIndex);
        });
    }

    public void OnClickCloseBtn()
    {
        closeBtn.onClick.AddListener(() =>
        {
            selectSkillPopUp.SetActive(false);
        });
    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.U2D;
using UnityEditor;
using Newtonsoft.Json;

public class SelectSkillPopUp : MonoBehaviour
{
    public GameObject[] selectSkills;
    public Image[] skillImgs;
    public TMP_Text[] skillIds;
    public TMP_Text[] skillNames;
    public TMP_Text[] skillDescs;

    public Button shuffleSkills;
    public Button selectBtn;
    public SpriteAtlas skillsAtlas;
    List<SkillData> skillList = new List<SkillData>();

    private int selectedCount = 0;
    private List<int> selectedIndexes = new List<int>();

    private List<SkillInfo> selectedSkillInfos = new List<SkillInfo>(); // 스킬 정보를 담을 리스트

    public void Start()
    {
        selectBtn.gameObject.SetActive(false);
        ShowLevelUp();

        foreach (var data in SkillManager.instance.skillDatas)
        {
            skillList.Add(data);
        }
        shuffleSkills.onClick.AddListener(ShuffleSkills);

        // Add click event to each skill object
        for (int i = 0; i < selectSkills.Length; i++)
        {
            int index = i;
            selectSkills[i].GetComponent<Button>().onClick.AddListener(() =>
            {
                ToggleSkill(index);
            });
        }

        // Select 버튼의 클릭 이벤트 추가
        selectBtn.onClick.AddListener(SaveSelectedSkillData);
    }

    public void ShowLevelUp()
    {
        selectedIndexes.Clear();

        for (int i = 0; i < skillImgs.Length; i++)
        {
            int randomIndex = SkillManager.instance.GetSkillsRandomNum();
            selectedIndexes.Add(randomIndex);

            skillImgs[i].sprite = SkillManager.instance.GetRandomAtlasImg(randomIndex);
            skillIds[i].text = SkillManager.instance.GetRandomAtlasImgId(randomIndex).ToString();
            skillNames[i].text = SkillManager.instance.GetRandomAtlasName(randomIndex);
            skillDescs[i].text = SkillManager.instance.GetRandomAtlasDesc(randomIndex);
        }
    }

    public void ShuffleSkills()
    {
        selectedCount = 0;

        for (int i = 0; i < skillImgs.Length; i++)
        {
            int randomIndex = GetUniqueRandomIndex();
            selectedIndexes[i] = randomIndex;

            skillIds[i].text = SkillManager.instance.GetRandomAtlasImgId(randomIndex).ToString();
            skillNames[i].text = SkillManager.instance.GetRandomAtlasName(randomIndex);
            skillDescs[i].text = SkillManager.instance.GetRandomAtlasDesc(randomIndex);
            skillImgs[i].sprite = SkillManager.instance.GetRandomAtlasImg(randomIndex);
            SetImageAlpha(skillImgs[i], 1f); // Reset alpha value
        }

        UpdateSelectBtn();
    }

    int GetUniqueRandomIndex()
    {
        int randomIndex;
        do
        {
            randomIndex = Random.Range(0, skillList.Count);
        } while (selectedIndexes.Contains(randomIndex));

        return randomIndex;
    }

    void SetImageAlpha(Image image, float alpha)
    {
        Color color = image.color;
        color.a = alpha;
        image.color = color;
    }

    void UpdateSelectBtn()
    {
        selectBtn.gameObject.SetActive(selectedCount == 2);
    }

    public void ToggleSkill(int index)
    {
        Image skillImage = skillImgs[index].GetComponent<Image>();
        float alpha = GetImageAlpha(skillImage);

        if (alpha == 1f)
        {
            // Select the skill
            if (selectedCount < 2)
            {
                SetImageAlpha(skillImage, 65f / 255f);
                selectedCount++;

                // 클릭된 스킬의 정보를 SkillInfo 객체에 담고 리스트에 추가
                SkillData selectedSkill = skillList[selectedIndexes[index]];
                SkillInfo skillInfo = new SkillInfo(selectedSkill.id, selectedSkill.name, selectedSkill.sprite_name, selectedSkill.desc);
                selectedSkillInfos.Add(skillInfo);
            }
        }
        else
        {
            // Deselect the skill
            SetImageAlpha(skillImage, 1f);
            selectedCount--;

            // 해당 스킬을 리스트에서 제거
            SkillData selectedSkill = skillList[selectedIndexes[index]];
            selectedSkillInfos.RemoveAll(skillInfo => skillInfo.id == selectedSkill.id);
        }

        UpdateSelectBtn();
    }

    float GetImageAlpha(Image image)
    {
        return image.color.a;
    }

    public void SaveSelectedSkillData()
    {
        // 선택된 스킬의 정보를 직렬화하여 로컬에 저장
        string json = JsonConvert.SerializeObject(selectedSkillInfos);
        // 저장할 경로를 지정하고 파일에 쓰기
        string filePath = Application.persistentDataPath + "/selected_skills.json";
        System.IO.File.WriteAllText(filePath, json);
        Debug.Log("Selected skill data saved to: " + filePath);
    }
    public List<SkillInfo> GetSelectedSkillInfos()
    {
        return selectedSkillInfos;
    }
}

 

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

public class SkillData
{
    public int id;
    public string name;
    public string sprite_name;
    public string desc;
}

 

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

public class SkillInfo
{
    public int id;
    public string name;
    public string sprite_name;
    public string desc;

    public SkillInfo(int id, string name, string sprite_name, string desc)
    {
        this.id = id;
        this.name = name;
        this.sprite_name = sprite_name;
        this.desc = desc;
    }
}

+ Recent posts