개발일지

[5.12] 프로젝트 RND - 다이얼로그

ljw4104 2021. 5. 12. 12:59

Dialog Output

결과

- 다이얼로그는 2초당 한번 자동으로 출력된다.

- 화면을 클릭하면 다음 스크립트를 바로 출력한다.

- 위의 두 개의 작업은 각각 독립된 수행작업이므로, 클릭과 다이얼로그는 다른 시간에 동작한다.

 

 

Dialog Save & Load

Save한 부분 / Loading한 부분

 

- Save를 하면 더 이상 Dialog가 나오지 않는다.

- Save의 기준은 각 챕터의 몇번째 Dialog인지를 체크한다

- Load를 하면 처음 시작부터 저장한 부분까지 모든 Dialog가 출력이 된다.

 

 

DataManagerRND.cs

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class DataManagerRND : MonoBehaviour
{

    public Button save;         //저장 버튼
    public Button load;         //로드 버튼
    public Button pass;         //스크립트 넘기는 버튼(이미지 안에 내장)

    private string[] dialogData;
    private int keyIndex;
    private bool clicked;
    private bool loaded;
    private Coroutine dialogCoroutine;
    private Coroutine saveCoroutine;

    void Awake()
    {
        DataManager.Instance.GetData();
        this.dialogData = new string[DataManager.Instance.dicDialog.Count];
        for (int i = 0; i < this.dialogData.Length; i++)
        {
            this.dialogData[i] = DataManager.Instance.dicDialog[i + 1].dialog;
        }
        this.keyIndex = 0;
    }
    // Start is called before the first frame update
    void Start()
    {
        this.dialogCoroutine = StartCoroutine(this.PassDialog());
        this.saveCoroutine = StartCoroutine(this.ClickDialog());
        this.save.onClick.AddListener(() =>
        {
            DataManager.Instance.SaveData(this.keyIndex);
            StopCoroutine(this.dialogCoroutine);
            StopCoroutine(this.saveCoroutine);
        });

        this.pass.onClick.AddListener(() =>
        {
            this.clicked = true;
        });

        this.load.onClick.AddListener(() =>
        {
            this.loaded = true;
            this.keyIndex = DataManager.Instance.LoadData();
        });
    }

    private IEnumerator PassDialog()
    {
        while (true)
        {
            if (this.keyIndex >= this.dialogData.Length) break;
            yield return new WaitForSeconds(2f);
            Debug.Log(this.dialogData[this.keyIndex++]);
        }
    }

    private IEnumerator ClickDialog()
    {
        while (true)
        {
            if (this.keyIndex >= this.dialogData.Length) break;

            if (this.clicked || this.loaded)
            {
                if (this.loaded)
                {
                    //로드된 데이터가 있으면 로드된 부분까지 쭉 로딩함.
                    for (int i = 0; i < this.keyIndex; i++)
                    {
                        Debug.Log(this.dialogData[i]);
                    }
                    this.loaded = false;
                }
                else
                {
                    Debug.Log(this.dialogData[this.keyIndex++]);
                }
                this.clicked = false;
            }
            yield return null;
        }
    }
}

 

DataManager.cs

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

public class DataManager
{
    public Dictionary<int, DialogData> dicDialog;
    private static DataManager instance;
    public static DataManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new DataManager();
            }
            return instance;
        }
    }
    private UserInfo user;

    private DataManager() { }

    public void GetData()       //초기 데이터 로딩
    {
        var ta = Resources.Load<TextAsset>("DialogData");
        var json = ta.text;
        this.dicDialog = JsonConvert.DeserializeObject<DialogData[]>(json).ToDictionary(x => x.id);
    }

    public void SaveData(int keyIndex)      //저장하기
    {
        var path = string.Format("{0}/user_info.json", Application.persistentDataPath);
        UserInfo newUser = new UserInfo();
        newUser.chapter = 0;
        newUser.keyIndex = keyIndex;
        this.user = newUser;

        var json = JsonConvert.SerializeObject(this.user);
        File.WriteAllText(path, json);
    }

    public int LoadData()      //불러오기
    {
        var path = string.Format("{0}/user_info.json", Application.persistentDataPath);
        var json = File.ReadAllText(path);
        this.user = JsonConvert.DeserializeObject<UserInfo>(json);

        return user.keyIndex;
    }
}