Draw texture in editor with size less than a single unit

I am using Graphics.DrawTexture on a custom editor’s OnSceneGUI to draw a texture in the scene, and I can’t get a texture drawn in less than a unity unit. Basically I am getting the following result:

100720-2017-08-26-143651-309x161-scrot.png

The code I am using is this:

Graphics.DrawTexture(new Rect(2f, 0f, 0.5f, 0.5f), tex);

Graphics.DrawTexture(new Rect(new Vector2(0, 0), new Vector2(1f, 1f)), tex);

I reckon that the Rect is in screen coordinates, and it’s taking a unity unit as a “pixel”, but there is no way then to draw a texture programatically on the editor of less than 1 unit of size?

EDIT: finally, I took this (drawtexture with less than unit in scene) for impossible, and accepted @Xarbrough 's answer that somehow showed how to accomplish part of the final intended behaviour and has a lot of good information to lead people who might find this question.

I would say you’ve zoomed into a pixel grid. And since there’s no half pixel, both are equally sized. Since its pixel coordinates you need to calculate the pixel per unit radio yourself.

Just to make sure: Do you want to draw the texture in world space unity? Then you can do this: Create a primitive and place it in the scene. You should set the HideFlags to HideAndDontSave and destroy the object yourself, when you are done (you don’t want it to be saved with the scene, so you control its lifetime manually). I’ve did this approach for my custom tile-based map editor and it worked quite well for preview objects. This is also the way Unity handles a lot of internal things, like the scene view camera for example.

Indeed, I can confirm that Graphics.DrawTexture kind of works in the scene view, but only in increments of 1 unit. It probably isn’t intended for this use. Maybe this even ties in why the Canvas object has to be a thousand units wide, assuming 1 unit = 1 pixel, but I’m just guessing.

If you want your texture to work like a Gizmo or Icon, you can use BeginGUI or other methods like DrawIcon.

Here is some code for the approach with a preview GameObject:

using UnityEngine;
using UnityEditor;

public class MyScript : MonoBehaviour
{
	public Texture tex;
	public Rect rect;
}

[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor
{
	private GameObject previewObj;
	private Material material;

	private void OnEnable()
	{
		MyScript script = (MyScript)target;

		// Create a temporary object in the scene.
		previewObj = GameObject.CreatePrimitive(PrimitiveType.Plane);
		previewObj.hideFlags = HideFlags.HideAndDontSave;

		// Also copy the default material (we don't want to change that for all Unity default objects)
		material = new Material(previewObj.GetComponent<Renderer>().sharedMaterial)
		{
			hideFlags = HideFlags.HideAndDontSave,
			mainTexture = script.tex,
		};
		previewObj.GetComponent<Renderer>().sharedMaterial = material;

		// Rotate to the 2D plane, assuming the texture UV's are flipped in this case.
		previewObj.transform.Rotate(90f, 180f, 0f);
	}

	private void OnDisable()
	{
		if (previewObj != null)
			DestroyImmediate(previewObj);

		if (material != null)
			DestroyImmediate(material);
	}

	private void OnSceneGUI()
	{
		MyScript script = (MyScript)target;
		if (previewObj)
		{
			previewObj.transform.position = script.rect.position;
			previewObj.transform.localScale = new Vector3(script.rect.width, 1f, script.rect.height);
		}
	}
}

Result

It draws a Quad that can be rotate and scaled to show your texture. Different objects and transformations might apply, but the important parts are the memory management with HideFlags, as to not cause leaks in the editor.

100742-textureinscene.png