Unity/코드 예제들

배열을 Dictionary로 사용. 직열화, 인터팩터 창에 출력가능.

소나무꼴 2020. 11. 19. 16:56

대충 만들어서 미구현된 부분이 많음..

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

[Serializable]
//[SerializeField]
public class DataBase
{
    public string id;
    public int num;
}

[Serializable]
//[SerializeField]
public class ListToDIc: IDictionary<string, DataBase>,
                             ISerializationCallbackReceiver
{

    public DataBase[] dataBase = new DataBase[2] { new DataBase() { id = "A" }, new DataBase() { id = "B" } };
    IDictionary<string, DataBase> m_LevelDictionary;

    #region IDictionary
    public DataBase this[string key] { get => m_LevelDictionary[key]; set => throw new System.NotImplementedException(); }

    public ICollection<string> Keys => throw new System.NotImplementedException();

    public ICollection<DataBase> Values => throw new System.NotImplementedException();

    public int Count => dataBase.Length;

    public bool IsReadOnly => throw new System.NotImplementedException();

    public void Add(string key, DataBase value)
    {
        throw new System.NotImplementedException();
    }

    public void Add(KeyValuePair<string, DataBase> item)
    {
        throw new System.NotImplementedException();
    }

    public void Clear()
    {
        throw new System.NotImplementedException();
    }

    public bool Contains(KeyValuePair<string, DataBase> item)
    {
        return m_LevelDictionary.Contains(item);
    }

    public bool ContainsKey(string key)
    {
        return m_LevelDictionary.ContainsKey(key);
    }

    public void CopyTo(KeyValuePair<string, DataBase>[] array, int arrayIndex)
    {
        throw new System.NotImplementedException();
    }

    public IEnumerator<KeyValuePair<string, DataBase>> GetEnumerator()
    {
        throw new System.NotImplementedException();
    }

    public bool Remove(string key)
    {
        throw new System.NotImplementedException();
    }

    public bool Remove(KeyValuePair<string, DataBase> item)
    {
        throw new System.NotImplementedException();
    }

    public bool TryGetValue(string key, out DataBase value)
    {
        throw new System.NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new System.NotImplementedException();
    }
    #endregion


    public void OnAfterDeserialize()
    {
        m_LevelDictionary = dataBase.ToDictionary(l => l.id);        
    }

    public void OnBeforeSerialize()
    {
        Debug.Log(dataBase.Length);
      //  throw new System.NotImplementedException();
    }


}

public class ListToDIcExample : MonoBehaviour
{
    public ListToDIc listToDic;
    public int temp;
    // Start is called before the first frame update
    void Start()
    {
        //if (listToDic.ContainsKey("A"))
        //{
        //    Debug.Log("A");
        //}
        //if (listToDic.ContainsKey("C"))
        //{
        //    Debug.Log("A");
        //}
        //listToDic.dataBase[0].id = "A";
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
        {
            if (listToDic.ContainsKey("A"))
            {
                Debug.Log("A");
            }
            if (listToDic.ContainsKey("C"))
            {
                Debug.Log("A");
            }
            listToDic.dataBase[0].id = "A";
        }
    }
}