Guest User

Template Builder Editor

a guest
May 11th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 36.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.Tilemaps;
  7.  
  8. namespace BaseProject
  9. {
  10.     [CustomEditor(typeof(TemplateBuilder))]
  11.     public class TemplateBuilderEditor : Editor
  12.     {
  13.         private readonly Vector3Int templateCorner = Vector3Int.zero;
  14.  
  15.         public SerializedProperty _editorFolder;
  16.  
  17.         public SerializedProperty _templates;
  18.         //public SerializedProperty _relatedTemplates;
  19.         public SerializedProperty _templateSize;
  20.         public SerializedProperty _templateName;
  21.         //public SerializedProperty _level;
  22.         public SerializedProperty _necessities;
  23.  
  24.         public SerializedProperty _borderTile;
  25.         public SerializedProperty _edgeTile;
  26.         public SerializedProperty _blankTile;
  27.         public SerializedProperty _colliderPrefabs;
  28.  
  29.         public SerializedProperty _colliderContainer;
  30.         public SerializedProperty _prefabContainer;
  31.  
  32.         //private static readonly string editorFolder = "Assets/Misc/Templates/";
  33.  
  34.         public void OnEnable()
  35.         {
  36.             _editorFolder = serializedObject.FindProperty("templateFolder");
  37.             _editorFolder = serializedObject.FindProperty("templateSubfolder");
  38.  
  39.             _templates = serializedObject.FindProperty("templates");
  40.             _templateSize = serializedObject.FindProperty("templateSize");
  41.             _templateName = serializedObject.FindProperty("templateName");
  42.             _colliderContainer = serializedObject.FindProperty("colliderContainer");
  43.             _prefabContainer = serializedObject.FindProperty("prefabContainer");
  44.  
  45.             //_relatedTemplates = serializedObject.FindProperty("relatedTemplates");
  46.             //_level = serializedObject.FindProperty("level");
  47.             _necessities = serializedObject.FindProperty("necessities");
  48.             _borderTile = serializedObject.FindProperty("borderTile");
  49.             _edgeTile = serializedObject.FindProperty("markTile");
  50.             _blankTile = serializedObject.FindProperty("blankTile");
  51.             _colliderPrefabs = serializedObject.FindProperty("colliderPrefabs");
  52.         }
  53.  
  54.         /*public void OnDisable()
  55.         {
  56.             TemplateBuilder builder = target as TemplateBuilder;
  57.             builder.templates = builder.templates;
  58.             builder.templateSize = builder.templateSize;
  59.             builder.templateName = _templateName;
  60.             builder.colliderContainer = _colliderContainer;
  61.             builder.prefabContainer = _prefabContainer;
  62.         }*/
  63.         public override void OnInspectorGUI()
  64.         {
  65.             GUI.enabled = false;
  66.             MonoScript script = MonoScript.FromMonoBehaviour(target as MonoBehaviour);
  67.             script = EditorGUILayout.ObjectField(script, typeof(MonoScript), false) as MonoScript;
  68.             GUI.enabled = true;
  69.  
  70.             //EditorGUILayout.LabelField("Templates are read left to right, top to bottom.");
  71.             Tools.current = UnityEditor.Tool.View;
  72.             TemplateBuilder builder = target as TemplateBuilder;
  73.  
  74.             if (builder.templates.x < 1)
  75.                 builder.templates.x = 1;
  76.             if (builder.templates.y < 1)
  77.                 builder.templates.y = 1;
  78.  
  79.             //GUI Style for create template button
  80.             GUIStyle createButton = new GUIStyle(GUI.skin.button);
  81.             createButton.fontSize = 18;
  82.             createButton.fontStyle = FontStyle.Bold;
  83.             createButton.margin = new RectOffset(0, 0, 6, 6);
  84.             //createButton.border = new RectOffset(8, 8, 8, 8);
  85.  
  86.             //if (GUILayout.Button(new GUIContent("Create Template", "Create a new template"), GUILayout.Height(40)))
  87.             if (GUILayout.Button(new GUIContent("Create Template", "Create a new template"), createButton))
  88.                 CreateTemplate(builder);
  89.  
  90.             GUILayout.BeginHorizontal();
  91.             if (GUILayout.Button(new GUIContent("Load Template", "Load an existing template")))
  92.                 LoadTemplate(builder);
  93.             if (GUILayout.Button(new GUIContent("Clear Template", "Clears the template for fresh use")))
  94.                 AdjustTemplate(builder, builder.templates, true);
  95.             GUILayout.EndHorizontal();
  96.  
  97.             GUILayout.BeginHorizontal();
  98.             GUILayout.FlexibleSpace();
  99.             if (GUILayout.Button(new GUIContent("Adjust Guidelines", "Adjust the guidelines on the tilemap to match the template"), GUILayout.Width(Screen.width / 2)))
  100.                 AdjustTemplate(builder, builder.templates, false);
  101.             GUILayout.FlexibleSpace();
  102.             GUILayout.EndHorizontal();
  103.  
  104.             builder.templateFolder = EditorGUILayout.TextField(new GUIContent("Template Folder"), builder.templateFolder);
  105.             builder.templateSubfolder = EditorGUILayout.TextField(new GUIContent("Subfolder"), builder.templateSubfolder);
  106.  
  107.             EditorGUILayout.LabelField("- Templates start from the bottom-left.");
  108.  
  109.             builder.templateName = EditorGUILayout.TextField(new GUIContent("Template Name"), builder.templateName);
  110.             builder.templateSize = EditorGUILayout.Vector2IntField(new GUIContent("Room Size"), builder.templateSize);
  111.             builder.templates = EditorGUILayout.Vector2IntField(new GUIContent("Template Group"), builder.templates);
  112.             int templates = builder.templates.x * builder.templates.y;
  113.  
  114.             if (builder.connections == null || templates != builder.connections.Length / 4)
  115.             {
  116.                 builder.connections = new bool[templates * 4];
  117.                 for (int i = 0; i < builder.connections.Length; i++)
  118.                     builder.connections[i] = new bool();
  119.                 builder.foldoutConnections = new bool[templates];
  120.                 for (int i = 0; i < builder.foldoutConnections.Length; i++)
  121.                     builder.foldoutConnections[i] = new bool();
  122.             }
  123.  
  124.             if (templates == 1)
  125.             {
  126.                 builder.foldoutConnections[0] = EditorGUILayout.Foldout(builder.foldoutConnections[0], "Template Connections");
  127.                 if (builder.foldoutConnections[0])
  128.                 {
  129.                     int spacer = 40;
  130.                     GUILayout.BeginVertical();
  131.  
  132.                     GUILayout.BeginHorizontal();
  133.                     GUILayout.Space(spacer);
  134.                     builder.connections[0] = EditorGUILayout.ToggleLeft("Up", builder.connections[0]);
  135.                     GUILayout.EndHorizontal();
  136.  
  137.                     GUILayout.BeginHorizontal();
  138.                     builder.connections[3] = EditorGUILayout.ToggleLeft("Left", builder.connections[3], GUILayout.Width(60));
  139.                     builder.connections[1] = EditorGUILayout.ToggleLeft("Right", builder.connections[1], GUILayout.Width(60));
  140.                     GUILayout.EndHorizontal();
  141.  
  142.                     GUILayout.BeginHorizontal();
  143.                     GUILayout.Space(spacer);
  144.                     builder.connections[2] = EditorGUILayout.ToggleLeft("Down", builder.connections[2]);
  145.                     GUILayout.EndHorizontal();
  146.  
  147.                     GUILayout.EndVertical();
  148.                 }
  149.             }
  150.             else
  151.             {
  152.                 int spacer = 40;
  153.                 for (int i = builder.foldoutConnections.Length - 1; i >= 0; i--)
  154.                 {
  155.                     int y = i % builder.templates.y;
  156.                     int x = i / builder.templates.y;
  157.                     builder.foldoutConnections[i] = EditorGUILayout.Foldout(builder.foldoutConnections[i], "Template " + (i + 1) + " (" + x + "," + y + ") connections");
  158.                     if (builder.foldoutConnections[i])
  159.                     {
  160.                         GUILayout.BeginVertical();
  161.  
  162.                         GUILayout.BeginHorizontal();
  163.                         GUILayout.Space(spacer);
  164.                         builder.connections[i * 4 + 0] = EditorGUILayout.ToggleLeft("Up", builder.connections[i * 4 + 0]);
  165.                         GUILayout.EndHorizontal();
  166.  
  167.                         GUILayout.BeginHorizontal();
  168.                         builder.connections[i * 4 + 3] = EditorGUILayout.ToggleLeft("Left", builder.connections[i * 4 + 3], GUILayout.Width(60));
  169.                         builder.connections[i * 4 + 1] = EditorGUILayout.ToggleLeft("Right", builder.connections[i * 4 + 1], GUILayout.Width(60));
  170.                         GUILayout.EndHorizontal();
  171.  
  172.                         GUILayout.BeginHorizontal();
  173.                         GUILayout.Space(spacer);
  174.                         builder.connections[i * 4 + 2] = EditorGUILayout.ToggleLeft("Down", builder.connections[i * 4 + 2]);
  175.                         GUILayout.EndHorizontal();
  176.  
  177.                         GUILayout.EndVertical();
  178.                     }
  179.                 }
  180.             }
  181.  
  182.             builder.necessities = EditorGUILayout.Foldout(builder.necessities, "Necessary Objects");
  183.             if (builder.necessities)
  184.             {
  185.                 builder.prefabContainer = EditorGUILayout.ObjectField("Prefab Container", builder.prefabContainer, typeof(Transform), true) as Transform;
  186.                 builder.colliderContainer = EditorGUILayout.ObjectField("Collider Container", builder.colliderContainer, typeof(Transform), true) as Transform;
  187.                 EditorGUILayout.PropertyField(_borderTile, new GUIContent("Border Tile"));
  188.                 EditorGUILayout.PropertyField(_edgeTile, new GUIContent("Mark Tile"));
  189.                 EditorGUILayout.PropertyField(_blankTile, new GUIContent("Blank Tile"));
  190.                 EditorGUILayout.PropertyField(_colliderPrefabs, new GUIContent("Collider Prefabs"), true);
  191.             }
  192.  
  193.             serializedObject.ApplyModifiedProperties();
  194.         }
  195.  
  196.         void CreateTemplate(TemplateBuilder builder)
  197.         {
  198.             //Verify the folders are present
  199.             AssetHelper.VerifyFolderStructure("Assets/" + builder.templateFolder);
  200.  
  201.             //Find the maps
  202.             Tilemap[] maps = FindObjectsOfType<Tilemap>();
  203.             Tilemap foreground = null;
  204.             Tilemap terrain = null;
  205.             Tilemap background = null;
  206.             for (int i = 0; i < maps.Length; i++)
  207.             {
  208.                 if (maps[i].name.Equals("Terrain"))
  209.                     terrain = maps[i];
  210.                 else if (maps[i].name.Equals("Background"))
  211.                     background = maps[i];
  212.                 else if (maps[i].name.Equals("Foreground"))
  213.                     foreground = maps[i];
  214.             }
  215.             if (background == null || foreground == null)
  216.                 Debug.LogWarning("Foreground: " + (foreground == null ? "null" : foreground.ToString()) + ", Background: " + (background == null ? "null" : background.ToString()));
  217.  
  218.             //Build colliderData for the map
  219.             if (builder.colliderContainer == null)
  220.             {
  221.                 GameObject cC = GameObject.Find("Template Colliders");
  222.                 builder.colliderContainer = cC != null ? cC.transform : null;
  223.             }
  224.             if (builder.prefabContainer == null)
  225.             {
  226.                 GameObject pC = GameObject.Find("Template Prefabs");
  227.                 builder.prefabContainer = pC != null ? pC.transform : null;
  228.             }
  229.  
  230.             if (builder.colliderContainer == null || builder.prefabContainer == null)
  231.                 Debug.LogWarning("Collider Container or Prefab Container is missing");
  232.  
  233.             //Create the template(s)
  234.             int templates = builder.templates.x * builder.templates.y;
  235.             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
  236.             if (templates == 1)
  237.             {
  238.                 Template t = ScriptableObject.CreateInstance<Template>();
  239.                 t.terrain = terrain.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
  240.  
  241.                 t.terrainRotation = new Matrix4x4[templateSize.x * templateSize.y];
  242.                 for (int y = 0; y < templateSize.y; y++)
  243.                     for (int x = 0; x < templateSize.x; x++)
  244.                         t.terrainRotation[x + y * templateSize.y] = terrain.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 0));
  245.  
  246.                 if (background != null)
  247.                 {
  248.                     t.background = background.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
  249.                     t.backgroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
  250.                     for (int y = 0; y < templateSize.y; y++)
  251.                         for (int x = 0; x < templateSize.x; x++)
  252.                             t.backgroundRotation[x + y * templateSize.y] = background.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
  253.                 }
  254.                 if (foreground != null)
  255.                 {
  256.                     t.foreground = foreground.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
  257.                     t.foregroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
  258.                     for (int y = 0; y < templateSize.y; y++)
  259.                         for (int x = 0; x < templateSize.x; x++)
  260.                             t.foregroundRotation[x + y * templateSize.y] = foreground.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
  261.                 }
  262.  
  263.                 // Matrix4x4 transMatrix = map.GetTransformMatrix(new Vector3Int(x + mapBounds.xMin, y + mapBounds.yMin, 0));
  264.  
  265.                 t.up = builder.connections[0];
  266.                 t.right = builder.connections[1];
  267.                 t.down = builder.connections[2];
  268.                 t.left = builder.connections[3];
  269.  
  270.                 //t.level = builder.level;
  271.                 t.size = new Vector2Int(builder.templateSize.x, builder.templateSize.y);
  272.  
  273.                 if (builder.colliderContainer != null)
  274.                     t.colliders = BuildColliders(builder, terrain, builder.colliderContainer, templateCorner);
  275.                 if (builder.prefabContainer != null)
  276.                     t.prefabs = GatherPrefabs(builder, builder.prefabContainer);
  277.  
  278.                 //Create the asset
  279.                 AssetDatabase.CreateAsset(t, "Assets/" + builder.templateFolder + builder.templateName + ".asset");
  280.                 Debug.Log("Template " + builder.templateName + " created successfully.");
  281.             }
  282.             else
  283.             {
  284.                 //Ensure folder structure is present
  285.                 AssetHelper.VerifyFolderStructure("Assets/" + builder.templateFolder + builder.templateSubfolder);
  286.  
  287.                 //Create master template
  288.                 TemplateMaster mt = ScriptableObject.CreateInstance<TemplateMaster>();
  289.                 mt.templateLayout = new Vector2Int(builder.templates.x, builder.templates.y);
  290.                 mt.templates = new Template[builder.templates.x * builder.templates.y];
  291.                 AssetDatabase.CreateAsset(mt, "Assets/" + builder.templateFolder + builder.templateName + ".asset");
  292.                 mt = AssetDatabase.LoadAssetAtPath<TemplateMaster>("Assets/" + builder.templateFolder + builder.templateName + ".asset"); //Reload for assignment
  293.  
  294.                 //Create the templates
  295.                 Template[] ts = new Template[templates];
  296.                 for (int i = 0; i < templates; i++)
  297.                 {
  298.                     Template t = ScriptableObject.CreateInstance<Template>();
  299.  
  300.                     int testX = i % builder.templates.x;
  301.                     int testY = i / builder.templates.x;
  302.                     //Debug.Log("Test X: "+testX+", Test Y: "+testY);
  303.                     int posX = testX * builder.templateSize.x + templateCorner.x;
  304.                     int posY = testY * builder.templateSize.y + templateCorner.y;
  305.                     //Debug.Log("X: "+ posX + ", Y: "+ posY);
  306.                     Vector3Int templateOrigin = new Vector3Int(posX, posY, 0);
  307.  
  308.                     t.terrain = terrain.GetTilesBlock(new BoundsInt(templateOrigin, templateSize));
  309.                     t.terrainRotation = new Matrix4x4[templateSize.x * templateSize.y];
  310.                     for (int y = 0; y < templateSize.y; y++)
  311.                         for (int x = 0; x < templateSize.x; x++)
  312.                             t.terrainRotation[x + y * templateSize.y] = terrain.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 0));
  313.  
  314.                     if (background != null)
  315.                     {
  316.                         t.background = background.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
  317.                         t.backgroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
  318.                         for (int y = 0; y < templateSize.y; y++)
  319.                             for (int x = 0; x < templateSize.x; x++)
  320.                                 t.backgroundRotation[x + y * templateSize.y] = background.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
  321.                     }
  322.                     if (foreground != null)
  323.                     {
  324.                         t.foreground = foreground.GetTilesBlock(new BoundsInt(templateCorner, templateSize));
  325.                         t.foregroundRotation = new Matrix4x4[templateSize.x * templateSize.y];
  326.                         for (int y = 0; y < templateSize.y; y++)
  327.                             for (int x = 0; x < templateSize.x; x++)
  328.                                 t.foregroundRotation[x + y * templateSize.y] = foreground.GetTransformMatrix(new Vector3Int(x + templateCorner.x, y + templateCorner.y, 1));
  329.                     }
  330.  
  331.                     t.up = builder.connections[i * 4 + 0];
  332.                     t.right = builder.connections[i * 4 + 1];
  333.                     t.down = builder.connections[i * 4 + 2];
  334.                     t.left = builder.connections[i * 4 + 3];
  335.  
  336.                     //t.level = builder.level;
  337.                     t.size = new Vector2Int(builder.templateSize.x, builder.templateSize.y);
  338.  
  339.                     //t.multiTemplateSize = builder.templates;
  340.                     t.master = mt;
  341.  
  342.                     if (builder.colliderContainer != null)
  343.                         t.colliders = BuildColliders(builder, terrain, builder.colliderContainer, templateOrigin);
  344.  
  345.                     ts[i] = t;
  346.                 }
  347.  
  348.                 //Assign the templates to the master
  349.                 for (int i = 0; i < templates; i++)
  350.                 {
  351.                     AssetDatabase.CreateAsset(ts[i], "Assets/" + builder.templateFolder + builder.templateSubfolder + builder.templateName + "." + (i + 1) + ".asset");
  352.                     ts[i].master = mt;
  353.                     mt.templates[i] = ts[i];
  354.                 }
  355.             }
  356.         }
  357.  
  358.         TemplateColliderData[] BuildColliders(TemplateBuilder builder, Tilemap terrain, Transform colliderContainer, Vector3Int templatePosition)
  359.         {
  360.             LevelAdjuster.BuildColliderData(terrain, colliderContainer.gameObject, new BoundsInt(templateCorner + templatePosition, new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1)));
  361.             TemplateColliderData[] colliderData = new TemplateColliderData[colliderContainer.transform.childCount];
  362.             for (int i = 0; i < colliderContainer.childCount; i++)
  363.             {
  364.                 Transform collider = colliderContainer.transform.GetChild(i);
  365.                 colliderData[i] = new TemplateColliderData();
  366.                 colliderData[i].location = new Vector2Int((int)collider.position.x - templatePosition.x, (int)collider.position.y - templatePosition.y); //Fix location for position in template
  367.  
  368.                 if (collider.name.Equals("Box Collider"))
  369.                 {
  370.                     BoxCollider2D box = collider.gameObject.GetComponent<BoxCollider2D>();
  371.                     colliderData[i].size = box.size;
  372.                     colliderData[i].offset = box.offset;
  373.                 }
  374.                 else //Look up which collider this is in a list
  375.                 {
  376.                     for (int n = 0; n < builder.colliderPrefabs.Length; n++)
  377.                     {
  378.                         if (collider.name.Equals(builder.colliderPrefabs[n].name))
  379.                         {
  380.                             colliderData[i].prefab = builder.colliderPrefabs[n];
  381.                             break;
  382.                         }
  383.                     }
  384.                 }
  385.             }
  386.  
  387.             return colliderData;
  388.         }
  389.  
  390.         TemplatePrefabData[] GatherPrefabs(TemplateBuilder builder, Transform prefabContainer)
  391.         {
  392.             TemplatePrefabData[] dataGroup = new TemplatePrefabData[prefabContainer.childCount];
  393.             for (int i = 0; i < prefabContainer.childCount; i++)
  394.             {
  395.                 Transform go = prefabContainer.GetChild(i);
  396.                 dataGroup[i] = new TemplatePrefabData();
  397.                 dataGroup[i].prefab = PrefabUtility.GetPrefabParent(go.gameObject) as GameObject;
  398.                 dataGroup[i].position = go.localPosition;
  399.                 dataGroup[i].rotation = go.localRotation;
  400.                 dataGroup[i].scale = go.localScale;
  401.                 if (dataGroup[i].prefab == null)
  402.                     Debug.LogError("GameObject " + go.name + " does not have a prefab.");
  403.             }
  404.             return dataGroup;
  405.         }
  406.  
  407.         void LoadTemplate(TemplateBuilder builder)
  408.         {
  409.             //Find the maps
  410.             Tilemap[] maps = FindObjectsOfType<Tilemap>();
  411.             //TileBase[] blanks = new TileBase[builder.templateSize.x * builder.templateSize.y];
  412.             Tilemap foreground = null;
  413.             Tilemap terrain = null;
  414.             Tilemap background = null;
  415.             for (int i = 0; i < maps.Length; i++)
  416.             {
  417.                 if (maps[i].name.Equals("Foreground"))
  418.                     foreground = maps[i];
  419.                 else if (maps[i].name.Equals("Terrain"))
  420.                     terrain = maps[i];
  421.                 else if (maps[i].name.Equals("Background"))
  422.                     background = maps[i];
  423.                 //maps[i].SetTilesBlock(new BoundsInt(_templateCorner, builder.templateSize), blanks);
  424.             }
  425.  
  426.             //Load the template
  427.             Template t = AssetDatabase.LoadAssetAtPath<Template>("Assets/" + builder.templateFolder + builder.templateName + ".asset");
  428.  
  429.             if (t != null) //Single template
  430.             {
  431.                 Debug.Log("Loading template " + _templateName + ". Tilemaps: " + foreground + ", " + terrain + ", " + background);
  432.                 builder.templateSize = new Vector2Int(t.size.x, t.size.y);
  433.                 Vector2Int templateLayout = t.master == null ? Vector2Int.one : t.master.templateLayout;
  434.                 Vector3Int templateSize = new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1);
  435.                 AdjustTemplate(builder, templateLayout, true);
  436.  
  437.                 if (foreground != null)
  438.                 {
  439.                     foreground.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.foreground);
  440.                     if (t.foregroundRotation.Length > 0)
  441.                         for (int y = 0; y < templateSize.y; y++)
  442.                             for (int x = 0; x < templateSize.x; x++)
  443.                                 foreground.SetTransformMatrix(new Vector3Int(x, y, 1), t.foregroundRotation[x + y * templateSize.y]);
  444.                 }
  445.                 terrain.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.terrain);
  446.                 if (t.terrainRotation.Length > 0)
  447.                     for (int y = 0; y < templateSize.y; y++)
  448.                         for (int x = 0; x < templateSize.x; x++)
  449.                             terrain.SetTransformMatrix(new Vector3Int(x, y, 0), t.terrainRotation[x + y * templateSize.y]);
  450.  
  451.                 if (background != null)
  452.                 {
  453.                     background.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.background);
  454.                     if (t.backgroundRotation.Length > 0)
  455.                         for (int y = 0; y < templateSize.y; y++)
  456.                             for (int x = 0; x < templateSize.x; x++)
  457.                                 background.SetTransformMatrix(new Vector3Int(x, y, 1), t.backgroundRotation[x + y * templateSize.y]);
  458.                 }
  459.  
  460.                 builder.connections = new bool[] { t.up, t.right, t.down, t.left };
  461.  
  462.                 //TODO: Load colliders and prefabs
  463.                 for (int i = 0; i < t.prefabs.Length; i++)
  464.                 {
  465.                     TemplatePrefabData data = t.prefabs[i];
  466.                     GameObject go = PrefabUtility.InstantiatePrefab(data.prefab) as GameObject;//Instantiate(data.prefab);
  467.                     go.transform.SetParent(builder.prefabContainer);
  468.                     go.name = data.prefab.name + " " + (i + 1).ToString();
  469.                     go.transform.localPosition = data.position;
  470.                     go.transform.localRotation = data.rotation;
  471.                     go.transform.localScale = data.scale;
  472.                 }
  473.  
  474.                 Debug.Log("Template " + builder.templateName + " loaded successfully.");
  475.             }
  476.             else
  477.             {
  478.                 TemplateMaster mt = AssetDatabase.LoadAssetAtPath<TemplateMaster>("Assets/" + builder.templateFolder + builder.templateName + ".asset");
  479.                 if (mt != null)
  480.                 {
  481.                     t = mt.templates[0];
  482.                     builder.templateSize = new Vector2Int(t.size.x, t.size.y);
  483.                     Vector2Int templateLayout = t.master == null ? Vector2Int.one : t.master.templateLayout;
  484.                     AdjustTemplate(builder, templateLayout, true);
  485.  
  486.                     Vector3Int templateSize = new Vector3Int(builder.templateSize.x, builder.templateSize.y, 1);
  487.                     builder.templates = mt.templateLayout;
  488.                     int templates = builder.templates.x * builder.templates.y;
  489.                     builder.connections = new bool[templates * 4];
  490.  
  491.                     for (int i = 0; i < templates; i++)
  492.                     {
  493.                         int testX = i % builder.templates.x;
  494.                         int testY = i / builder.templates.x;
  495.                         //Debug.Log("Test X: "+testX+", Test Y: "+testY);
  496.                         int posX = testX * builder.templateSize.x + templateCorner.x;
  497.                         int posY = testY * builder.templateSize.y + templateCorner.y;
  498.                         Vector3Int templateOrigin = new Vector3Int(posX, posY, 0);
  499.  
  500.                         builder.connections[i * 4 + 0] = t.up;
  501.                         builder.connections[i * 4 + 1] = t.right;
  502.                         builder.connections[i * 4 + 2] = t.down;
  503.                         builder.connections[i * 4 + 3] = t.left;
  504.  
  505.                         Debug.Log("Master Template " + mt.name + " loaded successfully");
  506.                         if (foreground != null)
  507.                         {
  508.                             foreground.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.foreground);
  509.                             if (t.foregroundRotation.Length > 0)
  510.                                 for (int y = 0; y < templateSize.y; y++)
  511.                                     for (int x = 0; x < templateSize.x; x++)
  512.                                         foreground.SetTransformMatrix(new Vector3Int(x, y, 1), t.foregroundRotation[x + y * templateSize.y]);
  513.                         }
  514.                         terrain.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.terrain);
  515.                         if (t.terrainRotation.Length > 0)
  516.                             for (int y = 0; y < templateSize.y; y++)
  517.                                 for (int x = 0; x < templateSize.x; x++)
  518.                                     terrain.SetTransformMatrix(new Vector3Int(x, y, 0), t.terrainRotation[x + y * templateSize.y]);
  519.  
  520.                         if (background != null)
  521.                         {
  522.                             background.SetTilesBlock(new BoundsInt(templateCorner, templateSize), t.background);
  523.                             if (t.backgroundRotation.Length > 0)
  524.                                 for (int y = 0; y < templateSize.y; y++)
  525.                                     for (int x = 0; x < templateSize.x; x++)
  526.                                         background.SetTransformMatrix(new Vector3Int(x, y, 1), t.backgroundRotation[x + y * templateSize.y]);
  527.                         }
  528.                     }
  529.                 }
  530.  
  531.                 else
  532.                 {
  533.                     Debug.Log("Could not find any template with the name " + builder.templateName + ".");
  534.                 }
  535.             }
  536.         }
  537.  
  538.         //TODO: Clear previous size of border, not current
  539.         void AdjustTemplate(TemplateBuilder builder, Vector2Int newTemplateAmount, bool clear)
  540.         {
  541.             //Find the maps and clear them. Also find the background map
  542.             Tilemap[] maps = FindObjectsOfType<Tilemap>();
  543.             TileBase[] blanks = new TileBase[builder.templateSize.x * builder.templateSize.y * builder.templates.x * builder.templates.y];
  544.             Tilemap border = null;
  545.  
  546.             Vector3Int clearSize = new Vector3Int(builder.templateSize.x * builder.templates.x, builder.templateSize.y * builder.templates.y, 1);
  547.             for (int i = 0; i < maps.Length; i++)
  548.             {
  549.                 if (clear)
  550.                     maps[i].SetTilesBlock(new BoundsInt(templateCorner, clearSize), blanks);
  551.                 if (maps[i].name.Contains("Template"))
  552.                     border = maps[i];
  553.             }
  554.  
  555.             if (clear)
  556.             {
  557.                 if (builder.colliderContainer != null)
  558.                     for (int i = builder.colliderContainer.childCount - 1; i >= 0; i--)
  559.                         GameObject.DestroyImmediate(builder.colliderContainer.GetChild(i).gameObject);
  560.  
  561.                 //Debug.Log(builder.prefabContainer + " Prefabs on child: " + builder.prefabContainer.childCount);
  562.                 if (builder.prefabContainer != null)
  563.                     for (int i = 0; i < builder.prefabContainer.childCount; i++)
  564.                         GameObject.DestroyImmediate(builder.prefabContainer.GetChild(i).gameObject);
  565.             }
  566.  
  567.             //Reset the borders
  568.             border.ClearAllTiles();
  569.             builder.templates = newTemplateAmount;
  570.  
  571.             //Blank tile, useful for grabbing
  572.             border.SetTile(new Vector3Int(templateCorner.x - 3, builder.templateSize.y * builder.templates.y - 0, 0), builder.blankTile);
  573.  
  574.             //Bottom left
  575.             border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 1, 0), builder.borderTile);
  576.             border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 1, 0), builder.borderTile);
  577.             border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y, 0), builder.borderTile);
  578.  
  579.             //Top left
  580.             border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
  581.             border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y + builder.templateSize.y * builder.templates.y - 0, 0), builder.borderTile);
  582.             border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y + builder.templateSize.y * builder.templates.y - 1, 0), builder.borderTile);
  583.  
  584.             //bottom right
  585.             border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y - 1, 0), builder.borderTile);
  586.             border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 1, templateCorner.y - 1, 0), builder.borderTile);
  587.             border.SetTile(new Vector3Int(templateCorner.x + builder.templateSize.x * builder.templates.x - 0, templateCorner.y - 0, 0), builder.borderTile);
  588.  
  589.             //Top right
  590.             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);
  591.             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);
  592.             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);
  593.  
  594.             //Left and right sides
  595.             if (builder.templateSize.y > 1)
  596.             {
  597.                 for (int i = 1; i < builder.templates.y; i++)
  598.                 {
  599.                     //Left
  600.                     border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.borderTile);
  601.                     border.SetTile(new Vector3Int(templateCorner.x - 1, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.borderTile);
  602.                     border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.markTile);
  603.                     border.SetTile(new Vector3Int(templateCorner.x - 0, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.markTile);
  604.  
  605.                     //Right
  606.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * builder.templates.x, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.borderTile);
  607.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * builder.templates.x, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.borderTile);
  608.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * builder.templates.x, templateCorner.y - 1 + builder.templateSize.y * i, 0), builder.markTile);
  609.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * builder.templates.x, templateCorner.y - 0 + builder.templateSize.y * i, 0), builder.markTile);
  610.                 }
  611.             }
  612.  
  613.             //Top and bottom sides
  614.             if (builder.templateSize.x > 1)
  615.             {
  616.                 for (int i = 1; i < builder.templates.x; i++)
  617.                 {
  618.                     //Bottom
  619.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 1, 0), builder.borderTile);
  620.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 1, 0), builder.borderTile);
  621.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 0, 0), builder.markTile);
  622.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 0, 0), builder.markTile);
  623.  
  624.                     //Top
  625.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 1 + builder.templateSize.y * builder.templates.y, 0), builder.markTile);
  626.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 1 + builder.templateSize.y * builder.templates.y, 0), builder.markTile);
  627.                     border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * i, templateCorner.y - 0 + builder.templateSize.y * builder.templates.y, 0), builder.borderTile);
  628.                     border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * i, templateCorner.y - 0 + builder.templateSize.y * builder.templates.y, 0), builder.borderTile);
  629.                 }
  630.             }
  631.  
  632.             //Center parts
  633.             if (builder.templateSize.x > 1 && builder.templateSize.y > 1)
  634.             {
  635.                 for (int x = 1; x < builder.templates.x; x++)
  636.                 {
  637.                     for (int y = 1; y < builder.templates.y; y++)
  638.                     {
  639.                         border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * x, templateCorner.y - 1 + builder.templateSize.y * y, 0), builder.markTile);
  640.                         border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * x, templateCorner.y - 1 + builder.templateSize.y * y, 0), builder.markTile);
  641.                         border.SetTile(new Vector3Int(templateCorner.x - 1 + builder.templateSize.x * x, templateCorner.y - 0 + builder.templateSize.y * y, 0), builder.markTile);
  642.                         border.SetTile(new Vector3Int(templateCorner.x - 0 + builder.templateSize.x * x, templateCorner.y - 0 + builder.templateSize.y * y, 0), builder.markTile);
  643.                     }
  644.                 }
  645.             }
  646.         }
  647.     }
  648. }
  649.  
Advertisement
Add Comment
Please, Sign In to add comment