Jump to content
  • Advertisement
Redguy34

How Do I Make A Gizmo In My In Game Level Editor[Unity3D]

Recommended Posts

What I Want To Do Is Add A Thing Like When You Move Something In The Unity Editor. I Want To Add This My Level Editor So Its Easier To Move Objects Also Rotation And Scale But Those Are Less Important To Me For Now

Share this post


Link to post
Share on other sites
Advertisement

If you just want to draw custom Gizmos without responding to the mouse you can do it by adding the draw calls you want to OnDrawGizmos or OnDrawGizmosSelected. This is a decent quick intro to them https://blog.theknightsofunity.com/custom-gizmos-ondrawgizmos/

When I first dealt with needing to drag custom gizmos around in the editor I found it was more complicated. This is what I did back in Unity 5.1. I used a custom editor for the component I wanted to add controls to. An empty one looks like this

[CustomEditor(typeof(WaypointPath))]
public class PathEditor : Editor
{
    private void OnSceneGUI ()
    {
    	// code for drawing and handling mouse events will go here
    }
}

In my case I needed custom handles (the interactive parts of a gizmo) to drag control points for defining a path. Stripped down to just the code needed to drag around a single handle on a single axis you end up with something like this. This isn't what I'm actually using since the real code deals with potentially dozens of these draggable handles which would make it harder to follow what's going on. I've not actually compiled or run this code.

Hopefully this helps in finding the information needed to create your own editor UI gizmos.

void OnSceneGUI()
{
    // generate an ID for the clickable part of your gizmo
    // in my case I used -10 then decremented for each additional handle
    // IIRC positive numbers cause problems
    int controlId = -10;
    float distFromMouse = HandleUtility.DistanceToRectangle(worldPositionOfYourHandle, Quaternion.identity, 0.25f);
    HandleUtility.AddControl(id, distFromMouse);
    
    // draw the handle, this is just an example of how it can be done
    Handles.CubeHandleCap(id, worldPositionOfYourHandle, Quaternion.identity, 0.25f);

    // handle events
    if(Event.current.type == EventType.MouseDown)
    {
        if(!Event.current.control && HandleUtilitiy.nearestControl == controlId)
        {
            // if needed, store the starting point of the mouse drag in a member variable
            dragStart = Event.current.mousePosition;
          
            // if you don't do this, you could click multiple objects at once
            Event.current.Use();
        }
    }
    else if(Event.current.type == EventType.MouseDrag)
    {
        if(!Event.current.control && HandleUtilitiy.nearestControl == controlId)
        {
            // do whatever you'd like with the drag
            // example of dragging the handle along the X axis only
            float newX = HandleUtility.CalcLineTranslation(dragStart, Event.current.mousePosition, worldPositionOfYourHandle, Vector3.right);
            Event.current.Use();
          
            // (change worldPositionOfYourHandle here)
        }
    }
}
        

Though if you just want to move an object around I'm not sure why you wouldn't want to use the built in gizmos.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!