x


How to draw a textured plane in Editor?

I want to make a custom editor which draws a textured plane in 3-space (not like a billboard icon). Think of it as a reference object. Is this possible, and which API's would be involved?

more ▼

asked Mar 02 '12 at 09:05 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

Good one. Usually I just use Graphics.Draw (and its variants) with a plane mesh, but it's a bit of a hack.

Mar 02 '12 at 09:08 AM syclamoth

Interesting. So I'm guessing DrawMeshNow in LateUpdate on script marked to run in edit mode?

Mar 02 '12 at 09:15 AM DaveA

Would you be able to post any working code? My attempts at this produce nothing. No Update or OnGUI seems to draw anything, using this as a basis (and setting mat and mesh in inspector): http://unity3d.com/support/documentation/ScriptReference/Graphics.DrawMeshNow.html

Mar 02 '12 at 10:23 PM DaveA

OnDrawGizmos seems to be working....

Mar 02 '12 at 10:28 PM DaveA
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

This is what's working for me. Props to Bunny83 and syclamoth!

@script ExecuteInEditMode()
var aMesh : Mesh;
var mat : Material;

function Start () {
    hideFlags = HideFlags.HideAndDontSave;
}

function OnDrawGizmos() {
    // SetPass to 0 if the material doesnt have a texture.
    mat.SetPass(1);
    Graphics.DrawMeshNow(aMesh, Matrix4x4.Scale(transform.localScale));
}
more ▼

answered Mar 02 '12 at 10:38 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

;) Ok a combination, but keep in mind that Graphics.DrawMeshNow is a pro only feature.

Mar 03 '12 at 12:32 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

You can use the usual procedure. Just use GameObjects and a camera. Make sure you use a seperate unused layer so it's seperated from the actual scene.

The next important thing is that you set the hideFlags of all objects you create for your editor to at least "DontSave" or "HideAndDontSave", otherwise the object get saved into the current scene.

If you want to use an EditorWindow, you can use Camera.Render in OnGUI() to render the camera into the editor window. Note: make sure you only call it in the repaint step.

more ▼

answered Mar 02 '12 at 10:07 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

" and a camera" - not sure what you mean. Can I access the editor's 3D view camera(s)? What I'm shooting for is like a gizmo, but that renders an image, presumably on a planar mesh (any mesh would be interesting too). It would be seen and perhaps manipulated in the Editor's 3D views, but not seen at runtime.

Mar 02 '12 at 08:32 PM DaveA

Well, the editor uses GameObjects and Cameras exactly like you do at runtime. The editors camera has hideflags set so it won't show up in the scenes hierarchy panel and it's not saved along with the scene.

I thought you talk about a custom editorwindow, but you can also draw something in the "normal" sceneview. If you have ILSpy, take a look into the UnityEditor.dll assembly. The SceneView class isn't documented, but it can be accessed. With SceneView.lastActiveSceneView.camera you can access the camera used by the scene view.

There's also the function Resources.FindObjectsOfTypeAll which can also "find" those internal objects.

You can do exactly the same. You can create a temporary gameobject with a mesh (plane, cube, ...) and display it right along with your usual scene objects. The hideflags also have a NotEditable flag so the object can't be edited. There's even an example with a plane mesh ^^

But be careful. You have to take care of destroing the objects when you don't need them anymore. They can cause memory leaks in the editor if you just forget about them. Some are caught by the editor, some are not.

Final note: Never use Destroy() in the editor. You always have to use DestroyImmediate() in the editor.

Mar 02 '12 at 10:49 PM Bunny83
(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:

x2210
x1677
x199
x69

asked: Mar 02 '12 at 09:05 AM

Seen: 1514 times

Last Updated: Mar 03 '12 at 12:32 PM