Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.Tilemaps;
- namespace BaseProject
- {
- [CustomEditor(typeof(TemplateBuilder))]
- public class TemplateBuilderEditor : Editor
- {
- private readonly Vector3Int templateCorner = Vector3Int.zero;
- public SerializedProperty _editorFolder;
- public SerializedProperty _templates;
- //public SerializedProperty _relatedTemplates;
- public SerializedProperty _templateSize;
- public SerializedProperty _templateName;
- //public SerializedProperty _level;
- public SerializedProperty _necessities;
- public SerializedProperty _borderTile;
- public SerializedProperty _edgeTile;
- public SerializedProperty _blankTile;
- public SerializedProperty _colliderPrefabs;
- public SerializedProperty _colliderContainer;
- public SerializedProperty _prefabContainer;
- //private static readonly string editorFolder = "Assets/Misc/Templates/";
- public void OnEnable()
- {
- _editorFolder = serializedObject.FindProperty("templateFolder");
- _editorFolder = serializedObject.FindProperty("templateSubfolder");
- _templates = serializedObject.FindProperty("templates");
- _templateSize = serializedObject.FindProperty("templateSize");
- _templateName = serializedObject.FindProperty("templateName");
- _colliderContainer = serializedObject.FindProperty("colliderContainer");
- _prefabContainer = serializedObject.FindProperty("prefabContainer");
- //_relatedTemplates = serializedObject.FindProperty("relatedTemplates");
- //_level = serializedObject.FindProperty("level");
- _necessities = serializedObject.FindProperty("necessities");
- _borderTile = serializedObject.FindProperty("borderTile");
- _edgeTile = serializedObject.FindProperty("markTile");
- _blankTile = serializedObject.FindProperty("blankTile");
- _colliderPrefabs = serializedObject.FindProperty("colliderPrefabs");
- }
- /*public void OnDisable()
- {
- TemplateBuilder builder = target as TemplateBuilder;
- builder.templates = builder.templates;
- builder.templateSize = builder.templateSize;
- builder.templateName = _templateName;
- builder.colliderContainer = _colliderContainer;
- builder.prefabContainer = _prefabContainer;
- }*/
- public override void OnInspectorGUI()
- {
- GUI.enabled = false;
- MonoScript script = MonoScript.FromMonoBehaviour(target as MonoBehaviour);
- script = EditorGUILayout.ObjectField(script, typeof(MonoScript), false) as MonoScript;
- GUI.enabled = true;
- //EditorGUILayout.LabelField("Templates are read left to right, top to bottom.");
- Tools.current = UnityEditor.Tool.View;
- TemplateBuilder builder = target as TemplateBuilder;
- if (builder.templates.x < 1)
- builder.templates.x = 1;
- if (builder.templates.y < 1)
- builder.templates.y = 1;
- //GUI Style for create template button
- GUIStyle createButton = new GUIStyle(GUI.skin.button);
- createButton.fontSize = 18;
- createButton.fontStyle = FontStyle.Bold;
- createButton.margin = new RectOffset(0, 0, 6, 6);
- //createButton.border = new RectOffset(8, 8, 8, 8);
- //if (GUILayout.Button(new GUIContent("Create Template", "Create a new template"), GUILayout.Height(40)))
- if (GUILayout.Button(new GUIContent("Create Template", "Create a new template"), createButton))
- CreateTemplate(builder);
- GUILayout.BeginHorizontal();
- if (GUILayout.Button(new GUIContent("Load Template", "Load an existing template")))
- LoadTemplate(builder);
- if (GUILayout.Button(new GUIContent("Clear Template", "Clears the template for fresh use")))
- AdjustTemplate(builder, builder.templates, true);
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (GUILayout.Button(new GUIContent("Adjust Guidelines", "Adjust the guidelines on the tilemap to match the template"), GUILayout.Width(Screen.width / 2)))
- AdjustTemplate(builder, builder.templates, false);
- GUILayout.FlexibleSpace();
- GUILayout.EndHorizontal();
- builder.templateFolder = EditorGUILayout.TextField(new GUIContent("Template Folder"), builder.templateFolder);
- builder.templateSubfolder = EditorGUILayout.TextField(new GUIContent("Subfolder"), builder.templateSubfolder);
- EditorGUILayout.LabelField("- Templates start from the bottom-left.");
- builder.templateName = EditorGUILayout.TextField(new GUIContent("Template Name"), builder.templateName);
- builder.templateSize = EditorGUILayout.Vector2IntField(new GUIContent("Room Size"), builder.templateSize);
- builder.templates = EditorGUILayout.Vector2IntField(new GUIContent("Template Group"), builder.templates);
- int templates = builder.templates.x * builder.templates.y;
- if (builder.connections == null || templates != builder.connections.Length / 4)
- {
- builder.connections = new bool[templates * 4];
- for (int i = 0; i < builder.connections.Length; i++)
- builder.connections[i] = new bool();
- builder.foldoutConnections = new bool[templates];
- for (int i = 0; i < builder.foldoutConnections.Length; i++)
- builder.foldoutConnections[i] = new bool();
- }
- if (templates == 1)
- {
- builder.foldoutConnections[0] = EditorGUILayout.Foldout(builder.foldoutConnections[0], "Template Connections");
- if (builder.foldoutConnections[0])
- {
- int spacer = 40;
- GUILayout.BeginVertical();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- builder.connections[0] = EditorGUILayout.ToggleLeft("Up", builder.connections[0]);
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- builder.connections[3] = EditorGUILayout.ToggleLeft("Left", builder.connections[3], GUILayout.Width(60));
- builder.connections[1] = EditorGUILayout.ToggleLeft("Right", builder.connections[1], GUILayout.Width(60));
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- builder.connections[2] = EditorGUILayout.ToggleLeft("Down", builder.connections[2]);
- GUILayout.EndHorizontal();
- GUILayout.EndVertical();
- }
- }
- else
- {
- int spacer = 40;
- for (int i = builder.foldoutConnections.Length - 1; i >= 0; i--)
- {
- int y = i % builder.templates.y;
- int x = i / builder.templates.y;
- builder.foldoutConnections[i] = EditorGUILayout.Foldout(builder.foldoutConnections[i], "Template " + (i + 1) + " (" + x + "," + y + ") connections");
- if (builder.foldoutConnections[i])
- {
- GUILayout.BeginVertical();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- builder.connections[i * 4 + 0] = EditorGUILayout.ToggleLeft("Up", builder.connections[i * 4 + 0]);
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- builder.connections[i * 4 + 3] = EditorGUILayout.ToggleLeft("Left", builder.connections[i * 4 + 3], GUILayout.Width(60));
- builder.connections[i * 4 + 1] = EditorGUILayout.ToggleLeft("Right", builder.connections[i * 4 + 1], GUILayout.Width(60));
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- GUILayout.Space(spacer);
- builder.connections[i * 4 + 2] = EditorGUILayout.ToggleLeft("Down", builder.connections[i * 4 + 2]);
- GUILayout.EndHorizontal();
- GUILayout.EndVertical();
- }
- }
- }
- builder.necessities = EditorGUILayout.Foldout(builder.necessities, "Necessary Objects");
- if (builder.necessities)
- {
- builder.prefabContainer = EditorGUILayout.ObjectField("Prefab Container", builder.prefabContainer, typeof(Transform), true) as Transform;
- builder.colliderContainer = EditorGUILayout.ObjectField("Collider Container", builder.colliderContainer, typeof(Transform), true) as Transform;
- EditorGUILayout.PropertyField(_borderTile, new GUIContent("Border Tile"));
- EditorGUILayout.PropertyField(_edgeTile, new GUIContent("Mark Tile"));
- EditorGUILayout.PropertyField(_blankTile, new GUIContent("Blank Tile"));
- EditorGUILayout.PropertyField(_colliderPrefabs, new GUIContent("Collider Prefabs"), true);
- }
- serializedObject.ApplyModifiedProperties();
- }
- void CreateTemplate(TemplateBuilder builder)
- {
- //Verify the folders are present
- AssetHelper.VerifyFolderStructure("Assets/" + builder.templateFolder);
- //Find the maps
- Tilemap[] maps = FindObjectsOfType<Tilemap>();
- Tilemap foreground = null;
- Tilemap terrain = null;
- Tilemap background = null;
- for (int i = 0; i < maps.Length; i++)
- {
- if (maps[i].name.Equals("Terrain"))
- terrain = maps[i];
- else if (maps[i].name.Equals("Background"))
- background = maps[i];
- else if (maps[i].name.Equals("Foreground"))
- foreground = maps[i];
- }
- if (background == null || foreground == null)
- Debug.LogWarning("Foreground: " + (foreground == null ? "null" : foreground.ToString()) + ", Background: " + (background == null ? "null" : background.ToString()));
- //Build colliderData for the map
- if (builder.colliderContainer == null)
- {
- GameObject cC = GameObject.Find("Template Colliders");
- builder.colliderContainer = cC != null ? cC.transform : null;
- }
- if (builder.prefabContainer == null)
- {
- GameObject pC = GameObject.Find("Template Prefabs");
- builder.prefabContainer = pC != null ? pC.transform : null;
- }
- if (builder.colliderContainer == null || builder.prefabContainer == null)
- Debug.LogWarning("Collider Container or Prefab Container is missing");
- //Create the template(s)
- int templates = builder.templates.x * builder.templates.y;
- Vector3Int templateSize = new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1); //The method needs a Vector3Int and also needs more than 1 z for some reason
- if (templates == 1)
- {
- Template t = ScriptableObject.CreateInstance<Template>();
- t.terrain = terrain.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
- t.terrainRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.terrainRotation[x + y * templateSize.y] = terrain.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 0));
- if (background != null)
- {
- t.background = background.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
- t.backgroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.backgroundRotation[x + y * templateSize.y] = background.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
- }
- if (foreground != null)
- {
- t.foreground = foreground.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
- t.foregroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.foregroundRotation[x + y * templateSize.y] = foreground.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
- }
- // Matrix4x4 transMatrix = map.GetTransformMatrix(new Vector3Int(x + mapBounds.xMin, y + mapBounds.yMin, 0));
- t.up = builder.connections[0];
- t.right = builder.connections[1];
- t.down = builder.connections[2];
- t.left = builder.connections[3];
- //t.level = builder.level;
- t.size = new Vector2Int(builder.templateSize.x, builder.templateSize.y);
- if (builder.colliderContainer != null)
- t.colliders = BuildColliders(builder, terrain, builder.colliderContainer, templateCorner);
- if (builder.prefabContainer != null)
- t.prefabs = GatherPrefabs(builder, builder.prefabContainer);
- //Create the asset
- AssetDatabase.CreateAsset(t, "Assets/" + builder.templateFolder + builder.templateName + ".asset");
- Debug.Log("Template " + builder.templateName + " created successfully.");
- }
- else
- {
- //Ensure folder structure is present
- AssetHelper.VerifyFolderStructure("Assets/" + builder.templateFolder + builder.templateSubfolder);
- //Create master template
- TemplateMaster mt = ScriptableObject.CreateInstance<TemplateMaster>();
- mt.templateLayout = new Vector2Int(builder.templates.x, builder.templates.y);
- mt.templates = new Template[builder.templates.x * builder.templates.y];
- AssetDatabase.CreateAsset(mt, "Assets/" + builder.templateFolder + builder.templateName + ".asset");
- mt = AssetDatabase.LoadAssetAtPath<TemplateMaster>("Assets/" + builder.templateFolder + builder.templateName + ".asset"); //Reload for assignment
- //Create the templates
- Template[] ts = new Template[templates];
- for (int i = 0; i < templates; i++)
- {
- Template t = ScriptableObject.CreateInstance<Template>();
- int testX = i % builder.templates.x;
- int testY = i / builder.templates.x;
- //Debug.Log("Test X: "+testX+", Test Y: "+testY);
- int posX = testX * builder.templateSize.x + templateCorner.x;
- int posY = testY * builder.templateSize.y + templateCorner.y;
- //Debug.Log("X: "+ posX + ", Y: "+ posY);
- Vector3Int templateOrigin = new Vector3Int(posX, posY, 0);
- t.terrain = terrain.GetTilesBlock(new BoundsInt(templateOrigin, templateSize));
- t.terrainRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.terrainRotation[x + y * templateSize.y] = terrain.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 0));
- if (background != null)
- {
- t.background = background.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
- t.backgroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.backgroundRotation[x + y * templateSize.y] = background.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
- }
- if (foreground != null)
- {
- t.foreground = foreground.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
- t.foregroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- t.foregroundRotation[x + y * templateSize.y] = foreground.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
- }
- t.up = builder.connections[i * 4 + 0];
- t.right = builder.connections[i * 4 + 1];
- t.down = builder.connections[i * 4 + 2];
- t.left = builder.connections[i * 4 + 3];
- //t.level = builder.level;
- t.size = new Vector2Int(builder.templateSize.x, builder.templateSize.y);
- //t.multiTemplateSize = builder.templates;
- t.master = mt;
- if (builder.colliderContainer != null)
- t.colliders = BuildColliders(builder, terrain, builder.colliderContainer, templateOrigin);
- ts[i] = t;
- }
- //Assign the templates to the master
- for (int i = 0; i < templates; i++)
- {
- AssetDatabase.CreateAsset(ts[i], "Assets/" + builder.templateFolder + builder.templateSubfolder + builder.templateName + "." + (i + 1) + ".asset");
- ts[i].master = mt;
- mt.templates[i] = ts[i];
- }
- }
- }
- TemplateColliderData[] BuildColliders(TemplateBuilder builder, Tilemap terrain, Transform colliderContainer, Vector3Int templatePosition)
- {
- LevelAdjuster.BuildColliderData(terrain, colliderContainer.gameObject, new BoundsInt(templateCorner + templatePosition, new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1)));
- TemplateColliderData[] colliderData = new TemplateColliderData[colliderContainer.transform.childCount];
- for (int i = 0; i < colliderContainer.childCount; i++)
- {
- Transform collider = colliderContainer.transform.GetChild(i);
- colliderData[i] = new TemplateColliderData();
- colliderData[i].location = new Vector2Int((int)collider.position.x - templatePosition.x, (int)collider.position.y - templatePosition.y); //Fix location for position in template
- if (collider.name.Equals("Box Collider"))
- {
- BoxCollider2D box = collider.gameObject.GetComponent<BoxCollider2D>();
- colliderData[i].size = box.size;
- colliderData[i].offset = box.offset;
- }
- else //Look up which collider this is in a list
- {
- for (int n = 0; n < builder.colliderPrefabs.Length; n++)
- {
- if (collider.name.Equals(builder.colliderPrefabs[n].name))
- {
- colliderData[i].prefab = builder.colliderPrefabs[n];
- break;
- }
- }
- }
- }
- return colliderData;
- }
- TemplatePrefabData[] GatherPrefabs(TemplateBuilder builder, Transform prefabContainer)
- {
- TemplatePrefabData[] dataGroup = new TemplatePrefabData[prefabContainer.childCount];
- for (int i = 0; i < prefabContainer.childCount; i++)
- {
- Transform go = prefabContainer.GetChild(i);
- dataGroup[i] = new TemplatePrefabData();
- dataGroup[i].prefab = PrefabUtility.GetPrefabParent(go.gameObject) as GameObject;
- dataGroup[i].position = go.localPosition;
- dataGroup[i].rotation = go.localRotation;
- dataGroup[i].scale = go.localScale;
- if (dataGroup[i].prefab == null)
- Debug.LogError("GameObject " + go.name + " does not have a prefab.");
- }
- return dataGroup;
- }
- void LoadTemplate(TemplateBuilder builder)
- {
- //Find the maps
- Tilemap[] maps = FindObjectsOfType<Tilemap>();
- //TileBase[] blanks = new TileBase[builder.templateSize.x * builder.templateSize.y];
- Tilemap foreground = null;
- Tilemap terrain = null;
- Tilemap background = null;
- for (int i = 0; i < maps.Length; i++)
- {
- if (maps[i].name.Equals("Foreground"))
- foreground = maps[i];
- else if (maps[i].name.Equals("Terrain"))
- terrain = maps[i];
- else if (maps[i].name.Equals("Background"))
- background = maps[i];
- //maps[i].SetTilesBlock(new BoundsInt(_templateCorner, builder.templateSize), blanks);
- }
- //Load the template
- Template t = AssetDatabase.LoadAssetAtPath<Template>("Assets/" + builder.templateFolder + builder.templateName + ".asset");
- if (t != null) //Single template
- {
- Debug.Log("Loading template " + _templateName + ". Tilemaps: " + foreground + ", " + terrain + ", " + background);
- builder.templateSize = new Vector2Int(t.size.x, t.size.y);
- Vector2Int templateLayout = t.master == null ? Vector2Int.one : t.master.templateLayout;
- Vector3Int templateSize = new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1);
- AdjustTemplate(builder, templateLayout, true);
- if (foreground != null)
- {
- foreground.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.foreground);
- if (t.foregroundRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- foreground.SetTransformMatrix(new Vector3Int(x, y, 1), t.foregroundRotation[x + y * templateSize.y]);
- }
- terrain.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.terrain);
- if (t.terrainRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- terrain.SetTransformMatrix(new Vector3Int(x, y, 0), t.terrainRotation[x + y * templateSize.y]);
- if (background != null)
- {
- background.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.background);
- if (t.backgroundRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- background.SetTransformMatrix(new Vector3Int(x, y, 1), t.backgroundRotation[x + y * templateSize.y]);
- }
- builder.connections = new bool[] { t.up, t.right, t.down, t.left };
- //TODO: Load colliders and prefabs
- for (int i = 0; i < t.prefabs.Length; i++)
- {
- TemplatePrefabData data = t.prefabs[i];
- GameObject go = PrefabUtility.InstantiatePrefab(data.prefab) as GameObject;//Instantiate(data.prefab);
- go.transform.SetParent(builder.prefabContainer);
- go.name = data.prefab.name + " " + (i + 1).ToString();
- go.transform.localPosition = data.position;
- go.transform.localRotation = data.rotation;
- go.transform.localScale = data.scale;
- }
- Debug.Log("Template " + builder.templateName + " loaded successfully.");
- }
- else
- {
- TemplateMaster mt = AssetDatabase.LoadAssetAtPath<TemplateMaster>("Assets/" + builder.templateFolder + builder.templateName + ".asset");
- if (mt != null)
- {
- t = mt.templates[0];
- builder.templateSize = new Vector2Int(t.size.x, t.size.y);
- Vector2Int templateLayout = t.master == null ? Vector2Int.one : t.master.templateLayout;
- AdjustTemplate(builder, templateLayout, true);
- Vector3Int templateSize = new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1);
- builder.templates = mt.templateLayout;
- int templates = builder.templates.x * builder.templates.y;
- builder.connections = new bool[templates * 4];
- for (int i = 0; i < templates; i++)
- {
- int testX = i % builder.templates.x;
- int testY = i / builder.templates.x;
- //Debug.Log("Test X: "+testX+", Test Y: "+testY);
- int posX = testX * builder.templateSize.x + templateCorner.x;
- int posY = testY * builder.templateSize.y + templateCorner.y;
- Vector3Int templateOrigin = new Vector3Int(posX, posY, 0);
- builder.connections[i * 4 + 0] = t.up;
- builder.connections[i * 4 + 1] = t.right;
- builder.connections[i * 4 + 2] = t.down;
- builder.connections[i * 4 + 3] = t.left;
- Debug.Log("Master Template " + mt.name + " loaded successfully");
- if (foreground != null)
- {
- foreground.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.foreground);
- if (t.foregroundRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- foreground.SetTransformMatrix(new Vector3Int(x, y, 1), t.foregroundRotation[x + y * templateSize.y]);
- }
- terrain.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.terrain);
- if (t.terrainRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- terrain.SetTransformMatrix(new Vector3Int(x, y, 0), t.terrainRotation[x + y * templateSize.y]);
- if (background != null)
- {
- background.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.background);
- if (t.backgroundRotation.Length > 0)
- for (int y = 0; y < templateSize.y; y++)
- for (int x = 0; x < templateSize.x; x++)
- background.SetTransformMatrix(new Vector3Int(x, y, 1), t.backgroundRotation[x + y * templateSize.y]);
- }
- }
- }
- else
- {
- Debug.Log("Could not find any template with the name " + builder.templateName + ".");
- }
- }
- }
- //TODO: Clear previous size of border, not current
- void AdjustTemplate(TemplateBuilder builder, Vector2Int newTemplateAmount, bool clear)
- {
- //Find the maps and clear them. Also find the background map
- Tilemap[] maps = FindObjectsOfType<Tilemap>();
- TileBase[] blanks = new TileBase[builder.templateSize.x * builder.templateSize.y * builder.templates.x * builder.templates.y];
- Tilemap border = null;
- Vector3Int clearSize = new Vector3Int(builder.templateSize.x * builder.templates.x, builder.templateSize.y * builder.templates.y, 1);
- for (int i = 0; i < maps.Length; i++)
- {
- if (clear)
- maps[i].SetTilesBlock(new BoundsInt(templateCorner, clearSize), blanks);
- if (maps[i].name.Contains("Template"))
- border = maps[i];
- }
- if (clear)
- {
- if (builder.colliderContainer != null)
- for (int i = builder.colliderContainer.childCount - 1; i >= 0; i--)
- GameObject.DestroyImmediate(builder.colliderContainer.GetChild(i).gameObject);
- //Debug.Log(builder.prefabContainer + " Prefabs on child: " + builder.prefabContainer.childCount);
- if (builder.prefabContainer != null)
- for (int i = 0; i < builder.prefabContainer.childCount; i++)
- GameObject.DestroyImmediate(builder.prefabContainer.GetChild(i).gameObject);
- }
- //Reset the borders
- border.ClearAllTiles();
- builder.templates = newTemplateAmount;
- //Blank tile, useful for grabbing
- border.SetTile(new Vector3Int(templateCorner.x - 3, builder.templateSize.y * builder.templates.y - 0, 0), builder.blankTile);
- //Bottom left
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y, 0), builder.borderTile);
- //Top left
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y + builder.templateSize.y * builder.templates.y - 1, 0), builder.borderTile);
- //bottom right
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 1, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y - 0, 0), builder.borderTile);
- //Top right
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 1, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y + builder.templateSize.y * builder.templates.y - 1, 0), builder.borderTile);
- //Left and right sides
- if (builder.templateSize.y > 1)
- {
- for (int i = 1; i < builder.templates.y; i++)
- {
- //Left
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.markTile);
- //Right
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * builder.templates.x, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * builder.templates.x, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * builder.templates.x, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * builder.templates.x, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.markTile);
- }
- }
- //Top and bottom sides
- if (builder.templateSize.x > 1)
- {
- for (int i = 1; i < builder.templates.x; i++)
- {
- //Bottom
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 1, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 0, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 0, 0), builder.markTile);
- //Top
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 1 + builder.templateSize.y * builder.templates.y, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 1 + builder.templateSize.y * builder.templates.y, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 0 + builder.templateSize.y * builder.templates.y, 0), builder.borderTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 0 + builder.templateSize.y * builder.templates.y, 0), builder.borderTile);
- }
- }
- //Center parts
- if (builder.templateSize.x > 1 && builder.templateSize.y > 1)
- {
- for (int x = 1; x < builder.templates.x; x++)
- {
- for (int y = 1; y < builder.templates.y; y++)
- {
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * x, templateCorner.y - 1 + builder.templateSize.y * y, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * x, templateCorner.y - 1 + builder.templateSize.y * y, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * x, templateCorner.y - 0 + builder.templateSize.y * y, 0), builder.markTile);
- border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * x, templateCorner.y - 0 + builder.templateSize.y * y, 0), builder.markTile);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment