Unity 2D Preview PrefabTile
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; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| #if UNITY_EDITOR | |
| using UnityEditor; | |
| #endif | |
| using UnityEngine; | |
| using InfEssence.PrefabTiles; | |
| namespace UnityEngine.Tilemaps | |
| { | |
| [Serializable] | |
| public class PrefabTile : TileBase | |
| { | |
| [Tooltip("Sprite shown in Palette/Object layer")] | |
| public Sprite Preview; | |
| [Tooltip("Prefab to instantiate on tile placement")] | |
| public GameObject Prefab; | |
| [Tooltip("Gizmo Color for this tile")] | |
| public Color Color = Color.green; | |
| static GameObject Dummy = null; | |
| public override bool GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData) | |
| { | |
| if (Dummy == null) | |
| { | |
| Dummy = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/World/Palettes/ScriptedTiles/Dummy.prefab"); | |
| } | |
| if (Preview != null) | |
| { | |
| tileData.sprite = Preview; | |
| } | |
| if (IsObjectLayer(tileMap)) | |
| { | |
| tileData.transform = Matrix4x4.identity; | |
| tileData.color = Color.white; | |
| tileData.colliderType = Tile.ColliderType.None; | |
| tileData.gameObject = Dummy; | |
| //tileData.flags = Convert.ToInt32(TileValue(tileMap, location)) << 3; | |
| return true; | |
| } | |
| return IsPaletteLayer(tileMap); | |
| } | |
| public bool TileValue(ITilemap tileMap, Vector3Int position) | |
| { | |
| TileBase tile = tileMap.GetTile(position); | |
| return (tile != null && tile == this); | |
| } | |
| public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go) | |
| { | |
| if (IsObjectLayer(tilemap)) | |
| { | |
| if (go != null) | |
| { | |
| var s = go.GetComponent<PrefabTileDummy>(); | |
| if (s != null) | |
| { | |
| s.Setup(this, position, tilemap); | |
| } | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| bool IsObjectLayer(ITilemap tilemap) | |
| { | |
| return tilemap.tilemap.name == "Objects"; | |
| } | |
| bool IsPaletteLayer(ITilemap tilemap) | |
| { | |
| return tilemap.tilemap.name == "Layer1"; | |
| } | |
| #if UNITY_EDITOR | |
| [MenuItem("Assets/Create/Prefab Tile")] | |
| public static void CreateBrush() | |
| { | |
| string path = EditorUtility.SaveFilePanelInProject("Save Prefab Tile", "New Prefab Tile", "asset", "Save Prefab Tile", "Assets/World/Palettes/ScriptedTiles/PrefabTiles"); | |
| if (path == "") | |
| return; | |
| AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<PrefabTile>(), path); | |
| } | |
| public override Sprite GetPreview() | |
| { | |
| return Preview; | |
| } | |
| #endif | |
| } | |
| #if UNITY_EDITOR | |
| [CustomEditor(typeof(PrefabTile))] | |
| public class PrefabTileEditor : Editor | |
| { | |
| private PrefabTile tile { get { return (target as PrefabTile); } } | |
| public static PrefabTileEditor Instance { get; private set; } | |
| void Awake() | |
| { | |
| Instance = this; | |
| } | |
| } | |
| #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 System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using UnityEngine; | |
| using UnityEngine.Tilemaps; | |
| using UnityEditor; | |
| using MovementEffects; | |
| namespace InfEssence.PrefabTiles | |
| { | |
| [ExecuteInEditMode] | |
| public abstract class PrefabTileComponent : MonoBehaviour | |
| { | |
| [SerializeField, HideInInspector] | |
| public PrefabTile Tile; | |
| [SerializeField, HideInInspector] | |
| public Vector3Int PlacedPosition; | |
| [SerializeField, HideInInspector] | |
| public ITilemap ObjectTileMap; | |
| public virtual string Identifier { get { return string.Empty; } } | |
| public abstract void OnGameStart(); | |
| public void LoadObjects() | |
| { | |
| OnGameStart(); | |
| } | |
| public string InternalTag | |
| { | |
| get | |
| { | |
| if (Tile != null) | |
| { | |
| return string.Format("{0}|{1}|{2}", Tile.Prefab.name, PlacedPosition.x, PlacedPosition.y); | |
| } | |
| else | |
| { | |
| return string.Empty; | |
| } | |
| } | |
| } | |
| void OnDrawGizmosSelected() | |
| { | |
| if (Tile != null && ObjectTileMap != null) | |
| { | |
| Gizmos.color = new Color(Tile.Color.r, Tile.Color.g, Tile.Color.b, 0.6f); | |
| Gizmos.DrawCube(transform.position + ObjectTileMap.tilemap.cellSize * 0.5f, ObjectTileMap.tilemap.cellSize); | |
| } | |
| } | |
| private void Update() | |
| { | |
| if (ObjectTileMap == null) | |
| { | |
| var o = GameObject.Find("Objects"); | |
| if (o != null) | |
| ObjectTileMap = o.GetComponent<Tilemap>(); | |
| } | |
| if (ObjectTileMap != null) | |
| { | |
| var t = ObjectTileMap.GetTile<PrefabTile>(PlacedPosition); | |
| if (t == null) | |
| { | |
| GameObject.DestroyImmediate(gameObject); | |
| } | |
| } | |
| } | |
| } | |
| [CustomEditor(typeof(PrefabTileComponent), true)] | |
| public class PrefabTileComponentEditor : Editor | |
| { | |
| PrefabTileComponent Component { get { return target as PrefabTileComponent; } } | |
| string LastIdentifier = string.Empty; | |
| public override void OnInspectorGUI() | |
| { | |
| if (LastIdentifier != Component.Identifier) | |
| { | |
| if (Component.Tile != null) | |
| target.name = string.Format("{0}({1}) [{2}/{3}]", Component.Tile.Prefab.name, Component.Identifier, Component.PlacedPosition.x, Component.PlacedPosition.y); | |
| } | |
| LastIdentifier = Component.Identifier; | |
| base.OnInspectorGUI(); | |
| } | |
| } | |
| } |
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 UnityEngine; | |
| using System.Collections; | |
| using System; | |
| using UnityEngine.Tilemaps; | |
| using UnityEditor; | |
| namespace InfEssence.PrefabTiles | |
| { | |
| [ExecuteInEditMode] | |
| public class PrefabTileDummy : MonoBehaviour | |
| { | |
| Transform ObjectTilemap; | |
| public PrefabTile PrefabTileRef { get; private set; } | |
| public Vector3Int Position { get; private set; } | |
| public ITilemap ObjectTileMap { get; private set; } | |
| public GameObject Instance { get; private set; } | |
| public void Setup(PrefabTile t, Vector3Int pos, ITilemap tilemap) | |
| { | |
| PrefabTileRef = t; | |
| Position = pos; | |
| ObjectTileMap = tilemap; | |
| CreateInstance(); | |
| } | |
| void CreateInstance() | |
| { | |
| if (Instance == null) | |
| { | |
| var container = GetContainer(); | |
| Instance = GetRefObject(); | |
| if (Instance == null) | |
| { | |
| Instance = GameObject.Instantiate(PrefabTileRef.Prefab, transform.position, Quaternion.identity); | |
| Instance.transform.SetParent(container); | |
| Instance.name = GetObjectName(); | |
| Instance.GetComponent<PrefabTileComponent>().Tile = PrefabTileRef; | |
| Instance.GetComponent<PrefabTileComponent>().PlacedPosition = Position; | |
| Instance.GetComponent<PrefabTileComponent>().ObjectTileMap = ObjectTileMap; | |
| } | |
| } | |
| } | |
| GameObject GetRefObject() | |
| { | |
| var container = GetContainer(); | |
| foreach (Transform child in container) | |
| { | |
| var c = child.GetComponent<PrefabTileComponent>(); | |
| if (c != null) | |
| { | |
| if (c.InternalTag == GetInternalTag()) | |
| { | |
| return c.gameObject; | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| string GetInternalTag() | |
| { | |
| return string.Format("{0}|{1}|{2}", PrefabTileRef.Prefab.name, Position.x, Position.y); | |
| } | |
| string GetObjectName() | |
| { | |
| if (Instance != null) | |
| { | |
| var c = Instance.GetComponent<PrefabTileComponent>().Identifier; | |
| return string.Format("{0}({1}) [{2}/{3}]", PrefabTileRef.Prefab.name, c, Position.x, Position.y); | |
| } | |
| return string.Format("{0} ({1}/{2})", PrefabTileRef.Prefab.name, Position.x, Position.y); | |
| } | |
| Transform GetContainer() | |
| { | |
| if (ObjectTilemap == null) | |
| { | |
| ObjectTilemap = GameObject.Find("Objects").transform; | |
| } | |
| var container = ObjectTilemap.FindChild(PrefabTileRef.Prefab.name); | |
| if (container == null) | |
| { | |
| container = new GameObject(PrefabTileRef.Prefab.name).transform; | |
| container.SetParent(ObjectTilemap); | |
| container.tag = "ObjectContainer"; | |
| } | |
| return container; | |
| } | |
| } | |
| } |
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; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using UnityEngine; | |
| using InfEssence.Environment.Puzzle; | |
| namespace InfEssence.PrefabTiles | |
| { | |
| public class PuzzleButtonTile : PrefabTileComponent | |
| { | |
| public string PuzzleId = ""; | |
| public override string Identifier | |
| { | |
| get | |
| { | |
| return PuzzleId; | |
| } | |
| } | |
| public override void OnGameStart() | |
| { | |
| var data = new PuzzleSpawnData(PuzzleType.GroundSwitch, transform.position, PuzzleId, typeof(SA_DoubleSwitch)); | |
| PuzzleSpawns.puzzleSpawns.Add(data); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment