123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using UnityEngine;
- using UnityEngine.Assertions;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace VoxelImporter
- {
- [AddComponentMenu("Voxel Importer/Voxel Chunks Object")]
- public class VoxelChunksObject : VoxelBase
- {
- public enum MaterialMode
- {
- Combine,
- Individual,
- }
- #if !UNITY_EDITOR
- void Awake()
- {
- Destroy(this);
- }
- #else
- public override bool EditorInitialize()
- {
- var result = base.EditorInitialize();
- //ver1.021 -> ver1.0.3
- if (material != null)
- {
- materials = new List<Material>();
- materials.Add(material);
- materialData = new List<MaterialData>();
- materialData.Add(new MaterialData());
- materialIndexes = new List<int>();
- materialIndexes.Add(0);
- material = null;
- result = true;
- }
- //ver1.0.4 new
- UpdateChunks();
- return result;
- }
- public VoxelChunksObjectChunk[] chunks; //ver1.0.4 new
- [SerializeField]
- protected Material material; //ver1.021 old
- public List<Material> materials; //ver1.0.3 new
- public Texture2D atlasTexture;
- public enum SplitMode
- {
- ChunkSize,
- QubicleMatrix = 100,
- WorldEditor = 100,
- }
- public SplitMode splitMode = SplitMode.QubicleMatrix;
- public IntVector3 chunkSize = new IntVector3(16, 16, 16);
- public bool createContactChunkFaces;
- public MaterialMode materialMode;
- public void UpdateChunks()
- {
- List<VoxelChunksObjectChunk> list = new List<VoxelChunksObjectChunk>();
- var all = Resources.FindObjectsOfTypeAll<VoxelChunksObjectChunk>();
- var transformCache = transform;
- for (int i = 0; i < all.Length; i++)
- {
- if (transformCache == all[i].transform.parent)
- list.Add(all[i]);
- }
- chunks = list.ToArray();
- if (updatedChunks != null)
- updatedChunks.Invoke();
- }
- public delegate void UpdatedChunks();
- public UpdatedChunks updatedChunks;
- #region Editor
- public Vector3 edit_importScale;
- public Vector3 edit_importOffset;
- public IntVector3 edit_chunkSize;
- public bool IsEditorChanged()
- {
- return edit_importScale != importScale ||
- edit_importOffset != importOffset ||
- edit_chunkSize != chunkSize;
- }
- public void RevertEditorParam()
- {
- edit_importScale = importScale;
- edit_importOffset = importOffset;
- edit_chunkSize = chunkSize;
- }
- public void ApplyEditorParam()
- {
- importScale = edit_importScale;
- importOffset = edit_importOffset;
- chunkSize = edit_chunkSize;
- }
- public override void SaveEditTmpData()
- {
- base.SaveEditTmpData();
- RevertEditorParam();
- }
- #endregion
- #region Asset
- public override bool IsUseAssetObject(UnityEngine.Object obj)
- {
- if (materialMode == VoxelChunksObject.MaterialMode.Combine)
- {
- if (materials != null)
- {
- for (int j = 0; j < materials.Count; j++)
- {
- if (materials[j] == obj) return true;
- }
- }
- if (atlasTexture == obj) return true;
- }
- if (chunks != null)
- {
- for (int i = 0; i < chunks.Length; i++)
- {
- var c = chunks[i];
- if (c == null) continue;
- if (c.mesh == obj) return true;
- if (materialMode == VoxelChunksObject.MaterialMode.Individual)
- {
- if (c.materials != null)
- {
- for (int j = 0; j < c.materials.Count; j++)
- {
- if (c.materials[j] == obj) return true;
- }
- }
- if (c.atlasTexture == obj) return true;
- }
- }
- }
- return false;
- }
- #endregion
- #region Undo
- public class RefreshCheckerChunk : RefreshChecker
- {
- public RefreshCheckerChunk(VoxelChunksObject voxelObject) : base(voxelObject)
- {
- controllerChunk = voxelObject;
- }
- public VoxelChunksObject controllerChunk;
- public VoxelChunksObject.SplitMode splitMode;
- public IntVector3 chunkSize;
- public bool createContactChunkFaces;
- public VoxelChunksObject.MaterialMode materialMode;
- public override void Save()
- {
- base.Save();
- splitMode = controllerChunk.splitMode;
- chunkSize = controllerChunk.chunkSize;
- createContactChunkFaces = controllerChunk.createContactChunkFaces;
- materialMode = controllerChunk.materialMode;
- }
- public override bool Check()
- {
- if (base.Check())
- return true;
- return splitMode != controllerChunk.splitMode ||
- chunkSize != controllerChunk.chunkSize ||
- createContactChunkFaces != controllerChunk.createContactChunkFaces ||
- materialMode != controllerChunk.materialMode;
- }
- }
- #endregion
- #endif
- }
- }
|