using LitJson; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; public class UITileSetting : MonoBehaviour { public Camera camera; public MapBlock block; public RectTransform rect; public Text txt; public Text txtTypeCurrent; public InputField inputDungeonName; [Space(10)] public Transform targetBlockType; public GameObject prefabItemToggle; public List blkTypes = new List(); [Space(10)] public Transform targetDoorType; public GameObject prefabItemDropdown; public List doorTypes = new List(); [Space(10)] public Toggle toggleOverlap; public Dropdown dropdownTypes; public Dropdown dropdownMaterials; public Dropdown dropdownObstacles; public InputField inputAngle; [Space(10)] public InputField inputJson; MapManager mapManager; private void Start() { if (mapManager == null) mapManager = (MapManager)FindObjectOfType(typeof(MapManager)); block.OnInitialized(mapManager, 0, Vector2.zero, MapBlock.Type.Empty);//에디터 Util.DropDownList.DropList(dropdownTypes, 0); int max = Util.EnumUtil.Length(); for (int i = 0; i < max; i++) { ItemDropdown item = Instantiate(prefabItemDropdown, targetDoorType).GetComponent(); item.OnInitialized($"{(MapDirection)i}", a => { OnChangedDoors(); }); doorTypes.Add(item); } max = Util.EnumUtil.Length(); for(int i = 0; i < max; i++) { ItemToggle item = Instantiate(prefabItemToggle, targetBlockType).GetComponent(); item.OnInitialized($"{(MapBlock.Type)i}", null); blkTypes.Add(item); } OnChangedDungeon();//실행 EventManager.Instance.Add("OnSelectTile", OnSelectTile); } private void OnDestroy() { EventManager.Instance.Remove("OnSelectTile", OnSelectTile); } MapTile tileSelect = null; void OnSelectTile(object value) { if (toggleOverlap.isOn && tileSelect == (MapTile)value) { OnApply(); return; } tileSelect = (MapTile)value; rect.transform.position = camera.WorldToScreenPoint(tileSelect.transform.position); txt.text = $"{tileSelect.name}\nIDX : {tileSelect.idx}"; txtTypeCurrent.text = $"{tileSelect.type} >"; } public void OnChangedDungeon() { for (int i = 0; i < blkTypes.Count; i++) blkTypes[i].toggle.isOn = true; for (int i = 0; i < doorTypes.Count; i++) { doorTypes[i].dropdown.value = (int)DoorType.Door; doorTypes[i].dropdown.RefreshShownValue(); } block.gameObject.SetActive(false); OnChangedDoors(); block.Setting(inputDungeonName.text);//에디터 Util.DropDownList.DropList(dropdownMaterials, mapManager.MaterialGet(inputDungeonName.text)[type].Select(x => x.name).ToList(), 0); Util.DropDownList.DropList(dropdownObstacles, mapManager.ObstacleGet(inputDungeonName.text).Select(x => x.name).ToList(), 0); OnChangedObstacle(); OnChangedMaterial(); } public void OnChangedDoors() { if (toggleHide.isOn) return; //block.Wall_Init(); block.Door_Init(); for (int i = 0; i < doorTypes.Count; i++) { MapDoor door = null; switch ((DoorType)doorTypes[i].dropdown.value) { case DoorType.Door: door = block.prefabDoorBasic; break; case DoorType.DoorBreak: door = block.prefabDoorBasicBreak; break; case DoorType.DoorLock: door = block.prefabDoorLock; break; case DoorType.Wall: door = block.prefabDoorWall; break; case DoorType.WallBreak: door = block.prefabDoorWallBreak; break; case DoorType.Boss: door = block.prefabDoorBoss; break; } block.Door_Create(door, (MapDirection)i); } //block.Setting(inputDungeonName.text); } public Toggle toggleHide; public void OnChangedWallHide(Toggle toggle) { if(toggleHide.isOn) { block.Door_Init(); block.Wall_Init(); } else { OnChangedDoors(); } } MapTile.Type type; public void OnChangedType() { type = (MapTile.Type)dropdownTypes.value; Util.DropDownList.DropList(dropdownMaterials, mapManager.MaterialGet(inputDungeonName.text)[type].Select(x => x.name).ToList(), 0); OnChangedMaterial(); } Material mat; public void OnChangedMaterial() { List list = mapManager.MaterialGet(inputDungeonName.text)[type]; if (list != null) { if (0 < list.Count) mat = list[dropdownMaterials.value]; else mat = null; } else mat = null; } public GameObject obstacle; public void OnChangedObstacle() { List list = mapManager.ObstacleGet(inputDungeonName.text); if (list != null && 0 < list.Count) obstacle = list[dropdownObstacles.value]; } public void OnObstacleInstall() { if (tileSelect == null) return; if (tileSelect.obstacle != null) OnObstacleUninstall(); tileSelect.obstacle = mapManager.ObjectCreateLocalPos(obstacle, tileSelect.transform, Vector3.one, Vector3.zero); tileSelect.obstacle.name = obstacle.name; List list = tileSelect.obstacle.GetComponentsInChildren().ToList(); for (int i = 0; i < list.Count; i++) list[i].enabled = false; } public void OnObstacleUninstall() { if (tileSelect == null || tileSelect.obstacle == null) return; tileSelect.obstacle.gameObject.SetActive(false); tileSelect.obstacle = null; } public void OnObstacleRotateL(InputField inputAngle) { if (tileSelect == null || tileSelect.obstacle == null) return; tileSelect.obstacle.transform.localEulerAngles += new Vector3(0, -float.Parse(inputAngle.text), 0); } public void OnObstacleRotateR(InputField inputAngle) { if (tileSelect == null || tileSelect.obstacle == null) return; tileSelect.obstacle.transform.localEulerAngles += new Vector3(0, float.Parse(inputAngle.text), 0); } public void OnApply() { if (tileSelect == null) return; if (mat == null) { Debug.LogError($"적용할 Material 없읍"); return; } tileSelect.OnApply(type, mat); } public void OnApply_ALL() { if (mat == null) { Debug.LogError($"적용할 Material 없읍"); return; } List tiles = new List(block.tiles.Values); for (int i = 0; i < tiles.Count; i++) { if (tiles[i].type == (MapTile.Type)dropdownTypes.value) tiles[i].OnApply((MapTile.Type)dropdownTypes.value, mat); } } public void OnJsonCreate() { TileData blockData = new TileData(); blockData.typeApply = new List(new bool[Util.EnumUtil.Length()]); for(int i = 0; i < blockData.typeApply.Count; i++) blockData.typeApply[i] = blkTypes[i].toggle.isOn; blockData.typeDoors = new List(new bool[Util.EnumUtil.Length()]); for (int i = 0; i < blockData.typeDoors.Count; i++) blockData.typeDoors[i] = (DoorType)doorTypes[i].dropdown.value != DoorType.Wall; blockData.tileDatas = new List(); List tiles = new List(block.tiles.Values); for (int i = 0; i < tiles.Count; i++) { if (tiles[i].type != MapTile.Type.None || tiles[i].obstacle != null) { if (tiles[i].obstacle == null) blockData.tileDatas.Add(Tile.Create(tiles[i].idx, tiles[i].type, string.Empty, 0)); else blockData.tileDatas.Add(Tile.Create(tiles[i].idx, tiles[i].type, tiles[i].obstacle.name, (int)tiles[i].obstacle.transform.eulerAngles.y)); } } inputJson.text = JsonMapper.ToJson(blockData); } public void OnJsonLoad() { OnChangedDungeon();//json 불러오기 TileData datas = JsonMapper.ToObject(inputJson.text); for (int i = 0; i < datas.typeApply.Count; i++) blkTypes[i].toggle.isOn = datas.typeApply[i]; for (int i = 0; i < datas.typeDoors.Count; i++) { doorTypes[i].dropdown.value = datas.typeDoors[i] ? (int)DoorType.Door : (int)DoorType.Wall; doorTypes[i].dropdown.RefreshShownValue(); } OnChangedDoors(); Vector2 idx = new Vector2(); for(int i = 0; i < datas.tileDatas.Count; i++) { idx.x = datas.tileDatas[i].x; idx.y = datas.tileDatas[i].y; MapTile tile = block.tiles[idx]; tile.OnApply(datas.tileDatas[i].type, mapManager.MaterialGet(inputDungeonName.text)[datas.tileDatas[i].type][0]); if(datas.tileDatas[i].obstacle != string.Empty) { obstacle = mapManager.ObstacleGet(inputDungeonName.text).Find(a => a.name.Equals(datas.tileDatas[i].obstacle)); tileSelect = tile; OnObstacleInstall(); tile.obstacle.transform.localEulerAngles = new Vector3(0, datas.tileDatas[i].angle, 0); } } } public void OnClose() { gameObject.SetActive(false); } }