Jump to content
Sign in to follow this  
Matt1988

[Rerelease!] Ruler - Complete Rewrite!

Recommended Posts

Ruler

Tired of counting grid squares to find out how big your pb_Objects are? Well never fear! Ruler has you covered.

Current Version: 2.0

Installation:

Put the Code, located at the bottom of the post, into a C# script called Ruler.cs inside of the Editor folder (You can make an Editor folder anywhere or you can use an existing Editor folder inside the ProBuilder project directory.)

Usage Instructions:

Once you have put the Ruler.cs in an Editor folder, navigate to "Window > ProBuilder > Editor" and click "Ruler Toggle". This will toggle Ruler to On or Off. You may also use the shortcut instead: "Ctrl+Shift+R" (This can be changed in the script in the event of conflicts.)

When Ruler is on, if you select exactly two vertices (in "Selection Mode Vertex") on a pb_Object, the distance between the selected verts will be displayed in between the selected verts.

Without further ado - here is the code.

/**
*  @ Matt1988
*  This extension was built by @Matt1988
* 
* Use: Use the MenuItem "Ruler Toggle" by navigating to "Window > ProBuilder > Editor > Ruler Toggle"
* This will toggle Ruler on or off.
* 
* When Ruler is on, if you select exactly two vertices on a pb_Object, the distance
* between the two vertices will display halfway between the two vertices.
* 
* Shortcut: CTRL+SHIFT+R
*/
using UnityEngine;
using UnityEditor;
using System.Collections;

public class Ruler : Editor {

public static bool on = false;

[MenuItem("Window/ProBuilder/Editor/Toggle Ruler _%#r")]
static void init() {
	//pb_Editor_Utility.ShowNotification("Ruler");
	if(on) {
		pb_Editor_Utility.ShowNotification("Ruler Off");
		SceneView.onSceneGUIDelegate -= OnSceneGUI;
                       on = false;
	}
	else {
		pb_Editor_Utility.ShowNotification("Ruler On");
		SceneView.onSceneGUIDelegate -= OnSceneGUI; //A failsafe to ensure that we don't add our method twice.
		SceneView.onSceneGUIDelegate += OnSceneGUI;
                       on = true;

	}

}

static pb_Object GetSelection()
{
	if(Selection.activeTransform != null) {
		return Selection.activeTransform.GetComponent();
	}
	return null;
}

public static void OnSceneGUI(SceneView view) {
	pb_Object pbo = GetSelection();
	if(pbo != null) {
		if(pbo.selected_triangles.Length == 2) {
			Vector3[] v = pbo.VerticesInWorldSpace(pbo.selected_triangles);
			float distance = Vector3.Distance(v[0], v[1]);

			Vector3 center = Vector3.zero;
			center+= v[0];
			center+= v[1];
			center /= 2f;
			GUIStyle style = new GUIStyle();
			style.normal.textColor = Color.red; //Set our font color to RED. Change this to suit your preference.
			Handles.Label(center, distance + "M", style);
		}
	}
}
}

To-Do List:

add conversions for feet, cm, inches, yards.

Share this post


Link to post
Share on other sites

Thanks Matt1988, this is great!

Funny how that works- ProBuilder was created to fix a gap in Unity, and now your tool was created to fix a gap in ProBuilder. I know Karl is especially excited for this sort of thing, and I'm really liking the idea as well. ProBuilder is getting more and more complex, and I'm sure various users will have very different uses for the tool, so the ability to easily "mod" or "extend" the base toolset really makes sense.

Heck, I'm going to make a forum just for this right now! :D

Share this post


Link to post
Share on other sites

Awesome :)

I agree completely. With so many products that are "mod-able" now a days, a lot of people look over anything that doesn't offer a mod api in the description.

The current version of the ruler here doesn't tap into any of the functionality of ProGrids directly yet, it just uses GameObjects as the moveable ends of the ruler so that it snaps to the grid. When I'm able to ditch the game objects and easily tap into ProGrids to snap PositionHandles, it will get rid of some Hierarchy clutter and add some more options later down the road :).

edit: oh and another advantage of a Mod style API: others may provide fixes for you decreasing the amount of attention that you have to spend filling gaps :).

Share this post


Link to post
Share on other sites

A known bug in version 1.0:

When you start up your project you will get some errors about transforms missing etc. Simply closing the window and re-opening it should fix the problem.

This is fixed in the current version that I'm working on.

Share this post


Link to post
Share on other sites

A rewrite of Ruler is up!

The rewrite is a much more elegant solution than the original. Hope some of you find it useful.

Previous versions are no longer available because of an emergency format.

Share this post


Link to post
Share on other sites

Thanks for the script. I made a few fixes because it gave me errors.

/**
 *  @ Matt1988
 *  This extension was built by @Matt1988
 *  [modified by Rich]
 * 
 * Use: Use the MenuItem "Ruler Toggle" by navigating to "Window > ProBuilder > Editor > Ruler Toggle"
 * This will toggle Ruler on or off.
 * 
 * When Ruler is on, if you select exactly two vertices on a pb_Object, the distance
 * between the two vertices will display halfway between the two vertices.
 * 
 */
using UnityEngine;
using UnityEditor;
using ProBuilder2.Common;
using ProBuilder2.EditorCommon;

public class RulerForProbuilder : Editor
{

    public static bool on = false;

    [MenuItem("Window/ProBuilder/Editor/Toggle Ruler")]
    static void init()
    {
        //pb_Editor_Utility.ShowNotification("Ruler");
        if (on)
        {
            pb_Editor_Utility.ShowNotification("Ruler Off");
            SceneView.onSceneGUIDelegate -= OnSceneGUI;
            on = false;
        }
        else
        {
            pb_Editor_Utility.ShowNotification("Ruler On");
            SceneView.onSceneGUIDelegate -= OnSceneGUI; //A failsafe to ensure that we don't add our method twice.
            SceneView.onSceneGUIDelegate += OnSceneGUI;
            on = true;

        }

    }

    static pb_Object GetSelection()
    {
        if (Selection.activeTransform != null)
        {
            return Selection.activeTransform.GetComponent<pb_Object>();
        }
        return null;
    }

    public static void OnSceneGUI(SceneView view)
    {
        pb_Object pbo = GetSelection();
        if (pbo != null)
        {
            if (pbo.SelectedTriangles.Length == 2)
            {
                Vector3[] v = pbo.VerticesInWorldSpace(pbo.SelectedTriangles);
                float distance = Vector3.Distance(v[0], v[1]);

                Vector3 center = Vector3.zero;
                center += v[0];
                center += v[1];
                center /= 2f;
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.white; //Set our font color to RED. Change this to suit your preference.
                style.fontStyle = FontStyle.Bold;
                Handles.Label(center, distance + " m", style);
            }
        }
    }
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×