Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. [CustomEditor(typeof(IsoMap))]
  7. public class IsoEditor : Editor {
  8.  
  9.     public TerrainType terrain = TerrainType.grass;
  10.     public int height = 16;
  11.  
  12.     IsoTile activeTile;
  13.     IsoTile lastTile;
  14.  
  15.     bool dragging = false;
  16.     Color hilightColor = new Color(.8f, .8f, .8f, 1f);
  17.  
  18.     IsoMap node;
  19.  
  20.     private void OnEnable()
  21.     {
  22.         node = (IsoMap)target;
  23.     }
  24.  
  25.     void OnSceneGUI()
  26.     {
  27.         if (node == null) node = (IsoMap)target;
  28.  
  29.         if (Event.current.type == EventType.MouseMove || dragging)
  30.         {
  31.             RaycastHit2D hit = Physics2D.Raycast(Camera.current.ScreenToWorldPoint(HandleUtility.GUIPointToScreenPixelCoordinate(Event.current.mousePosition)), -Vector2.up);
  32.             if (hit)
  33.             {
  34.                 IsoTile tile = hit.transform.GetComponent<IsoTile>();
  35.                 if(tile != null && tile.transform.parent == node.transform)
  36.                 {
  37.                     activeTile = tile;
  38.                     if (!dragging) {
  39.                         tile.rend.color = hilightColor;
  40.                         tile.GroundRenderer.color = hilightColor;
  41.                         //tile.rend.sortingOrder = 1;
  42.                         //tile.GroundRenderer.sortingOrder = 1;
  43.                         if (lastTile != null && lastTile != activeTile)
  44.                         {
  45.                             lastTile.rend.color = Color.white;
  46.                             lastTile.GroundRenderer.color = Color.white;
  47.                             //lastTile.rend.sortingOrder = 0;
  48.                             //lastTile.GroundRenderer.sortingOrder = 0;
  49.                         }
  50.                         height = (int)tile.height;
  51.                     }
  52.                    
  53.                 }
  54.             }
  55.         }
  56.         if(Event.current.type == EventType.ScrollWheel)
  57.         {
  58.             height -= (int)Mathf.Sign(Event.current.delta.y)*4;
  59.             height = Mathf.Max(height,8);
  60.             Event.current.Use();
  61.  
  62.             if(activeTile != null)
  63.             {
  64.                 activeTile.height = height;
  65.             }
  66.         }
  67.         if(Event.current.type == EventType.MouseDown)
  68.         {
  69.             dragging = true;
  70.             Debug.Log("DRAG START");
  71.             PlaceTile();            
  72.             GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
  73.  
  74.         }
  75.         if (dragging)
  76.         {
  77.             if (activeTile != lastTile)
  78.             {
  79.                 PlaceTile();
  80.             }
  81.             if (Event.current.type == EventType.MouseUp)
  82.             {
  83.                 Debug.Log("DRAG STOP");
  84.                 dragging = false;
  85.             }
  86.         }
  87.         lastTile = activeTile;
  88.     }
  89.     public void PlaceTile()
  90.     {
  91.         if (node == null) node = (IsoMap)target;
  92.         IsoTile newTile = Instantiate(node.Tiles[(int)terrain], node.transform);
  93.         newTile.transform.position = activeTile.transform.position;
  94.         newTile.location = activeTile.location;
  95.  
  96.         if (!dragging) newTile.height = activeTile.height;
  97.         else newTile.height = height;
  98.  
  99.         node.grid[(int)newTile.location.x, (int)newTile.location.y] = newTile;
  100.         newTile.name = "Tile" + newTile.location.x.ToString() + "-" + newTile.location.y.ToString();
  101.         DestroyImmediate(activeTile.gameObject);
  102.         activeTile = newTile;
  103.     }
  104.     public override void OnInspectorGUI()
  105.     {
  106.         base.OnInspectorGUI();
  107.  
  108.         GUILayout.BeginHorizontal();
  109.         GUILayout.Label("TerrainType");
  110.         terrain = (TerrainType)EditorGUILayout.EnumPopup(terrain);
  111.         GUILayout.EndHorizontal();
  112.  
  113.         GUILayout.BeginHorizontal();
  114.         if (GUILayout.Button("Clear"))
  115.         {
  116.             node.Clear();
  117.         }
  118.  
  119.         if (GUILayout.Button("ClearHeights"))
  120.         {
  121.             foreach(IsoTile tile in node.grid)
  122.             {
  123.                 tile.ClearAllHeights();
  124.             }
  125.         }
  126.  
  127.         if(GUILayout.Button("Check"))
  128.         {
  129.             node.CheckTiles();
  130.         }
  131.         GUILayout.EndHorizontal();
  132.     }
  133.  
  134.     public enum TerrainType
  135.     {
  136.         empty,
  137.         grass,
  138.         dirt,
  139.         stone,
  140.         wood,
  141.         water
  142.     }
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement