- Home /
how to create handles like editor gizmos in runtime?
i would like to create gizmos like movement gizmos of the editor at runtime. i want to know how can i implement one for the runtime? i would be more than happy if the real implementer of editor handles answer me and tell me what to do.
@Ashkan just leave the question open til the deadline.. you might have another cool answer.. this is a good one :) Let's see..
I'm reluctantly forced to agree. :) Just don't let it expire. Plus, I can't wait to see Lipis's answer, I'm sure it's almost done. :)
@Cyclops.. hehe..! actually I wasn't about to implementing it.. but since I got some ideas.. let's see.. :)
doesn't work. it says Assets/Gizmo/Gizmo.js(23,18): BCE0017: The best overload for the method 'GizmoHandle.setType(GizmoType)' is not compatible with the argument list '(UnityEditor.GizmoType)'. Any solutions?
Answer by Lipis · Mar 17, 2010 at 03:06 AM
Here is my Gizmo solution in action.
This is a full working gizmos and you are able to change Position
, Rotation
and Scale
of the any object during run-time.
Feel free to download the source code and if you have any question drop a line in the comments.
@Ashkan Just let me know if thats what you were looking for?
@Lipis. Dang - way better than $$anonymous$$e. I had to open my mouth... :)
@Cyclops it's easier than you think.. the whole thing is not taking more than an hour from scratch.. :)
@Lipis, maybe easy for you, but I always seem to do things the hard way... :)
Answer by Cyclops · Mar 14, 2010 at 09:13 PM
Well, I have written a basic implementation of a faked run-time version of a three-axis Location Gizmo that allows you to - click on an axis to move an object along the chosen axis (try saying that three times fast...)
Turned out to be a lot harder than I expected when I started - I thought, "okay, first I need to draw three orthogonal lines", and things went down-hill from there. Who knew drawing a line was that hard? :)
What I wound up doing was creating a Procedural Mesh (and there was another learning curve) of an arrow, in triplicate, attach a red/blue/green material to it, and rotate each as needed. Then attach a mouse-down script to each axis, that moves both the Gizmo object and its Parent object as long as the mouse is held down. It's not a true mouse-drag, which would have been a lot more work, and this was plenty as it is.
All of this is done procedurally - it requires three scripts, one of which, s_GizmoCreate.cs, you drag/drop to a Hierarchy object. When the game starts, the script creates the Gizmo onto the object. You could also build it into a GUI, such that the script is attached to an object you clicked on - but again, that's another metric ton of work. :)
As I said, this is a basic implementation of a single Gizmo type (specifically the 3-axis Move Gizmo). But it demonstrates all the programming techniques that you would use for others, such as the Rotation Gizmo.
I've put a complete sample package, with the three scripts needed, on the Unify Wiki at:
i have some questions about meshes and FBX files man. can you email me at ashkan_gc(at)yahoo(dot)com?
Glad you liked it. Enough to check the Bounty box/checkbox? :) While I realize it's crude - it was only intended as proof-of-concept and to demo the techniques. If you want a full, professional implementation, then we're talking hourly rates. :)
Answer by HiddenMonk · Oct 20, 2016 at 04:00 AM
I have taken Murchos answer approach of using the GL class and made a decent replica of the unity editor transform gizmo. Its not just for show either, you can move, rotate, and scale at runtime.
Here is a video demonstrating it - https://www.youtube.com/watch?v=IUQqhS8tsNo
Here is the source code - https://github.com/HiddenMonk/Unity3DRuntimeTransformGizmo
Total beginner here, so please don't hate the dumb question. I know program$$anonymous$$g but my Unity is really bad (i just learned what gizmos are): How would i do this with some custom (package-provided) gizmos? I have a spline generator that i'd like to turn into a road generator for a game, which means I need to access the control points and anchors of the spline in gamemode.
Thank you so much!
Answer by Murcho · Mar 14, 2010 at 01:05 AM
Here's a start, by using the GL.LINES mode with immediate drawing during the OnPostRender() function, you are able to draw the handles. I've got as far as the actual lines drawing, however not the ability to select them. Something tells me that will require a lot more code.
Place this script onto your camera, then drag the target for the handle into the target slot. This also handles local and global direction handles with a drop down on the script, and you can change the length of the handles with the handleLength variable.
using UnityEngine; using System.Collections;
public class LineDrawing : MonoBehaviour { static Material lineMaterial; public enum TransformSpace { Global, Local }
public Transform target;
public TransformSpace space = TransformSpace.Global;
public float handleLength = 5.0f;
static void CreateLineMaterial()
{
if (!lineMaterial)
{
lineMaterial = new Material("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
void OnPostRender()
{
Vector3 xAxisEnd;
Vector3 yAxisEnd;
Vector3 zAxisEnd;
if (space == TransformSpace.Global)
{
xAxisEnd = target.transform.position + Vector3.right * handleLength;
yAxisEnd = target.transform.position + Vector3.up * handleLength;
zAxisEnd = target.transform.position + Vector3.forward * handleLength;
}
else
{
xAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.right * handleLength);
yAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.up * handleLength);
zAxisEnd = target.transform.position + target.transform.TransformDirection(Vector3.forward * handleLength);
}
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
// X line
GL.Color(new Color(1, 0, 0, 0.5f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(xAxisEnd.x, xAxisEnd.y, xAxisEnd.z);
// Y line
GL.Color(new Color(0, 1, 0, 0.5f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(yAxisEnd.x, yAxisEnd.y, yAxisEnd.z);
// Z line
GL.Color(new Color(0, 0, 1, 0.5f));
GL.Vertex3(target.transform.position.x, target.transform.position.y, target.transform.position.z);
GL.Vertex3(zAxisEnd.x, zAxisEnd.y, zAxisEnd.z);
GL.End();
}
}
From here you should get a good idea on how to draw lines in the same fashion handles are drawn, but in runtime. If you want to try and draw the cones on the end, use GL.Begin(GL.TRIANGLES) and send three vertex3 calls through at a time. You may need to do some matrix maths to get them looking right however.
You are correct, sorry I tend to not notice those notices (Pro user here).
I have taken your approach and finalized it. Currently its on the second page of these answers, so I am leaving a comment here so people know. Here is a demo video - https://www.youtube.com/watch?v=IUQqhS8tsNo Source code - https://github.com/Hidden$$anonymous$$onk/Unity3DRuntimeTransformGizmo
Answer by Stelimar · Mar 06, 2010 at 10:05 PM
Short Answer: Unfortunately, you can't.
Long Answer: The Handles class which is used to create movement/scale/rotation handles is an Editor class, which means you can only use it within editor scripts, not at runtime. There is no runtime equivalent, and as far as I know, there is no way to use editor classes at runtime. If you really HAVE to have this functionality, you would have to write your own implementation.
i know that man. i want to implement one myself. i told this in my question.
Your answer
![](https://koobas.hobune.stream/wayback/20220612100252im_/https://answers.unity.com/themes/thub/images/avi.jpg)