Created
April 26, 2020 11:37
-
-
Save longtran2904/7fa924c1770dff91b5c0828fa0cdbb7f to your computer and use it in GitHub Desktop.
Custom room template initializer and custom tilemaplayers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Assets.ProceduralLevelGenerator.Scripts.Generators.Common.RoomTemplates.RoomTemplateInitializers; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
namespace Assets.Scripts | |
{ | |
public class CustomRoomTemplateInitializer : BaseRoomTemplateInitializer | |
{ | |
public override void Initialize() | |
{ | |
base.Initialize(); | |
// Here is where i place my custom gameobject | |
var cameraInfoObject = CreateCustomGameObject<CameraInfo>("Camera Info", gameObject); | |
var lightsObject = CreateCustomGameObject("Lights", gameObject); | |
var enemiesObject = CreateCustomGameObject<EnemySpawner>("Enemies", gameObject); | |
} | |
// Here is a function i made to create custom gameobject with different components | |
protected GameObject CreateCustomGameObject<T>(string name, GameObject parentObject) where T : Component | |
{ | |
var tilemapObject = new GameObject(name); | |
tilemapObject.transform.SetParent(parentObject.transform); | |
var customScript = gameObject.AddComponent<T>(); | |
return tilemapObject; | |
} | |
protected GameObject CreateCustomGameObject(string name, GameObject parentObject) | |
{ | |
var tilemapObject = new GameObject(name); | |
tilemapObject.transform.SetParent(parentObject.transform); | |
return tilemapObject; | |
} | |
protected override void InitializeTilemaps(GameObject tilemapsRoot) | |
{ | |
// Create an instance of your tilemap layers handler | |
var tilemapLayersHandler = ScriptableObject.CreateInstance<CustomTilemapsLayersHandler>(); | |
// Initialize tilemaps | |
tilemapLayersHandler.InitializeTilemaps(tilemapsRoot); | |
} | |
// Add create menu field to create a room template with this room template initializer | |
// This should be simplified in the next version of the plugin so that users do not have to copy paste this | |
#if UNITY_EDITOR | |
[MenuItem("Assets/Create/Dungeon generator/Custom room template")] | |
public static void CreatePlatformerRoomTemplate() | |
{ | |
// Create empty game object | |
var roomTemplate = new GameObject(); | |
// Add room template initializer, initialize room template, destroy initializer | |
// ONLY THIS LINE HAS TO BE CHANGED if we want to add a menu item with a different RoomTemplateInitializer | |
var roomTemplateInitializer = roomTemplate.AddComponent<CustomRoomTemplateInitializer>(); | |
roomTemplateInitializer.Initialize(); | |
Object.DestroyImmediate(roomTemplateInitializer); | |
// Save prefab | |
var currentPath = GetCurrentPath(); | |
PrefabUtility.SaveAsPrefabAsset(roomTemplate, AssetDatabase.GenerateUniqueAssetPath(currentPath + "/Custom Room template.prefab")); | |
// Remove game object from scene | |
Object.DestroyImmediate(roomTemplate); | |
} | |
public static string GetCurrentPath() | |
{ | |
var path = AssetDatabase.GetAssetPath(Selection.activeObject); | |
if (path == "") | |
{ | |
path = "Assets"; | |
} | |
else if (Path.GetExtension(path) != "") | |
{ | |
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), ""); | |
} | |
return path; | |
} | |
#endif | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Assets.ProceduralLevelGenerator.Scripts.Generators.Common.RoomTemplates.TilemapLayers; | |
using UnityEngine; | |
using UnityEngine.Tilemaps; | |
namespace Assets.Scripts | |
{ | |
// TilemapLayersHandlerBase inherit from ScriptableObject so we need to create an asset menu field that we will use to create the scriptable object instance | |
// The menu name can be changed to anything you want | |
[CreateAssetMenu(menuName = "Dungeon generator/Custom tilemap layers handler", fileName = "CustomTilemapLayersHandler")] | |
public class CustomTilemapsLayersHandler : TilemapLayersHandlerBase | |
{ | |
public override void InitializeTilemaps(GameObject gameObject) | |
{ | |
// First make sure that you add the grid component | |
gameObject.AddComponent<Grid>(); | |
// And then create child game objects with their tilemaps | |
var background1TilemapObject = CreateTilemapGameObject("Background 1", gameObject, 0); | |
var background2TilemapObject = CreateTilemapGameObject("Background 2", gameObject, 1); | |
var wallsTilemapObject = CreateTilemapGameObject("Walls", gameObject, 2); | |
AddCollider(wallsTilemapObject); | |
var collideableTilemapObject = CreateTilemapGameObject("Collideable", gameObject, 3); | |
AddCollider(collideableTilemapObject); | |
var other2TilemapObject = CreateTilemapGameObject("Other 1", gameObject, 4); | |
var other3TilemapObject = CreateTilemapGameObject("Other 2", gameObject, 5); | |
} | |
protected GameObject CreateTilemapGameObject(string name, GameObject parentObject, int sortingOrder) | |
{ | |
var tilemapObject = new GameObject(name); | |
tilemapObject.transform.SetParent(parentObject.transform); | |
var tilemap = tilemapObject.AddComponent<Tilemap>(); | |
var tilemapRenderer = tilemapObject.AddComponent<TilemapRenderer>(); | |
tilemapRenderer.sortingOrder = sortingOrder; | |
return tilemapObject; | |
} | |
protected void AddCollider(GameObject gameObject, bool isTrigger = false) | |
{ | |
var tilemapCollider2D = gameObject.AddComponent<TilemapCollider2D>(); | |
tilemapCollider2D.isTrigger = isTrigger; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment