Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if UNITY_EDITOR
- using System;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- using System.Collections.Generic;
- using Extensions;
- public class GenMapRoomTextureFromFerr2DTerrain : EditorWindow
- {
- //public Ferr2D_Path Source;
- public Color MapColor = Color.blue;
- public Vector2 Scale = new Vector2(1, 1);
- public string OutputDirectory = string.Empty;
- [MenuItem("Project Tools/Generate Map Room Texture From Ferr2DTerrain")]
- public static void Init()
- {
- var window = EditorWindow.GetWindow(typeof(GenMapRoomTextureFromFerr2DTerrain));
- window.position = new Rect(Screen.width / 2, Screen.height / 2, 800, 400);
- window.ShowPopup();
- }
- void OnGUI()
- {
- GUILayout.Label("Render Image From Ferr2DTerrainSettings", EditorStyles.boldLabel);
- //Source = EditorGUILayout.ObjectField("Source Ferr2D_Path", Source, typeof(Ferr2D_Path), true) as Ferr2D_Path;
- OutputDirectory = EditorGUILayout.TextField("Output Directory", OutputDirectory);
- if (GUILayout.Button("Execute"))
- {
- if (OutputDirectory != string.Empty)
- {
- Execute();
- }
- else
- {
- Debug.LogError("Missing Target BoxCollider2d rectangle or Terrain");
- }
- }
- if (GUILayout.Button("Clear Selection"))
- {
- ClearVals();
- }
- if (GUILayout.Button("Close"))
- {
- this.Close();
- }
- }
- void ClearVals()
- {
- }
- void Execute()
- {
- //var result = Source.GetVertsRaw();
- //var objs =GameObject.FindGameObjectsWithTag("Enviroment");
- var objs = GameObject.FindObjectsOfType<Ferr2DT_PathTerrain>().ToList();
- var env = objs.Where(x=>x.gameObject.tag=="Environment").ToList();
- var colliderVertsList = new List<Vector2>();
- for (int i = 0; i < env.Count; i++)
- {
- var a = env[i].GetColliderVerts().MergeLists<Vector2>();
- colliderVertsList.AddRange(a);
- }
- //foreach(P v2d in objs)
- //{
- // merge.AddRange(v2d);
- //}
- //Debug.Log("Source.GetVertsRaw().MaxX() = "+Source.GetVertsRaw().MaxX() );
- //Debug.Log("Source.GetVertsRaw().MinX() = "+Source.GetVertsRaw().MinX() );
- Debug.Log("Source.GetVertsRaw().MaxY() = " + colliderVertsList.MaxY());
- Debug.Log("Source.GetVertsRaw().MinY() = " + colliderVertsList.MinY());
- var width = Mathf.RoundToInt(colliderVertsList.MaxX() - colliderVertsList.MinX());
- var height = Mathf.RoundToInt(colliderVertsList.MaxY() - colliderVertsList.MinY());
- var doors= GameObject.FindGameObjectsWithTag("Door");
- var offsetZero = colliderVertsList.MinXY();
- var offsetMax = colliderVertsList.MaxXY();
- //var width = 200;
- //var height = 200;
- Texture2D render = new Texture2D(width, height);
- var offsetPoints = colliderVertsList.Apply(offsetZero);
- render.FloodFillBorder(0, 0, Color.black, Color.yellow);
- render.DrawLines(offsetPoints, Color.red);
- int doorRadius = 2;
- Color doorColor = Color.blue;
- for (int i = 0; i < doors.Count(); i++)
- {
- //var pos = new Vector2(doors[i].transform.position.x+offsetZero.x, doors[i].transform.position.y+offsetZero.y);
- var pos = new Vector2(doors[i].transform.position.x , doors[i].transform.position.y );
- render.DrawCircle(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), doorRadius, doorColor);
- Debug.Log("Door pos x:" + pos.x + "|| y:"+pos.y);
- }
- //render.SetPixels(0, 0, width, height, colors);
- File.WriteAllBytes(OutputDirectory + "map_" + Application.loadedLevelName + ".png", render.EncodeToPNG());
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment