ObjectSerializer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Core
  5. {
  6. [System.Serializable]
  7. public class ObjectSerializer
  8. {
  9. public void Clear()
  10. {
  11. objectKeyList.Clear();
  12. objectList.Clear();
  13. gameObjectKeyList.Clear();
  14. gameObjectList.Clear();
  15. scriptableObjectKeyList.Clear();
  16. scriptableObjecttList.Clear();
  17. unityObjectKeyList.Clear();
  18. unityObjectList.Clear();
  19. }
  20. #region Objects
  21. [SerializeField] private List<string> objectKeyList = new List<string>();
  22. [SerializeField] private List<object> objectList = new List<object>();
  23. private int GetObjectIndex(string key)
  24. {
  25. for (int i = 0; i < objectKeyList.Count; i++)
  26. {
  27. if(objectKeyList[i] == key) return i;
  28. }
  29. return -1;
  30. }
  31. public void AddObject(string key, object obj)
  32. {
  33. int index = GetObjectIndex(key);
  34. if(index == -1)
  35. {
  36. objectKeyList.Add(key);
  37. objectList.Add(obj);
  38. }
  39. else objectList[index] = obj;
  40. }
  41. public object GetObject(string key)
  42. {
  43. int index = GetObjectIndex(key);
  44. if(index == -1) return null;
  45. else if(index >= objectList.Count) return null;
  46. else return objectList[index];
  47. }
  48. #endregion
  49. #region gameObjects
  50. [SerializeField] private List<string> gameObjectKeyList = new List<string>();
  51. [SerializeField] private List<GameObject> gameObjectList = new List<GameObject>();
  52. private int GetGameObjectIndex(string key)
  53. {
  54. for (int i = 0; i < gameObjectKeyList.Count; i++)
  55. {
  56. if(gameObjectKeyList[i] == key) return i;
  57. }
  58. return -1;
  59. }
  60. public void AddGameObject(string key, GameObject obj)
  61. {
  62. int index = GetGameObjectIndex(key);
  63. if(index == -1)
  64. {
  65. gameObjectKeyList.Add(key);
  66. gameObjectList.Add(obj);
  67. }
  68. else gameObjectList[index] = obj;
  69. }
  70. public GameObject GetGameObject(string key)
  71. {
  72. int index = GetGameObjectIndex(key);
  73. if(index == -1) return null;
  74. else if(index >= gameObjectList.Count) return null;
  75. else return gameObjectList[index];
  76. }
  77. public int GameObjectCount() => gameObjectList.Count;
  78. #endregion
  79. #region scriptablebjects
  80. [SerializeField] private List<string> scriptableObjectKeyList = new List<string>();
  81. [SerializeField] private List<ScriptableObject> scriptableObjecttList = new List<ScriptableObject>();
  82. private int GetScriptableObjectIndex(string key)
  83. {
  84. for (int i = 0; i < scriptableObjectKeyList.Count; i++)
  85. {
  86. if(scriptableObjectKeyList[i] == key) return i;
  87. }
  88. return -1;
  89. }
  90. public void AddScriptableObject(string key, ScriptableObject obj)
  91. {
  92. int index = GetScriptableObjectIndex(key);
  93. if(index == -1)
  94. {
  95. scriptableObjectKeyList.Add(key);
  96. scriptableObjecttList.Add(obj);
  97. }
  98. else scriptableObjecttList[index] = obj;
  99. }
  100. public ScriptableObject GetScriptableObject(string key)
  101. {
  102. int index = GetScriptableObjectIndex(key);
  103. if(index == -1) return null;
  104. else if(index >= scriptableObjecttList.Count) return null;
  105. else return scriptableObjecttList[index];
  106. }
  107. public int ScriptableObjectCount() => scriptableObjecttList.Count;
  108. #endregion
  109. #region UnityObjects
  110. [SerializeField] private List<string> unityObjectKeyList = new List<string>();
  111. [SerializeField] private List<UnityEngine.Object> unityObjectList = new List<UnityEngine.Object>();
  112. private int GetUnityObjectIndex(string key)
  113. {
  114. for (int i = 0; i < unityObjectKeyList.Count; i++)
  115. {
  116. if(unityObjectKeyList[i] == key) return i;
  117. }
  118. return -1;
  119. }
  120. public void AddUnityObject(string key, UnityEngine.Object obj)
  121. {
  122. int index = GetUnityObjectIndex(key);
  123. if(index == -1)
  124. {
  125. unityObjectKeyList.Add(key);
  126. unityObjectList.Add(obj);
  127. }
  128. else unityObjectList[index] = obj;
  129. }
  130. public object GetUnityObject(string key)
  131. {
  132. int index = GetUnityObjectIndex(key);
  133. if(index == -1) return null;
  134. else if(index >= unityObjectList.Count) return null;
  135. else return unityObjectList[index];
  136. }
  137. #endregion
  138. }
  139. }