x


UnityEditor - Drawing on a Texture

hello, do you know how I can draw with my mouse on textures? or other drawing tricks? I speak in from the side of the editor not in the gameScene. Below a sample code:

using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow
{
   string myString = "Bella zi";
   bool groupEnabled;
   bool myBool = true;
   float myFloat = 1.23f;
   Texture2D texty = new Texture2D(150,150);

   // Add menu named "My Window" to the Window menu
   [MenuItem ("Window/My Window")]
   static void Init ()
   {
      // Get existing open window or if none, make a new one:
      MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
      window.Show ();
   }

   void OnGUI ()
   {
      GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
      EditorGUILayout.TextArea(myString, GUILayout.Height(200));

      //little box with texture to draw <-------
      GUILayout.Box(texty,GUILayout.Width(150),GUILayout.Height(150));      

      groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
      myBool = EditorGUILayout.Toggle ("Toggle", myBool);
      myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
      EditorGUILayout.EndToggleGroup ();
   }
}

thanks!!

more ▼

asked Mar 22 '10 at 08:50 AM

Alberto gravatar image

Alberto
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can use Texture2D.SetPixel(...) to set the color of an individual pixel on a texture. If your texture is on the GUI, it should be fairly easy to compute its position using its rectangle, which you can then offset from the Mouse position in order to get the pixel coordinates of the pixel that the mouse is over.

If this texture is in 3D space, however, you can check out my other answer here:

http://answers.unity3d.com/questions/10683/detecting-mouseclick-on-pixels-in-sprite-using-sm2-pixel-collisions

which details how you can Raycast to get which pixel the mouse is pointing at.

more ▼

answered Jun 08 '10 at 10:01 PM

qJake gravatar image

qJake
11.6k 43 78 161

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1674
x354
x348
x65

asked: Mar 22 '10 at 08:50 AM

Seen: 5393 times

Last Updated: Mar 22 '10 at 08:50 AM