I know it sound insane to create a “paint program” but its more of a 2D scene editor that I’m hoping to create but I need to be able to figure out some basic drawing primitives. My code is as follows. If you run the code, you’ll notice that the canvas updates every so often without consideration as to when and the coordinates are off. I can’t determine the position of the canvas.
Any idea of how I might solve these two problems?
Cheers,
Jeremy
using UnityEditor;
using UnityEngine;
using System.Collections;
public class TestEditor : EditorWindow {
[MenuItem("Editor/TestEditor")]
static void ShowWindow ()
{
EditorWindow.GetWindow<TestEditor> ();
}
Texture2D canvas;
bool isInitialized = false;
public TestEditor()
{
}
public void OnGUI()
{
this.wantsMouseMove = true;
if (!isInitialized)
{
canvas = new Texture2D(200,200, TextureFormat.ARGB32, false);
canvas.wrapMode = TextureWrapMode.Clamp;
for(int x = 0; x < canvas.width; x++)
for(int y = 0; y < canvas.height; y++)
canvas.SetPixel(x,y,Color.white);
canvas.Apply();
isInitialized = true;
}
GUILayout.Box (canvas);
Event current = Event.current;
switch(current.type)
{
case EventType.MouseMove:
int x = (int) current.mousePosition.x;
int y = (int) current.mousePosition.y;
Debug.Log("MouseMove:" + x + ":" + y);
if (x < canvas.width y < canvas.height)
{
canvas.SetPixel(x,y,Color.black);
canvas.Apply();
}
break;
case EventType.MouseDown:
Debug.Log("MouseDown:" + current.mousePosition.x + ":" + current.mousePosition.y);
break;
case EventType.MouseUp:
Debug.Log("MouseUp:" + current.mousePosition.x + ":" + current.mousePosition.y);
break;
}
}
}