ObjectSerializer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Core
  5. {
  6. // Todo: Refactor components to use lists instead of a dictionary
  7. // Todo: Refactor ScriptableObjects to use lists instead of a dictionary
  8. [System.Serializable]
  9. public class ObjectSerializer
  10. {
  11. //[HideInInspector] public Dictionary<string, Component> components = new Dictionary<string, Component>();
  12. //[HideInInspector] public Dictionary<string, ScriptableObject> scriptableObjects = new Dictionary<string, ScriptableObject>();
  13. public void Clear()
  14. {
  15. objectKeyList.Clear();
  16. objectList.Clear();
  17. gameObjectKeyList.Clear();
  18. gameObjectList.Clear();
  19. //components.Clear();
  20. //scriptableObjects.Clear();
  21. }
  22. #region Objects
  23. [SerializeField] private List<string> objectKeyList = new List<string>();
  24. [SerializeField] private List<object> objectList = new List<object>();
  25. private int GetObjectIndex(string key)
  26. {
  27. for (int i = 0; i < objectKeyList.Count; i++)
  28. {
  29. if(objectKeyList[i] == key) return i;
  30. }
  31. return -1;
  32. }
  33. public void AddObject(string key, object obj)
  34. {
  35. int index = GetObjectIndex(key);
  36. if(index == -1)
  37. {
  38. objectKeyList.Add(key);
  39. objectList.Add(obj);
  40. }
  41. else objectList[index] = obj;
  42. }
  43. public object GetObject(string key)
  44. {
  45. int index = GetObjectIndex(key);
  46. if(index == -1) return null;
  47. else if(index >= objectList.Count) return null;
  48. else return objectList[index];
  49. }
  50. #endregion
  51. #region gameObjects
  52. [SerializeField] private List<string> gameObjectKeyList = new List<string>();
  53. [SerializeField] private List<GameObject> gameObjectList = new List<GameObject>();
  54. private int GetGameObjectIndex(string key)
  55. {
  56. for (int i = 0; i < gameObjectKeyList.Count; i++)
  57. {
  58. if(gameObjectKeyList[i] == key) return i;
  59. }
  60. return -1;
  61. }
  62. public void AddGameObject(string key, GameObject obj)
  63. {
  64. int index = GetGameObjectIndex(key);
  65. if(index == -1)
  66. {
  67. gameObjectKeyList.Add(key);
  68. gameObjectList.Add(obj);
  69. }
  70. else gameObjectList[index] = obj;
  71. }
  72. public GameObject GetGameObject(string key)
  73. {
  74. int index = GetGameObjectIndex(key);
  75. if(index == -1) return null;
  76. else if(index >= gameObjectList.Count) return null;
  77. else return gameObjectList[index];
  78. }
  79. public int GameObjectCount() => gameObjectList.Count;
  80. #endregion
  81. }
  82. }