DisableData.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. #if UNITY_EDITOR
  7. namespace VoxelImporter
  8. {
  9. [Serializable]
  10. public class DisableData : ISerializationCallbackReceiver
  11. {
  12. public void OnBeforeSerialize()
  13. {
  14. }
  15. public void OnAfterDeserialize()
  16. {
  17. IntVector3 max = IntVector3.zero;
  18. for (int i = 0; i < disableList.Count; i++)
  19. {
  20. max = IntVector3.Max(max, disableList[i]);
  21. }
  22. indexTable = new DataTable3<VoxelBase.Face>(max.x + 1, max.y + 1, max.z + 1);
  23. for (int i = 0; i < disableList.Count; i++)
  24. {
  25. indexTable.Set(disableList[i], faceList[i]);
  26. }
  27. }
  28. public DisableData Clone()
  29. {
  30. DisableData clone = new DisableData();
  31. clone.disableList = new List<IntVector3>(disableList);
  32. clone.faceList = new List<VoxelBase.Face>(faceList);
  33. clone.OnAfterDeserialize();
  34. return clone;
  35. }
  36. public bool IsEqual(DisableData src)
  37. {
  38. if (disableList != null && src.disableList != null)
  39. {
  40. if (disableList.Count != src.disableList.Count) return false;
  41. for (int i = 0; i < disableList.Count; i++)
  42. {
  43. if (disableList[i] != src.disableList[i]) return false;
  44. }
  45. }
  46. else if (disableList != null && src.disableList == null)
  47. {
  48. return false;
  49. }
  50. else if (disableList == null && src.disableList != null)
  51. {
  52. return false;
  53. }
  54. if (faceList != null && src.faceList != null)
  55. {
  56. if (faceList.Count != src.faceList.Count) return false;
  57. for (int i = 0; i < faceList.Count; i++)
  58. {
  59. if (faceList[i] != src.faceList[i]) return false;
  60. }
  61. }
  62. else if (faceList != null && src.faceList == null)
  63. {
  64. return false;
  65. }
  66. else if (faceList == null && src.faceList != null)
  67. {
  68. return false;
  69. }
  70. return true;
  71. }
  72. public void SetDisable(IntVector3 pos, VoxelBase.Face face)
  73. {
  74. if (!indexTable.Contains(pos))
  75. {
  76. indexTable.Set(pos, face);
  77. disableList.Add(pos);
  78. faceList.Add(face);
  79. }
  80. else
  81. {
  82. indexTable.Set(pos, face);
  83. int index = disableList.IndexOf(pos);
  84. faceList[index] = face;
  85. }
  86. }
  87. public void RemoveDisable(IntVector3 pos)
  88. {
  89. if (indexTable.Contains(pos))
  90. {
  91. indexTable.Remove(pos);
  92. var index = disableList.IndexOf(pos);
  93. disableList.RemoveAt(index);
  94. faceList.RemoveAt(index);
  95. }
  96. }
  97. public VoxelBase.Face GetDisable(IntVector3 pos)
  98. {
  99. return indexTable.Get(pos);
  100. }
  101. public void ClearDisable()
  102. {
  103. indexTable.Clear();
  104. disableList.Clear();
  105. faceList.Clear();
  106. }
  107. public void AllAction(Action<IntVector3, VoxelBase.Face> action)
  108. {
  109. indexTable.AllAction((x, y, z, face) =>
  110. {
  111. action(new IntVector3(x, y, z), face);
  112. });
  113. }
  114. private DataTable3<VoxelBase.Face> indexTable = new DataTable3<VoxelBase.Face>();
  115. [SerializeField]
  116. private List<IntVector3> disableList = new List<IntVector3>();
  117. [SerializeField]
  118. private List<VoxelBase.Face> faceList = new List<VoxelBase.Face>();
  119. }
  120. }
  121. #endif