- Home /
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?
Good one. Usually I just use Graphics.Draw (and its variants) with a plane mesh, but it's a bit of a hack.
Interesting. So I'm guessing Draw$$anonymous$$eshNow in LateUpdate on script marked to run in edit mode?
Would you be able to post any working code? $$anonymous$$y 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.Draw$$anonymous$$eshNow.html
Answer by DaveA · Mar 02, 2012 at 10:38 PM
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));
}
;) Ok a combination, but keep in $$anonymous$$d that Graphics.Draw$$anonymous$$eshNow is a pro only feature.
Answer by Bunny83 · Mar 02, 2012 at 10:07 AM
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.
" 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.
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.
Your answer
Follow this Question
Related Questions
How would I make a custom editor for texture swatches like Terrain editor? 2 Answers
Are textured/z-clipped gizmos possible? 0 Answers
Draw texture in editor with size less than a single unit 2 Answers
Blending edges of textures in the Terrain Editor 0 Answers
Unity editor and inspector header 0 Answers