musicm122

GenMapRoomTextureFromFerr2DTerrain

Sep 21st, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using System.Collections;
  7. using UnityEditor;
  8. using System.Collections.Generic;
  9. using Extensions;
  10.  
  11. public class GenMapRoomTextureFromFerr2DTerrain : EditorWindow
  12. {
  13.   //public Ferr2D_Path Source;
  14.   public Color MapColor = Color.blue;
  15.   public Vector2 Scale = new Vector2(1, 1);
  16.   public string OutputDirectory = string.Empty;
  17.  
  18.  
  19.   [MenuItem("Project Tools/Generate Map Room Texture From Ferr2DTerrain")]
  20.   public static void Init()
  21.   {
  22.     var window = EditorWindow.GetWindow(typeof(GenMapRoomTextureFromFerr2DTerrain));
  23.     window.position = new Rect(Screen.width / 2, Screen.height / 2, 800, 400);
  24.     window.ShowPopup();
  25.   }
  26.  
  27.   void OnGUI()
  28.   {
  29.     GUILayout.Label("Render Image From Ferr2DTerrainSettings", EditorStyles.boldLabel);
  30.  
  31.     //Source = EditorGUILayout.ObjectField("Source Ferr2D_Path", Source, typeof(Ferr2D_Path), true) as Ferr2D_Path;
  32.    
  33.     OutputDirectory = EditorGUILayout.TextField("Output Directory", OutputDirectory);
  34.  
  35.     if (GUILayout.Button("Execute"))
  36.     {
  37.       if (OutputDirectory != string.Empty)
  38.       {
  39.         Execute();
  40.       }
  41.       else
  42.       {
  43.         Debug.LogError("Missing Target BoxCollider2d rectangle or Terrain");
  44.       }
  45.     }
  46.  
  47.     if (GUILayout.Button("Clear Selection"))
  48.     {
  49.       ClearVals();
  50.     }
  51.     if (GUILayout.Button("Close"))
  52.     {
  53.       this.Close();
  54.     }
  55.   }
  56.  
  57.   void ClearVals()
  58.   {
  59.   }
  60.  
  61.   void Execute()
  62.   {
  63.     //var result = Source.GetVertsRaw();
  64.     //var objs =GameObject.FindGameObjectsWithTag("Enviroment");
  65.    
  66.  
  67.     var objs = GameObject.FindObjectsOfType<Ferr2DT_PathTerrain>().ToList();
  68.     var env = objs.Where(x=>x.gameObject.tag=="Environment").ToList();
  69.     var colliderVertsList = new List<Vector2>();
  70.  
  71.     for (int i = 0; i < env.Count; i++)
  72.     {
  73.       var a = env[i].GetColliderVerts().MergeLists<Vector2>();
  74.       colliderVertsList.AddRange(a);
  75.     }
  76.  
  77.     //foreach(P v2d in objs)
  78.     //{
  79.     //  merge.AddRange(v2d);
  80.     //}
  81.     //Debug.Log("Source.GetVertsRaw().MaxX() = "+Source.GetVertsRaw().MaxX() );
  82.     //Debug.Log("Source.GetVertsRaw().MinX() = "+Source.GetVertsRaw().MinX() );
  83.  
  84.     Debug.Log("Source.GetVertsRaw().MaxY() = " + colliderVertsList.MaxY());
  85.     Debug.Log("Source.GetVertsRaw().MinY() = " + colliderVertsList.MinY());
  86.  
  87.     var width = Mathf.RoundToInt(colliderVertsList.MaxX() - colliderVertsList.MinX());
  88.     var height = Mathf.RoundToInt(colliderVertsList.MaxY() - colliderVertsList.MinY());
  89.    
  90.     var doors= GameObject.FindGameObjectsWithTag("Door");
  91.     var offsetZero = colliderVertsList.MinXY();
  92.     var offsetMax = colliderVertsList.MaxXY();
  93.  
  94.     //var width = 200;
  95.     //var height = 200;
  96.    
  97.     Texture2D render = new Texture2D(width, height);
  98.     var offsetPoints = colliderVertsList.Apply(offsetZero);
  99.    
  100.     render.FloodFillBorder(0, 0, Color.black, Color.yellow);
  101.     render.DrawLines(offsetPoints, Color.red);
  102.     int doorRadius = 2;
  103.     Color doorColor = Color.blue;
  104.     for (int i = 0; i < doors.Count(); i++)
  105.     {
  106.       //var pos = new Vector2(doors[i].transform.position.x+offsetZero.x, doors[i].transform.position.y+offsetZero.y);
  107.       var pos = new Vector2(doors[i].transform.position.x , doors[i].transform.position.y );
  108.       render.DrawCircle(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), doorRadius, doorColor);
  109.       Debug.Log("Door pos x:" + pos.x + "|| y:"+pos.y);
  110.     }
  111.  
  112.     //render.SetPixels(0, 0, width, height, colors);
  113.     File.WriteAllBytes(OutputDirectory + "map_" + Application.loadedLevelName + ".png", render.EncodeToPNG());
  114.   }
  115. }
  116.  
  117.  
  118. #endif
Advertisement
Add Comment
Please, Sign In to add comment