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.
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.
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 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 - Unity3d Runtime Transform Gizmo - YouTube
Here is the source code - GitHub - HiddenMonk/Unity3DRuntimeTransformGizmo: A runtime transform gizmo similar to unitys editor so you can translate (move, rotate, scale) objects at runtime.
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.
@ Murcho I really like your solution by using the GL.LINES.
I have a scene where I have 3 objects and I would like to display theires axes by GL.LINES.
The probleme is that the GL.LINES system is on my camera, where I can add one Target.
How can I use this system with several Target ?
Thanks for your help.
Old thread but we created a product for the problem, you can find it here:
http://forum.unity3d.com/threads/in-game-gizmo-transform.299275/
it uses Unity GL renders. It costs $14 but if you really don’t have the money tell us your situation and we can work something out. The only reason for the price is that I am a student trying to make money for college.
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.