Created
April 6, 2014 01:48
-
-
Save jquave/10000461 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using UnityEditor; | |
| public class LevelEditorWindow : EditorWindow { | |
| Texture levelData; | |
| int previewSize = 500; | |
| int topMargin = 50; | |
| Color activeColor = Color.white; | |
| // Add menu named "My Window" to the Window menu | |
| [MenuItem ("JQ Software/Level Editor")] | |
| static void Init () { | |
| // Get existing open window or if none, make a new one: | |
| LevelEditorWindow window = (LevelEditorWindow)EditorWindow.GetWindow (typeof (LevelEditorWindow)); | |
| window.wantsMouseMove = true; | |
| } | |
| void ClickedFromPos(Vector2 mousePos) { | |
| bool isWithinImageBounds = (mousePos.x>0 && mousePos.x<previewSize & mousePos.y>topMargin && mousePos.y<(previewSize+topMargin)); | |
| if(isWithinImageBounds) { | |
| if(levelData==null) return; | |
| int width = levelData.width; | |
| int height = levelData.height; | |
| float x = Mathf.FloorToInt(((mousePos.x/previewSize)*width)); | |
| float y = height - Mathf.FloorToInt((((mousePos.y - topMargin)/previewSize)*height))-1; | |
| //Debug.Log(mousePos.y); | |
| Vector2 pixelClicked = new Vector2( x, y ); | |
| Texture2D pd = (Texture2D)levelData; | |
| pd.SetPixel((int)pixelClicked.x, (int)pixelClicked.y, activeColor); | |
| pd.Apply(); | |
| //levelData = (Texture)pd; | |
| Repaint (); | |
| } | |
| } | |
| void Clear() { | |
| Texture2D pd = (Texture2D)levelData; | |
| //pd.SetPixel((int)pixelClicked.x, (int)pixelClicked.y, Color.magenta); | |
| int width = levelData.width; | |
| int height = levelData.height; | |
| for(int x=0;x<width;x++) { | |
| for(int y=0;y<height;y++) { | |
| pd.SetPixel((int)x, (int)y, Color.white); | |
| } | |
| } | |
| pd.Apply(); | |
| Repaint (); | |
| } | |
| void OnGUI () { | |
| levelData = (Texture) EditorGUILayout.ObjectField(levelData, typeof(Texture), false, null); | |
| if(levelData!=null) { | |
| EditorGUI.DrawPreviewTexture(new Rect(0,topMargin,500,500), levelData); | |
| } | |
| float buttonMargin = 15; | |
| int x = 0; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Erase")) { | |
| activeColor = Color.white; | |
| } | |
| x+=100; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Floor")) { | |
| activeColor = Color.cyan; | |
| } | |
| x+=100; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Wall")) { | |
| activeColor = Color.black; | |
| } | |
| x+=100; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Enemy Spawner")) { | |
| activeColor = Color.red; | |
| } | |
| x+=100; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Player")) { | |
| activeColor = Color.green; | |
| } | |
| x+=100; | |
| if(GUI.Button(new Rect(x,previewSize + topMargin + buttonMargin,100,100), "Clear")) { | |
| Clear(); | |
| } | |
| Event e = Event.current; | |
| /* | |
| if (e.type == EventType.MouseUp) { | |
| Vector2 mousePos = e.mousePosition; | |
| ClickedFromPos(mousePos); | |
| }*/ | |
| // Debug.Log(e); | |
| if (e.type == EventType.MouseDrag) { | |
| //if(e.clickCount>0) { | |
| ClickedFromPos(e.mousePosition); | |
| //} | |
| } | |
| // Repaint the window as wantsMouseMove doesnt trigger a repaint automatically | |
| // if (e.type == EventType.MouseMove) | |
| // Repaint (); | |
| // | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment