Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- [CustomEditor(typeof(IsoMap))]
- public class IsoEditor : Editor {
- public TerrainType terrain = TerrainType.grass;
- public int height = 16;
- IsoTile activeTile;
- IsoTile lastTile;
- bool dragging = false;
- Color hilightColor = new Color(.8f, .8f, .8f, 1f);
- IsoMap node;
- private void OnEnable()
- {
- node = (IsoMap)target;
- }
- void OnSceneGUI()
- {
- if (node == null) node = (IsoMap)target;
- if (Event.current.type == EventType.MouseMove || dragging)
- {
- RaycastHit2D hit = Physics2D.Raycast(Camera.current.ScreenToWorldPoint(HandleUtility.GUIPointToScreenPixelCoordinate(Event.current.mousePosition)), -Vector2.up);
- if (hit)
- {
- IsoTile tile = hit.transform.GetComponent<IsoTile>();
- if(tile != null && tile.transform.parent == node.transform)
- {
- activeTile = tile;
- if (!dragging) {
- tile.rend.color = hilightColor;
- tile.GroundRenderer.color = hilightColor;
- //tile.rend.sortingOrder = 1;
- //tile.GroundRenderer.sortingOrder = 1;
- if (lastTile != null && lastTile != activeTile)
- {
- lastTile.rend.color = Color.white;
- lastTile.GroundRenderer.color = Color.white;
- //lastTile.rend.sortingOrder = 0;
- //lastTile.GroundRenderer.sortingOrder = 0;
- }
- height = (int)tile.height;
- }
- }
- }
- }
- if(Event.current.type == EventType.ScrollWheel)
- {
- height -= (int)Mathf.Sign(Event.current.delta.y)*4;
- height = Mathf.Max(height,8);
- Event.current.Use();
- if(activeTile != null)
- {
- activeTile.height = height;
- }
- }
- if(Event.current.type == EventType.MouseDown)
- {
- dragging = true;
- Debug.Log("DRAG START");
- PlaceTile();
- GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
- }
- if (dragging)
- {
- if (activeTile != lastTile)
- {
- PlaceTile();
- }
- if (Event.current.type == EventType.MouseUp)
- {
- Debug.Log("DRAG STOP");
- dragging = false;
- }
- }
- lastTile = activeTile;
- }
- public void PlaceTile()
- {
- if (node == null) node = (IsoMap)target;
- IsoTile newTile = Instantiate(node.Tiles[(int)terrain], node.transform);
- newTile.transform.position = activeTile.transform.position;
- newTile.location = activeTile.location;
- if (!dragging) newTile.height = activeTile.height;
- else newTile.height = height;
- node.grid[(int)newTile.location.x, (int)newTile.location.y] = newTile;
- newTile.name = "Tile" + newTile.location.x.ToString() + "-" + newTile.location.y.ToString();
- DestroyImmediate(activeTile.gameObject);
- activeTile = newTile;
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- GUILayout.BeginHorizontal();
- GUILayout.Label("TerrainType");
- terrain = (TerrainType)EditorGUILayout.EnumPopup(terrain);
- GUILayout.EndHorizontal();
- GUILayout.BeginHorizontal();
- if (GUILayout.Button("Clear"))
- {
- node.Clear();
- }
- if (GUILayout.Button("ClearHeights"))
- {
- foreach(IsoTile tile in node.grid)
- {
- tile.ClearAllHeights();
- }
- }
- if(GUILayout.Button("Check"))
- {
- node.CheckTiles();
- }
- GUILayout.EndHorizontal();
- }
- public enum TerrainType
- {
- empty,
- grass,
- dirt,
- stone,
- wood,
- water
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement