티스토리 뷰

  • 오전
    • 오전회의
    • 트러블슈팅 작성 및 버그찾기위한 테스트.
  • 오후
    • 스테이지 저장을 위한 데이터 임시 저장 시스템 구축
    • DoTween작업 시작.

 

 

스테이지 저장을 위한 데이터 임시 저장

프로토 타입으로 잠시 만들어뒀엇습니다.

 

 

1. 저장할 스테이지 정보 클래스 만들기

[System.Serializable]
public class StageSaveInfo
{
    public int stageIdx;
    public bool isCleared;
    public bool isEnable;

    public StageSaveInfo(StageButton stagebutton)
    {
        stageIdx = stagebutton.stageIdx;
        isCleared = stagebutton.isCleared;
        isEnable = stagebutton.isEnable;
    }
}

 

2. 로드시에 필요한 형태의 데이터 클래스 만들기

[System.Serializable]
public class SaveData
{
    public string playerName;   
    public List<StageSaveInfo> clears;  //클리어 데이터 목록
    
    
    public SaveData()
    {
        playerName = "Unknown";
        clears = new List<StageSaveInfo>();
    }

}

 

3. SaveManager만들기

public class SaveManagerPrototype : SingletonBase<SaveManagerPrototype>
{
    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(gameObject);
    }

    public void SaveClearData(StageSaveInfo stageData)
    {
        BinaryFormatter formatter = new BinaryFormatter();

        string path = Application.persistentDataPath + "/player.clear";
        FileStream stream = new FileStream(path, FileMode.Create);

        SaveData data = new SaveData();
        data.clears.Add(stageData);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public SaveData LoadPlayer()
    {
        string path = Application.persistentDataPath + "/player.clear";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            SaveData data = formatter.Deserialize(stream) as SaveData;

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }


}

 

 

위 작업은 제대로 돌아가는지 테스트는 아직 못해본 상태입니다 ㅠ

다음에 활용할 수 있으면 좋겠습니다

 

참고 : https://youtu.be/XOjd_qU2Ido?si=nscjy8eSo_01ToPX

 

 

DoTween작업

fade in fade out 작업

 

public class MySceneManager : SingletonBase<MySceneManager>
{
    public CanvasGroup fadeImg;
    float fadeDuration = 0.5f; //암전되는 시간.

    public GameObject Loading;
    public TextMeshProUGUI loadingTxt;


    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(gameObject);
        SceneManager.sceneLoaded += OnSceneLoaded; // 이벤트에 추가
    }

    public void ChangeScene(string sceneName)
    {
        fadeImg.DOFade(1, fadeDuration)
            .OnStart(() =>
            {
                fadeImg.blocksRaycasts = true; //아래 레이캐스트 막기
            })
            .OnComplete(() =>
            {
                StartCoroutine("LoadScene", sceneName);
            });
    }

    IEnumerator LoadScene(string sceneName)
    {
        Loading.SetActive(true); //로딩 화면을 띄움

        AsyncOperation async = SceneManager.LoadSceneAsync(sceneName);
        async.allowSceneActivation = false; //퍼센트 딜레이용

        float past_time = 0;
        float percentage = 0;

        while (!(async.isDone))
        {
            yield return null;

            past_time += Time.deltaTime;

            if (percentage >= 90)
            {
                percentage = Mathf.Lerp(percentage, 100, past_time);

                if (percentage == 100)
                {
                    async.allowSceneActivation = true; //씬 전환 준비 완료
                }
            }
            else
            {
                percentage = Mathf.Lerp(percentage, async.progress * 100f, past_time);
                if (percentage >= 90) past_time = 0;
            }
            loadingTxt.text = percentage.ToString("0") + "%"; //로딩 퍼센트 표기
        }
    }
    private void OnDestroy()
    {
        SceneManager.sceneLoaded -= OnSceneLoaded; // 이벤트에서 제거*
    }

    private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        fadeImg.DOFade(0, fadeDuration)
        .OnStart(() => {
            Loading.SetActive(false);
        })
        .OnComplete(() => {
            fadeImg.blocksRaycasts = false;
        });
    }
}

 

 

마이씬매니저를 싱글톤으로 만들어줬습니다

파괴되지않게 하여 씬 로드를 할떄마다 다음과 같은 fade in fadeout효과가 있는 로딩씬을 호출할 수 있습니다.

 

public void OnButtonClicked()
{
    MySceneManager.Instance.ChangeScene("LobbyScene");
    //SceneManager.LoadScene("LobbyScene");
}

 

씬이 바뀔때 씬매니저 로드씬 대신

마이씬매니저의 체인지씬을 써주면 로딩화면을 사용할 수 있습니다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
글 보관함