x


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.

more ▼

asked Mar 06 '10 at 08:30 PM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

@Ashkan just leave the question open til the deadline.. you might have another cool answer.. this is a good one :) Let's see..

Mar 15 '10 at 08:19 PM Lipis

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. :)

Mar 16 '10 at 02:46 PM Cyclops

@Cyclops.. hehe..! actually I wasn't about to implementing it.. but since I got some ideas.. let's see.. :)

Mar 16 '10 at 08:41 PM Lipis
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Here is my Gizmo solution in action.

Gizmos 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.

more ▼

answered Mar 17 '10 at 03:06 AM

Lipis gravatar image

Lipis
2.3k 10 23 48

@Ashkan Just let me know if thats what you were looking for?

Mar 17 '10 at 03:07 AM Lipis

@Lipis. Dang - way better than mine. I had to open my mouth... :)

Mar 17 '10 at 01:38 PM Cyclops

@Cyclops it's easier than you think.. the whole thing is not taking more than an hour from scratch.. :)

Mar 17 '10 at 05:37 PM Lipis

@Lipis, maybe easy for you, but I always seem to do things the hard way... :)

Mar 17 '10 at 05:48 PM Cyclops

If it's complicated it's wrong...!! :)

Mar 17 '10 at 06:01 PM Lipis
(comments are locked)
10|3000 characters needed characters left

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:

Fake Gizmo Demo

more ▼

answered Mar 14 '10 at 09:13 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

thank you for the great work

Mar 15 '10 at 06:34 AM Ashkan_gc

i have some questions about meshes and FBX files man. can you email me at ashkan_gc(at)yahoo(dot)com?

Mar 15 '10 at 07:17 AM Ashkan_gc

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. :)

Mar 15 '10 at 02:11 PM Cyclops

@Cyclops I didn't manage to see your solution in action..

Mar 17 '10 at 10:23 AM Lipis
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 14 '10 at 01:05 AM

Murcho gravatar image

Murcho
2.7k 12 23 53

Isn't GL.Lines, a Pro-only feature?

Mar 17 '10 at 01:39 PM Cyclops

You are correct, sorry I tend to not notice those notices (Pro user here).

Mar 18 '10 at 03:21 AM Murcho
(comments are locked)
10|3000 characters needed characters left

@ 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.

more ▼

answered Dec 21 '11 at 06:42 PM

michael bricout gravatar image

michael bricout
3 1 1 1

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 06 '10 at 10:05 PM

Stelimar gravatar image

Stelimar
3k 14 16 50

i know that man. i want to implement one myself. i told this in my question.

Mar 07 '10 at 06:28 AM Ashkan_gc

i wrote in my question that i want to implement one.

Mar 15 '10 at 06:37 AM Ashkan_gc
(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:

x1730
x386
x70
x8

asked: Mar 06 '10 at 08:30 PM

Seen: 9221 times

Last Updated: Jan 10 at 08:30 AM