/// Spawns all prefabs for a given tilemap /// Expects populated chunks to have valid GameObjects public static void SpawnPrefabs(tk2dTileMap tileMap, bool forceBuild) { // Restart these lists that will be stored in the tileMap tilePrefabsList TilePrefabsX = new List <int>(); TilePrefabsY = new List <int>(); TilePrefabsLayer = new List <int>(); TilePrefabsInstance = new List <GameObject>(); int[] prefabCounts = new int[tileMap.data.tilePrefabs.Length]; int numLayers = tileMap.Layers.Length; for (int layerId = 0; layerId < numLayers; ++layerId) { var layer = tileMap.Layers[layerId]; var layerData = tileMap.data.Layers[layerId]; // We skip offsetting the first one if (layer.IsEmpty || layerData.skipMeshGeneration) { continue; } for (int cellY = 0; cellY < layer.numRows; ++cellY) { int baseY = cellY * layer.divY; for (int cellX = 0; cellX < layer.numColumns; ++cellX) { int baseX = cellX * layer.divX; var chunk = layer.GetChunk(cellX, cellY); if (chunk.IsEmpty) { continue; } if (!forceBuild && !chunk.Dirty) { continue; } SpawnPrefabsForChunk(tileMap, chunk, baseX, baseY, layerId, prefabCounts); } } } tileMap.SetTilePrefabsList(TilePrefabsX, TilePrefabsY, TilePrefabsLayer, TilePrefabsInstance); }
/// <summary> /// Moves the chunk's gameobject's children to the prefab root /// </summary> public static void HideTileMapPrefabs(tk2dTileMap tileMap) { if (tileMap.renderData == null || tileMap.Layers == null) { // No Render Data to parent Prefab Root to return; } if (tileMap.PrefabsRoot == null) { var go = tileMap.PrefabsRoot = tk2dUtil.CreateGameObject("Prefabs"); go.transform.parent = tileMap.renderData.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; } int instListCount = tileMap.GetTilePrefabsListCount(); bool[] instExists = new bool[instListCount]; for (int i = 0; i < tileMap.Layers.Length; ++i) { var layer = tileMap.Layers[i]; for (int j = 0; j < layer.spriteChannel.chunks.Length; ++j) { var chunk = layer.spriteChannel.chunks[j]; if (chunk.gameObject == null) continue; var t = chunk.gameObject.transform; int childCount = t.childCount; for (int k = 0; k < childCount; ++k) { GameObject go = t.GetChild(k).gameObject; for (int q = 0; q < instListCount; ++q) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(q, out x, out y, out layerIdx, out instance); if (instance == go) { instExists[q] = true; break; } } } } } Object[] prefabs = tileMap.data.tilePrefabs; List<int> tileX = new List<int>(); List<int> tileY = new List<int>(); List<int> tileLayer = new List<int>(); List<GameObject> tileInst = new List<GameObject>(); for (int i = 0; i < instListCount; ++i) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(i, out x, out y, out layerIdx, out instance); // Is it already IN the list for some reason? if (!instExists[i]) { int tileId = (x >= 0 && x < tileMap.width && y >= 0 && y < tileMap.height) ? tileMap.GetTile(x, y, layerIdx) : -1; if (tileId >= 0 && tileId < prefabs.Length && prefabs[tileId] != null) { instExists[i] = true; } } if (instExists[i]) { tileX.Add(x); tileY.Add(y); tileLayer.Add(layerIdx); tileInst.Add(instance); tk2dUtil.SetTransformParent(instance.transform, tileMap.PrefabsRoot.transform); } } tileMap.SetTilePrefabsList(tileX, tileY, tileLayer, tileInst); }
/// Spawns all prefabs for a given tilemap /// Expects populated chunks to have valid GameObjects public static void SpawnPrefabs(tk2dTileMap tileMap, bool forceBuild) { // Restart these lists that will be stored in the tileMap tilePrefabsList TilePrefabsX = new List<int>(); TilePrefabsY = new List<int>(); TilePrefabsLayer = new List<int>(); TilePrefabsInstance = new List<GameObject>(); int[] prefabCounts = new int[tileMap.data.tilePrefabs.Length]; int numLayers = tileMap.Layers.Length; for (int layerId = 0; layerId < numLayers; ++layerId) { var layer = tileMap.Layers[layerId]; var layerData = tileMap.data.Layers[layerId]; // We skip offsetting the first one if (layer.IsEmpty || layerData.skipMeshGeneration) continue; for (int cellY = 0; cellY < layer.numRows; ++cellY) { int baseY = cellY * layer.divY; for (int cellX = 0; cellX < layer.numColumns; ++cellX) { int baseX = cellX * layer.divX; var chunk = layer.GetChunk(cellX, cellY); if (chunk.IsEmpty) continue; if (!forceBuild && !chunk.Dirty) continue; SpawnPrefabsForChunk(tileMap, chunk, baseX, baseY, layerId, prefabCounts); } } } tileMap.SetTilePrefabsList(TilePrefabsX, TilePrefabsY, TilePrefabsLayer, TilePrefabsInstance); }
/// <summary> /// Moves the chunk's gameobject's children to the prefab root /// </summary> public static void HideTileMapPrefabs(tk2dTileMap tileMap) { if (tileMap.renderData == null) { // No Render Data to parent Prefab Root to return; } else { if (tileMap.PrefabsRoot == null) { var go = tileMap.PrefabsRoot = new GameObject("Prefabs"); go.transform.parent = tileMap.renderData.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; } } if (tileMap.Layers == null) { return; } int instListCount = tileMap.GetTilePrefabsListCount(); bool[] instExists = new bool[instListCount]; for (int i = 0; i < tileMap.Layers.Length; ++i) { var layer = tileMap.Layers[i]; for (int j = 0; j < layer.spriteChannel.chunks.Length; ++j) { var chunk = layer.spriteChannel.chunks[j]; if (chunk.gameObject == null) { continue; } var t = chunk.gameObject.transform; int childCount = t.childCount; for (int k = 0; k < childCount; ++k) { GameObject go = t.GetChild(k).gameObject; for (int q = 0; q < instListCount; ++q) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(q, out x, out y, out layerIdx, out instance); if (instance == go) { instExists[q] = true; break; } } } } } List <int> tileX = new List <int>(); List <int> tileY = new List <int>(); List <int> tileLayer = new List <int>(); List <GameObject> tileInst = new List <GameObject>(); for (int i = 0; i < instListCount; ++i) { if (instExists[i]) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(i, out x, out y, out layerIdx, out instance); tileX.Add(x); tileY.Add(y); tileLayer.Add(layerIdx); tileInst.Add(instance); instance.transform.parent = tileMap.PrefabsRoot.transform; } } tileMap.SetTilePrefabsList(tileX, tileY, tileLayer, tileInst); }
/// <summary> /// Moves the chunk's gameobject's children to the prefab root /// </summary> public static void HideTileMapPrefabs(tk2dTileMap tileMap) { if (tileMap.renderData == null || tileMap.Layers == null) { // No Render Data to parent Prefab Root to return; } if (tileMap.PrefabsRoot == null) { var go = tileMap.PrefabsRoot = tk2dUtil.CreateGameObject("Prefabs"); go.transform.parent = tileMap.renderData.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; } int instListCount = tileMap.GetTilePrefabsListCount(); bool[] instExists = new bool[instListCount]; for (int i = 0; i < tileMap.Layers.Length; ++i) { var layer = tileMap.Layers[i]; for (int j = 0; j < layer.spriteChannel.chunks.Length; ++j) { var chunk = layer.spriteChannel.chunks[j]; if (chunk.gameObject == null) { continue; } var t = chunk.gameObject.transform; int childCount = t.childCount; for (int k = 0; k < childCount; ++k) { GameObject go = t.GetChild(k).gameObject; for (int q = 0; q < instListCount; ++q) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(q, out x, out y, out layerIdx, out instance); if (instance == go) { instExists[q] = true; break; } } } } } Object[] prefabs = tileMap.data.tilePrefabs; List <int> tileX = new List <int>(); List <int> tileY = new List <int>(); List <int> tileLayer = new List <int>(); List <GameObject> tileInst = new List <GameObject>(); for (int i = 0; i < instListCount; ++i) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(i, out x, out y, out layerIdx, out instance); // Is it already IN the list for some reason? if (!instExists[i]) { int tileId = (x >= 0 && x < tileMap.width && y >= 0 && y < tileMap.height) ? tileMap.GetTile(x, y, layerIdx) : -1; if (tileId >= 0 && tileId < prefabs.Length && prefabs[tileId] != null) { instExists[i] = true; } } if (instExists[i]) { tileX.Add(x); tileY.Add(y); tileLayer.Add(layerIdx); tileInst.Add(instance); tk2dUtil.SetTransformParent(instance.transform, tileMap.PrefabsRoot.transform); } } tileMap.SetTilePrefabsList(tileX, tileY, tileLayer, tileInst); }
public static void SpawnPrefabs(tk2dTileMap tileMap, bool forceBuild) { TilePrefabsX = new List<int>(); TilePrefabsY = new List<int>(); TilePrefabsLayer = new List<int>(); TilePrefabsInstance = new List<GameObject>(); int[] prefabCounts = new int[tileMap.data.tilePrefabs.Length]; int length = tileMap.Layers.Length; for (int i = 0; i < length; i++) { Layer layer = tileMap.Layers[i]; LayerInfo info = tileMap.data.Layers[i]; if (!layer.IsEmpty && !info.skipMeshGeneration) { for (int j = 0; j < layer.numRows; j++) { int baseY = j * layer.divY; for (int k = 0; k < layer.numColumns; k++) { int baseX = k * layer.divX; SpriteChunk chunk = layer.GetChunk(k, j); if (!chunk.IsEmpty && (forceBuild || chunk.Dirty)) { SpawnPrefabsForChunk(tileMap, chunk, baseX, baseY, i, prefabCounts); } } } } } tileMap.SetTilePrefabsList(TilePrefabsX, TilePrefabsY, TilePrefabsLayer, TilePrefabsInstance); }
public static void HideTileMapPrefabs(tk2dTileMap tileMap) { if ((tileMap.renderData != null) && (tileMap.Layers != null)) { if (tileMap.PrefabsRoot == null) { GameObject obj6 = tk2dUtil.CreateGameObject("Prefabs"); tileMap.PrefabsRoot = obj6; GameObject obj2 = obj6; obj2.transform.parent = tileMap.renderData.transform; obj2.transform.localPosition = Vector3.zero; obj2.transform.localRotation = Quaternion.identity; obj2.transform.localScale = Vector3.one; } int tilePrefabsListCount = tileMap.GetTilePrefabsListCount(); bool[] flagArray = new bool[tilePrefabsListCount]; for (int i = 0; i < tileMap.Layers.Length; i++) { Layer layer = tileMap.Layers[i]; for (int k = 0; k < layer.spriteChannel.chunks.Length; k++) { SpriteChunk chunk = layer.spriteChannel.chunks[k]; if (chunk.gameObject != null) { Transform transform = chunk.gameObject.transform; int childCount = transform.childCount; for (int m = 0; m < childCount; m++) { GameObject gameObject = transform.GetChild(m).gameObject; for (int n = 0; n < tilePrefabsListCount; n++) { int num7; int num8; int num9; GameObject obj4; tileMap.GetTilePrefabsListItem(n, out num7, out num8, out num9, out obj4); if (obj4 == gameObject) { flagArray[n] = true; break; } } } } } } UnityEngine.Object[] tilePrefabs = tileMap.data.tilePrefabs; List<int> xs = new List<int>(); List<int> ys = new List<int>(); List<int> layers = new List<int>(); List<GameObject> instances = new List<GameObject>(); for (int j = 0; j < tilePrefabsListCount; j++) { int num11; int num12; int num13; GameObject obj5; tileMap.GetTilePrefabsListItem(j, out num11, out num12, out num13, out obj5); if (!flagArray[j]) { int index = (((num11 < 0) || (num11 >= tileMap.width)) || ((num12 < 0) || (num12 >= tileMap.height))) ? -1 : tileMap.GetTile(num11, num12, num13); if (((index >= 0) && (index < tilePrefabs.Length)) && (tilePrefabs[index] != null)) { flagArray[j] = true; } } if (flagArray[j]) { xs.Add(num11); ys.Add(num12); layers.Add(num13); instances.Add(obj5); tk2dUtil.SetTransformParent(obj5.transform, tileMap.PrefabsRoot.transform); } } tileMap.SetTilePrefabsList(xs, ys, layers, instances); } }
/// <summary> /// Moves the chunk's gameobject's children to the prefab root /// </summary> public static void HideTileMapPrefabs(tk2dTileMap tileMap) { if (tileMap.renderData == null) { // No Render Data to parent Prefab Root to return; } else { if (tileMap.PrefabsRoot == null) { var go = tileMap.PrefabsRoot = new GameObject("Prefabs"); go.transform.parent = tileMap.renderData.transform; go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; } } if (tileMap.Layers == null) return; int instListCount = tileMap.GetTilePrefabsListCount(); bool[] instExists = new bool[instListCount]; for (int i = 0; i < tileMap.Layers.Length; ++i) { var layer = tileMap.Layers[i]; for (int j = 0; j < layer.spriteChannel.chunks.Length; ++j) { var chunk = layer.spriteChannel.chunks[j]; if (chunk.gameObject == null) continue; var t = chunk.gameObject.transform; int childCount = t.childCount; for (int k = 0; k < childCount; ++k) { GameObject go = t.GetChild(k).gameObject; for (int q = 0; q < instListCount; ++q) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(q, out x, out y, out layerIdx, out instance); if (instance == go) { instExists[q] = true; break; } } } } } List<int> tileX = new List<int>(); List<int> tileY = new List<int>(); List<int> tileLayer = new List<int>(); List<GameObject> tileInst = new List<GameObject>(); for (int i = 0; i < instListCount; ++i) { if (instExists[i]) { int x, y, layerIdx; GameObject instance; tileMap.GetTilePrefabsListItem(i, out x, out y, out layerIdx, out instance); tileX.Add(x); tileY.Add(y); tileLayer.Add(layerIdx); tileInst.Add(instance); instance.transform.parent = tileMap.PrefabsRoot.transform; } } tileMap.SetTilePrefabsList(tileX, tileY, tileLayer, tileInst); }