Pull request #6: Maintenant, le level editor génère les tilemaps
Merge in CGD/gather-and-defend from feature/LevelEditor_GenerateTilemaps to main * commit 'ac0e52e0df5857fc846810e4bfc5b6c613e9f4ed': Maintenant, le level editor génère les tilemaps
This commit is contained in:
commit
1f3e29b4e7
@ -24,8 +24,7 @@ namespace GatherAndDefend.LevelEditor
|
|||||||
- Load button : iterates through all scene tilemaps and fills them according to what data can be found in the level file.
|
- Load button : iterates through all scene tilemaps and fills them according to what data can be found in the level file.
|
||||||
|
|
||||||
Important considerations :
|
Important considerations :
|
||||||
- the save and load buttons won't work if there is no Level assigned.
|
- the save and load buttons won't work if there is no Level assigned.", MessageType.None);
|
||||||
- the loading won't work if the scene tilemap names aren't the same as the tilemap data keys in the Level file", MessageType.None);
|
|
||||||
|
|
||||||
DrawDefaultInspector();
|
DrawDefaultInspector();
|
||||||
if (GUILayout.Button(nameof(Create)))
|
if (GUILayout.Button(nameof(Create)))
|
||||||
@ -98,7 +97,7 @@ Important considerations :
|
|||||||
|
|
||||||
foreach (Tilemap tilemap in targ.GetComponentsInChildren<Tilemap>())
|
foreach (Tilemap tilemap in targ.GetComponentsInChildren<Tilemap>())
|
||||||
{
|
{
|
||||||
map.Write(tilemap);
|
map.SaveFromTilemap(tilemap);
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetDatabase.CreateAsset(map, path + "/" + name + extension);
|
AssetDatabase.CreateAsset(map, path + "/" + name + extension);
|
||||||
@ -114,10 +113,17 @@ Important considerations :
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (Tilemap tilemap in targ.GetComponentsInChildren<Tilemap>())
|
while (targ.transform.childCount > 0)
|
||||||
{
|
{
|
||||||
tilemap.ClearAllTiles();
|
DestroyImmediate(targ.transform.GetChild(0).gameObject);
|
||||||
targ.Level.Read(tilemap);
|
}
|
||||||
|
|
||||||
|
foreach (TilemapData tilemapData in targ.Level)
|
||||||
|
{
|
||||||
|
var tilemap = new GameObject(tilemapData.Key).AddComponent<Tilemap>();
|
||||||
|
var rend = tilemap.gameObject.AddComponent<TilemapRenderer>();
|
||||||
|
tilemapData.LoadToTilemap(tilemap);
|
||||||
|
tilemap.transform.SetParent(targ.transform);
|
||||||
}
|
}
|
||||||
_infoText = string.Empty;
|
_infoText = string.Empty;
|
||||||
EditorSceneManager.MarkAllScenesDirty();
|
EditorSceneManager.MarkAllScenesDirty();
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Tilemaps;
|
|
||||||
|
|
||||||
namespace GatherAndDefend.LevelEditor
|
namespace GatherAndDefend.LevelEditor
|
||||||
{
|
{
|
||||||
|
[RequireComponent(typeof(Grid))]
|
||||||
public class LevelEditor : MonoBehaviour
|
public class LevelEditor : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
|
|||||||
@ -1,25 +1,36 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine.Tilemaps;
|
using UnityEngine.Tilemaps;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace GatherAndDefend.LevelEditor
|
namespace GatherAndDefend.LevelEditor
|
||||||
{
|
{
|
||||||
public class Level : ScriptableObject
|
public class Level : ScriptableObject, IEnumerable<TilemapData>
|
||||||
{
|
{
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private List<TilemapData> _data = new List<TilemapData>();
|
private List<TilemapData> _data = new List<TilemapData>();
|
||||||
public void Write(Tilemap tilemap)
|
public void SaveFromTilemap(Tilemap tilemap)
|
||||||
{
|
{
|
||||||
var data = new TilemapData();
|
var data = new TilemapData();
|
||||||
data.SaveFromTilemap(tilemap);
|
data.SaveFromTilemap(tilemap);
|
||||||
_data.Add(data);
|
_data.Add(data);
|
||||||
}
|
}
|
||||||
public void Read(Tilemap tilemap)
|
public void LoadToTilemap(Tilemap tilemap)
|
||||||
{
|
{
|
||||||
var data = _data.Find(x => x.Key == tilemap.name);
|
var data = _data.Find(x => x.Key == tilemap.name);
|
||||||
if (data == null) return;
|
if (data == null) return;
|
||||||
|
|
||||||
data.LoadToTilemap(tilemap);
|
data.LoadToTilemap(tilemap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerator<TilemapData> GetEnumerator()
|
||||||
|
{
|
||||||
|
return _data.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return _data.GetEnumerator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,22 +2,46 @@
|
|||||||
using UnityEngine.Tilemaps;
|
using UnityEngine.Tilemaps;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace GatherAndDefend.LevelEditor
|
namespace GatherAndDefend.LevelEditor
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class TilemapData
|
public class TilemapData : IEnumerable<TileData>
|
||||||
{
|
{
|
||||||
|
public const int INVISIBLE_LAYER = 6;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private string _key;
|
private string _key;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private List<TileData> tiles;
|
private List<TileData> _tiles;
|
||||||
|
[SerializeField]
|
||||||
|
private bool _isInvisible;
|
||||||
|
[SerializeField]
|
||||||
|
private bool _isCollidable;
|
||||||
|
[SerializeField]
|
||||||
|
private bool _isTrigger;
|
||||||
|
[SerializeField]
|
||||||
|
private int _renderOrder;
|
||||||
|
[SerializeField]
|
||||||
|
private string _renderLayer;
|
||||||
|
|
||||||
|
|
||||||
public string Key => _key;
|
public string Key => _key;
|
||||||
|
|
||||||
public void LoadToTilemap(Tilemap reference)
|
public void LoadToTilemap(Tilemap reference)
|
||||||
{
|
{
|
||||||
foreach (TileData data in tiles)
|
var rend = reference.GetComponent<TilemapRenderer>();
|
||||||
|
rend.sortingOrder = _renderOrder;
|
||||||
|
rend.sortingLayerName = _renderLayer;
|
||||||
|
if (_isInvisible) rend.gameObject.layer = INVISIBLE_LAYER;
|
||||||
|
if (_isCollidable)
|
||||||
|
{
|
||||||
|
var collision = rend.gameObject.AddComponent<TilemapCollider2D>();
|
||||||
|
collision.isTrigger = _isTrigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foreach (TileData data in _tiles)
|
||||||
{
|
{
|
||||||
reference.SetTile(data.Position, data.Tile);
|
reference.SetTile(data.Position, data.Tile);
|
||||||
}
|
}
|
||||||
@ -25,7 +49,18 @@ namespace GatherAndDefend.LevelEditor
|
|||||||
public void SaveFromTilemap(Tilemap reference)
|
public void SaveFromTilemap(Tilemap reference)
|
||||||
{
|
{
|
||||||
_key = reference.name;
|
_key = reference.name;
|
||||||
tiles = new List<TileData>();
|
if (_isCollidable = reference.GetComponent<TilemapCollider2D>())
|
||||||
|
{
|
||||||
|
_isTrigger = reference.GetComponent<TilemapCollider2D>().isTrigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isInvisible = reference.gameObject.layer == INVISIBLE_LAYER;
|
||||||
|
_renderLayer = reference.GetComponent<TilemapRenderer>().sortingLayerName;
|
||||||
|
_renderOrder = reference.GetComponent<TilemapRenderer>().sortingOrder;
|
||||||
|
|
||||||
|
|
||||||
|
_tiles = new List<TileData>();
|
||||||
|
|
||||||
BoundsInt bounds = reference.cellBounds;
|
BoundsInt bounds = reference.cellBounds;
|
||||||
for (int i = bounds.xMin; i <= bounds.xMax; i++)
|
for (int i = bounds.xMin; i <= bounds.xMax; i++)
|
||||||
{
|
{
|
||||||
@ -35,13 +70,24 @@ namespace GatherAndDefend.LevelEditor
|
|||||||
TileBase tile = reference.GetTile(position);
|
TileBase tile = reference.GetTile(position);
|
||||||
if (!tile) continue;
|
if (!tile) continue;
|
||||||
var tileData = new TileData(position, tile);
|
var tileData = new TileData(position, tile);
|
||||||
tiles.Add(tileData);
|
_tiles.Add(tileData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerator<TileData> GetEnumerator()
|
||||||
|
{
|
||||||
|
return _tiles.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return _tiles.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
public TilemapData()
|
public TilemapData()
|
||||||
{
|
{
|
||||||
tiles = new List<TileData>();
|
_tiles = new List<TileData>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user