VoxelChunksObjectExplosion.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace VoxelImporter
  6. {
  7. [AddComponentMenu("Voxel Importer/Extra/Explosion/Voxel Chunks Object Explosion")]
  8. [ExecuteInEditMode, RequireComponent(typeof(VoxelChunksObject))]
  9. public class VoxelChunksObjectExplosion : VoxelBaseExplosion
  10. {
  11. #if UNITY_EDITOR
  12. protected VoxelChunksObject voxelObject { get; private set; }
  13. #endif
  14. public VoxelChunksObjectChunkExplosion[] chunksExplosion;
  15. public List<Material> materials;
  16. public VoxelChunksObject.MaterialMode materialMode;
  17. protected override void Awake()
  18. {
  19. base.Awake();
  20. #if UNITY_EDITOR
  21. voxelObject = GetComponent<VoxelChunksObject>();
  22. if (voxelObject != null)
  23. {
  24. UpdatedChunks();
  25. voxelObject.updatedChunks += UpdatedChunks;
  26. }
  27. #endif
  28. }
  29. protected override void OnDestroy()
  30. {
  31. base.OnDestroy();
  32. #if UNITY_EDITOR
  33. if (voxelObject != null)
  34. {
  35. voxelObject.updatedChunks -= UpdatedChunks;
  36. }
  37. #endif
  38. }
  39. #if UNITY_EDITOR
  40. private void UpdatedChunks()
  41. {
  42. if (voxelObject == null || voxelObject.chunks == null) return;
  43. chunksExplosion = new VoxelChunksObjectChunkExplosion[voxelObject.chunks.Length];
  44. for (int i = 0; i < voxelObject.chunks.Length; i++)
  45. {
  46. if (voxelObject.chunks[i] == null) continue;
  47. chunksExplosion[i] = voxelObject.chunks[i].GetComponent<VoxelChunksObjectChunkExplosion>();
  48. }
  49. }
  50. #endif
  51. public override void DrawMesh()
  52. {
  53. if (chunksExplosion != null)
  54. {
  55. for (int i = 0; i < chunksExplosion.Length; i++)
  56. {
  57. if (chunksExplosion[i] == null) continue;
  58. chunksExplosion[i].DrawMesh();
  59. }
  60. }
  61. }
  62. public override void SetEnableExplosionObject(bool enable)
  63. {
  64. enabled = enable;
  65. if(chunksExplosion != null)
  66. {
  67. for (int i = 0; i < chunksExplosion.Length; i++)
  68. {
  69. if (chunksExplosion[i] == null) continue;
  70. chunksExplosion[i].enabled = enable;
  71. }
  72. }
  73. }
  74. public override void SetEnableRenderer(bool enable)
  75. {
  76. if (chunksExplosion != null)
  77. {
  78. for (int i = 0; i < chunksExplosion.Length; i++)
  79. {
  80. if (chunksExplosion[i] == null) continue;
  81. chunksExplosion[i].SetEnableRenderer(enable);
  82. }
  83. }
  84. }
  85. #if UNITY_EDITOR
  86. #region Asset
  87. public override bool IsUseAssetObject(UnityEngine.Object obj)
  88. {
  89. if (materials != null)
  90. {
  91. for (int i = 0; i < materials.Count; i++)
  92. {
  93. if (materials[i] == obj) return true;
  94. }
  95. }
  96. if(chunksExplosion != null)
  97. {
  98. for (int i = 0; i < chunksExplosion.Length; i++)
  99. {
  100. if (chunksExplosion[i] == null || chunksExplosion[i].meshes == null) continue;
  101. for (int j = 0; j < chunksExplosion[i].meshes.Count; j++)
  102. {
  103. if (chunksExplosion[i].meshes[j] == null) continue;
  104. if (chunksExplosion[i].meshes[j].mesh == obj) return true;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. #endregion
  111. #endif
  112. }
  113. }