New helpful utilities list

Published 4/4/2020 by createdbyx in News
Tags:

Added a new page containing a list of helpfull utilities.


/// <summary>
/// Determines whether an enum has been marked Obsolete.
/// </summary>
/// <param name="value">The enum value to check against.</param>
/// <returns>
///   <c>true</c> if marked obsolete; otherwise, <c>false</c>.
/// </returns>
public static bool IsEnumObsolete(this Enum value)
{
    var fi = value.GetType().GetField(value.ToString());
    var attributes = (ObsoleteAttribute[])fi.GetCustomAttributes(typeof(ObsoleteAttribute), false);
    return attributes.Length > 0;
}


 

/// <summary>Removes any items in a list that match a criteria</summary>
/// <param name="list">The list.</param>
/// <param name="predicate">The callback used to determine weather or not the item in the list should be removed.</param>
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is <see langword="null"/></exception>
public static void RemoveAny(this IList list,  Predicate<object> predicate )
{
    if (predicate == null)
    {
        throw new ArgumentNullException("predicate");
    }

    for (var i = list.Count - 1; i >= 0; i--)
    {
        var item = list[i];
        if (predicate(item))
        {
            list.RemoveAt(i);
        }
    }
}

I was recently working on a WPF MVVM application that used MEF to load plugins. I had broken up the main WPF application into smaller more manageable compartmentalized projects to help ensure more cross platform code. But when I tried to run the program it would start up fine but as soon as it tried to utilize logic from my plugins I was getting "assembly or one of it’s dependencies not found" exceptions.

Main Application
    -> LibraryA (References to Xceed.WPF.Toolkit)

Turns out when visual studio compiled my solution it was not copying Xceed.Wpf.Tookit into the main application bin/Debug folder. To solve this took a lot of searching on the interwebz before coming across this handy solution. Recursively Copying Indirect Project Dependencies in MSBuild.

Just download the CopyIndirectDependencies.targets CopyIndirectDependencies.targets (9.19 kb) and put it in your main application folder. Then edit your projects .csproj file to include this line "<Import Project="CopyIndirectDependencies.targets" />"

 

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  ...

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="CopyIndirectDependencies.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

Now when you build your main application project it should scan for indirect dependancies and copy them to the projects bin/Debug folder.


Provided below are two extension methods for determining if two unity Rect types intersect one another.

/// <summary>
/// Returns a third Rect structure that represents the intersection of two other Rect structures. 
/// If there is no intersection, an empty Rect is returned.
/// </summary>
/// <param name="a">
/// A rectangle to intersect.   
/// </param>
/// <param name="b">
/// B rectangle to intersect.  
/// </param>
/// <returns>
/// A Rect that represents the intersection of a and b.
/// </returns>
public static Rect Intersect(this Rect a, Rect b)
{
    float x = Math.Max((sbyte)a.x, (sbyte)b.x);
    var num2 = Math.Min(a.x + a.width, b.x + b.width);
    float y = Math.Max((sbyte)a.y, (sbyte)b.y);
    var num4 = Math.Min(a.y + a.height, b.y + b.height);
    if ((num2 >= x) && (num4 >= y))
    {
        return new Rect(x, y, num2 - x, num4 - y);
    }

    return new Rect();
}

/// <summary>
/// Determines if this rectangle intersects with rect.
/// </summary>
/// <param name="source">The source rectangle from which the intersection will be tested.</param>
/// <param name="rect">
/// The rectangle to test.
/// </param>
/// <returns>
/// This method returns true if there is any intersection, otherwise false.
/// </returns>
public static bool Intersects(this Rect source, Rect rect)
{
    return !((source.x > rect.xMax) || (source.xMax< rect.x) || (source.y > rect.yMax) || (source.yMax < rect.y));
}

The DrawRectangle & DrawLine helper methods allow you to draw a lines and rectangles inside OnGUI methods.

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.</param>
public static void DrawRectangle(Rect rect)
{
    DrawRectangle(rect, GUI.contentColor, 1f);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="color">
/// The color of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, Color color)
{
    DrawRectangle(rect, color, 1);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="thickness">
/// The line thickness of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, float thickness)
{
    DrawRectangle(rect, GUI.contentColor, thickness);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="color">
/// The color of the rectangle.
/// </param>
/// <param name="thickness">
/// The line thickness of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, Color color, float thickness)
{
    DrawHLine(new Vector2(rect.x, rect.y), rect.width, color, thickness);
    DrawHLine(new Vector2(rect.x, rect.y + rect.height - 1), rect.width, color, thickness);
    DrawVLine(new Vector2(rect.x, rect.y), rect.height, color, thickness);
    DrawVLine(new Vector2(rect.x + rect.width - 1, rect.y), rect.height, color, thickness);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB)
{
    DrawLine(pointA, pointB, GUI.contentColor, 1.0f);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="color">The color to use.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color)
{
    DrawLine(pointA, pointB, color, 1.0f);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="thickness">THe thickness of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, float thickness)
{
    DrawLine(pointA, pointB, GUI.contentColor, thickness);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="color">The color to use.</param>
/// <param name="thickness">THe thickness of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float thickness)
{
    // Save the current GUI matrix, since we're going to make changes to it.
    var matrix = GUI.matrix;

    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Determine the angle of the line.
    var angle = Vector2.Angle(pointB - pointA, Vector2.right);

    // Vector3.Angle always returns a positive number.
    // If pointB is above pointA, then angle needs to be negative.
    if (pointA.y > pointB.y)
    {
        angle = -angle;
    }

    // Use ScaleAroundPivot to adjust the size of the line.
    // We could do this when we draw the texture, but by scaling it here we can use
    //  non-integer values for the thickness and length (such as sub 1 pixel widths).
    // Note that the pivot point is at +.5 from pointA.y, this is so that the thickness of the line
    //  is centered on the origin at pointA.
    GUIUtility.ScaleAroundPivot(new Vector2((pointB - pointA).magnitude, thickness), new Vector2(pointA.x, pointA.y + 0.5f));

    // Set the rotation for the line.
    //  The angle was calculated with pointA as the origin.
    GUIUtility.RotateAroundPivot(angle, pointA);

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1, 1), Texture2D.whiteTexture);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.matrix = matrix;
    GUI.color = savedColor;
}

/// <summary>
/// Draws a horizontal line.
/// </summary>
/// <param name="pointA">
/// The start point of the line.
/// </param>
/// <param name="length">
/// The length of the line.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
/// <param name="thickness">
/// THe thickness of the line.
/// </param>
/// <remarks>
/// The line will be drawn from <see cref="pointA"/> moving to the right. To draw to the left use a negative <see cref="length"/> value.
/// </remarks>
public static void DrawHLine(Vector2 pointA, float length, Color color, float thickness)
{
    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, length - 1, thickness), Texture2D.whiteTexture, ScaleMode.StretchToFill);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.color = savedColor;
}

/// <summary>
/// Draws a vertical line.
/// </summary>
/// <param name="pointA">
/// The start point of the line.
/// </param>
/// <param name="length">
/// The length of the line.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
/// <param name="thickness">
/// THe thickness of the line.
/// </param>
/// <remarks>
/// The line will be drawn from <see cref="pointA"/> moving up. To draw down use a negative <see cref="length"/> value.
/// </remarks>
public static void DrawVLine(Vector2 pointA, float length, Color color, float thickness)
{
    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, thickness, length - 1), Texture2D.whiteTexture, ScaleMode.StretchToFill);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.color = savedColor;
}

The FillRectangle helper methods allow you to draw a filled rectangles inside OnGUI methods.

        /// <summary>
        /// Draws a filled rectangle.
        /// </summary>
        /// <param name="rect">
        /// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
        /// </param>
        public static void FillRectangle(Rect rect)
        {
            FillRectangle(rect, GUI.color);
        }

        /// <summary>
        /// Draws a filled rectangle.
        /// </summary>
        /// <param name="rect">
        /// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
        /// </param>
        /// <param name="color">
        /// The color of the rectangle.
        /// </param>
        public static void FillRectangle(Rect rect, Color color)
        {
            // Store current GUI color, so we can switch it back later,
            // and set the GUI color to the color parameter
            var savedColor = GUI.color;
            GUI.color = color;

            // Finally, draw the actual rectangle.
            GUI.DrawTexture(rect, Texture2D.whiteTexture, ScaleMode.StretchToFill);

            // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
            GUI.color = savedColor;
        }

Some times you need to make sure a component has been set on a game object and if it does not exist add it.

        /// <summary>
        /// Tries to get a component and if it does not exist adds the component and returns a reference to it. 
        /// </summary>
        /// <remarks>
        /// <example>
        /// Usage example:
        /// <code>
        /// var boxCollider = transform.GetOrAddComponent<BoxCollider>();
        /// </code>
        /// </example>
        /// </remarks>
        public static T GetOrAddComponent<T>(this Component child) where T : Component
        {
            var result = child.GetComponent<T>();
            if (result == null)
            {
                result = child.gameObject.AddComponent<T>();
            }

            return result;
        }

        /// <summary>Gets the value associated with the specified key and casts the value to the desired type.</summary>
        /// <returns>true if the <see cref="T:System.Collections.Generic.Dictionary`2" /> contains an element with the specified key; otherwise, false.</returns>
        /// <param name="dictionary">The dictionary to retrieve the value from.</param>
        /// <param name="key">The key of the value to get.</param>
        /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value" /> parameter. This parameter is passed uninitialized.</param>
        public static bool TryGetValueCast<T, V, C>(this IDictionary<T, V> dictionary, T name, out C value)
        {
            return TryGetValueCast(dictionary, name, out value, default(C));
        }

        /// <summary>Gets the value associated with the specified key and casts the value to the desired type.</summary>
        /// <returns>true if the <see cref="T:System.Collections.Generic.Dictionary`2" /> contains an element with the specified key; otherwise, false.</returns>
        /// <param name="dictionary">The dictionary to retrieve the value from.</param>
        /// <param name="key">The key of the value to get.</param>
        /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value" /> parameter. This parameter is passed uninitialized.</param>
        /// <param name="defaultValue">The default value to return if unable to fetch using key.</param>
        public static bool TryGetValueCast<T, V, C>(this IDictionary<T, V> dictionary, T key, out C value, C defaultValue)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            try
            {
                value = (C)Convert.ChangeType(dictionary[key], typeof(C));
            }
            catch
            {
                value = defaultValue;
                return false;
            }

            return true;
        }

The code snippet below allows you to attach a translation gizmo to a object to allow the user to move that object while the game is running. Works just like the unity translation gizmo.

TranslationGizmo

    using System;        
    using UnityEngine;

    /// <summary>
    /// The translation gizmo.
    /// </summary>
    public class TranslationGizmo : MonoBehaviour
    {
        #region Fields

        /// <summary>
        /// The camera reference used to determin distance to translation gizmo.
        /// </summary>
        public UnityEngine.Camera cameraReference;

        /// <summary>
        /// Used to determine how thick the envelope is around a axis line when attempting to click and drag it with the mouse.
        /// </summary>
        public float axisEnvelope = 0.04f;

        /// <summary>
        /// Determines the length of the cone.
        /// </summary>
        public float coneLength = 0.85f;

        /// <summary>
        /// Specifies how many segments the cone is made up of.
        /// </summary>
        public int coneSegments = 30;

        /// <summary>
        /// Determines the diameter of the cone.
        /// </summary>
        public float coneSize = 0.075f;

        /// <summary>
        /// Determines the length of the axis lines.
        /// </summary>
        public float lineLength = 0.13f;

        /// <summary>
        /// The line material used to render the lines.
        /// </summary>
        public Material lineMaterial;

        /// <summary>
        /// Determines the size of the square panning gizmos.
        /// </summary>
        public float panScale = 0.3f;

        /// <summary>
        /// Determines the transparency level of the axis plane quads.
        /// </summary>
        public float planeTransparency = 0.3f;

        /// <summary>
        /// The color that will be used for the X axis.
        /// </summary>
        public Color xAxisColor = Color.red;

        /// <summary>
        /// The color that will be used for the Y axis.
        /// </summary>
        public Color yAxisColor = Color.green;

        /// <summary>
        /// The color that will be used for the Z axis.
        /// </summary>
        public Color zAxisColor = Color.blue;

        /// <summary>
        /// Holds the button state used to determine if dragging has started.
        /// </summary>
        protected bool inputButtonState;

        /// <summary>
        /// Holds the relative mouse X position.
        /// </summary>
        protected float mouseAxisX;

        /// <summary>
        /// Holds the relative mouse Y position.
        /// </summary>
        protected float mouseAxisY;

        /// <summary>
        /// Holds the pre-calculated distance value from the camera to the game object.
        /// </summary>
        private float cameraDistance;

        /// <summary>
        /// The hit mouse down.
        /// </summary>
        private Vector3 hitMouseDown;

        /// <summary>
        /// The hit mouse move.
        /// </summary>
        private Vector3 hitMouseMove;

        /// <summary>
        /// Holds wheather or not the input axis has changed.
        /// </summary>
        private bool inputAxisChanged;

        /// <summary>
        /// Holds wheather or not the mouse if being dragged.
        /// </summary>
        private bool isDragging;

        /// <summary>
        /// Holds XY plane information.
        /// </summary>
        private Plane planeXY;

        /// <summary>
        /// Holds XZ plane information.
        /// </summary>
        private Plane planeXZ;

        /// <summary>
        /// Holds YZ plane information.
        /// </summary>
        private Plane planeYZ;

        /// <summary>
        /// Holds the previous input button state.
        /// </summary>
        private bool previousInputButtonState;

        /// <summary>
        /// Holds the rotation matrix used to determine at what rotation the gizmo will be drawn at.
        /// </summary>
        private Matrix4x4 rotationMatrix;

        /// <summary>
        /// Holds the value for the <see cref="WorldTranslation"/> property.
        /// </summary>
        [SerializeField]
        private bool worldTranslation;

        /// <summary>
        /// Gets or sets a value indicating whether world translation is enabled.
        /// </summary>
        /// <value>
        ///   <c>true</c> if world translation is active; otherwise, <c>false</c> for local translation.
        /// </value>
        public bool WorldTranslation
        {
            get
            {
                return this.worldTranslation;
            }

            set
            {
                this.worldTranslation = value;
            }
        }

        /// <summary>
        /// Holds the type of axis movement is currently being made.
        /// </summary>
        private TypeOfMove typeMovementBeingMade;

        #endregion

        #region Public Events

        /// <summary>
        /// Occurs when position changed.
        /// </summary>
        public event EventHandler<Vector3EventArgs> PositionChanged;

        #endregion

        #region Enums

        /// <summary>
        /// Enum defining the types of axis movement.
        /// </summary>
        private enum TypeOfMove
        {
            /// <summary>
            /// No axis movement.
            /// </summary>
            None,

            /// <summary>
            /// Movement along the X axis.
            /// </summary>
            XAxis,

            /// <summary>
            /// Movement along the 
            /// </summary>
            X2Axis,

            /// <summary>
            /// Movement along the Y axis.
            /// </summary>
            YAxis,

            /// <summary>
            /// The y 2 axis.
            /// </summary>
            Y2Axis,

            /// <summary>
            /// Movement along the Z axis.
            /// </summary>
            ZAxis,

            /// <summary>
            /// The z 2 axis.
            /// </summary>
            Z2Axis,

            /// <summary>
            /// Movement along the XZ axis.
            /// </summary>
            XZAxis,

            /// <summary>
            /// Movement along the YZ axis.
            /// </summary>
            YZAxis,

            /// <summary>
            /// Movement along the XY axis.
            /// </summary>
            XYAxis,
        };

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Awake is called when the script instance is being loaded.
        /// </summary>
        public void Awake()
        {
            // if no material assigned setup a default material
            if (this.lineMaterial == null)
            {
#if !UNITY_5
                this.lineMaterial =
                     new Material(
                         "Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + "    Blend SrcAlpha OneMinusSrcAlpha "
                         + "    ZWrite Off Cull Off Fog { Mode Off } ZTest Always" + "    BindChannels {"
                         + "      Bind \"vertex\", vertex Bind \"color\", color }" + "} } }");
#else
                // Unity has a built-in shader that is useful for drawing
                // simple colored things.
                var shader = Shader.Find("Hidden/Internal-Colored");
                this.lineMaterial = new Material(shader);
                this.lineMaterial.hideFlags = HideFlags.HideAndDontSave;
                // Turn on alpha blending
                this.lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                this.lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                // Turn backface culling off
                this.lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
                // Turn off depth writes
                this.lineMaterial.SetInt("_ZWrite", 0);
#endif
            }
        }

        /// <summary>
        /// Update is called every frame, if the MonoBehaviour is enabled.
        /// </summary>
        public void Update()
        {
            if (cameraReference == null)
            {
                return;
            }

            // calculate distance once between the camera and the game objects position
            var transformReference = this.transform;
            var position = transformReference.position;
            this.cameraDistance = Vector3.Distance(this.cameraReference.transform.position, position) * this.lineLength;

            this.previousInputButtonState = this.inputButtonState;
            this.GetInputButtonState();

            // if the input button is released stop moving
            if (!this.inputButtonState)
            {
                this.isDragging = false;
                this.typeMovementBeingMade = TypeOfMove.None;
                return;
            }

            // only handle dragging is already dragging or if input button state was pressed
            if (this.isDragging || (!this.previousInputButtonState && this.inputButtonState))
            {
                // get mouse/input values
                this.GetInputValues();
                this.inputAxisChanged = Math.Abs(this.mouseAxisX) > float.Epsilon || Math.Abs(this.mouseAxisY) > float.Epsilon;

                // update planes based on world or local translation
                if (this.worldTranslation)
                {
                    // update planes
                    this.planeXZ.SetNormalAndPosition(Vector3.up, position);
                    this.planeXY.SetNormalAndPosition(Vector3.forward, position);
                    this.planeYZ.SetNormalAndPosition(Vector3.right, position);
                }
                else
                {
                    // update planes
                    this.planeXZ.SetNormalAndPosition(transformReference.up, position);
                    this.planeXY.SetNormalAndPosition(transformReference.forward, position);
                    this.planeYZ.SetNormalAndPosition(transformReference.right, position);
                }

                // cast a ray once
                var ray = this.cameraReference.ScreenPointToRay(Input.mousePosition);

                // handle axis translations
                this.HandleXZ(ray);
                this.HandleXY(ray);
                this.HandleYZ(ray);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Get the state of the input button used to determine if dragging has occurred.
        /// </summary>
        protected virtual void GetInputButtonState()
        {
            this.inputButtonState = Input.GetMouseButton(0);
        }

        /// <summary>
        /// Gets the X/Y input values
        /// </summary>
        protected virtual void GetInputValues()
        {
            this.mouseAxisX = Input.GetAxis("Mouse X");
            this.mouseAxisY = Input.GetAxis("Mouse Y");
        }

        /// <summary>
        /// Raises the <see cref="PositionChanged"/> event.
        /// </summary>
        /// <param name="previousPosition">
        /// The previous Position.
        /// </param>
        /// <param name="position">
        /// The position.
        /// </param>
        protected virtual void OnPositionChanged(Vector3 previousPosition, Vector3 position)
        {
            var handler = this.PositionChanged;
            if (handler != null)
            {
                handler(this, new Vector3EventArgs(previousPosition, position));
            }
        }

        /// <summary>
        /// Uses OpenGL to draw a line for the axis.
        /// </summary>
        /// <param name="color">
        /// The color to use when drawing the cone.
        /// </param>
        /// <param name="axis">
        /// Defines axis that the cone will be drawn against.
        /// </param>
        /// <param name="axisU">
        /// Represents the "X" axis to draw the base of the cone against.
        /// </param>
        /// <param name="axisV">
        /// Represents the "Y" axis to draw the base of the cone against.
        /// </param>
        private void DrawAxis(Color color, Vector3 axis, Vector3 axisU, Vector3 axisV)
        {
            // scale the axis by the camera distance
            axis = axis * this.cameraDistance;

            // start drawing lines
            GL.Begin(GL.LINES);
            GL.Color(color);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(axis.x, axis.y, axis.z);
            GL.End();

            // draw the cone for the axis
            this.DrawCone(axis, axisU, axisV, this.cameraDistance * this.coneSize, this.coneLength, color);
        }

        /// <summary>
        /// Uses OpenGL to draw a cone for the axis.
        /// </summary>
        /// <param name="axis">
        /// Defines axis that the cone will be drawn against.
        /// </param>
        /// <param name="axisU">
        /// Represents the "X" axis to draw the base of the cone against.
        /// </param>
        /// <param name="axisV">
        /// Represents the "Y" axis to draw the base of the cone against.
        /// </param>
        /// <param name="radius">
        /// Specifies the radius for the base of the cone.
        /// </param>
        /// <param name="length">
        /// Specifies how tall the cone is.
        /// </param>
        /// <param name="color">
        /// The color to use when drawing the cone.
        /// </param>
        private void DrawCone(Vector3 axis, Vector3 axisU, Vector3 axisV, float radius, float length, Color color)
        {
            GL.Begin(GL.TRIANGLES);
            GL.Color(color);

            // pre-calculate the angle step for each cone segment
            var angle = (2 * Mathf.PI) / this.coneSegments;

            for (var i = 0; i <= this.coneSegments; i++)
            {
                var pt = axisU * Mathf.Cos(angle * i) * radius;
                pt += axisV * Mathf.Sin(angle * i) * radius;
                pt += axis * length;
                GL.Vertex(pt);

                pt = axisU * Mathf.Cos(angle * (i + 1)) * radius;
                pt += axisV * Mathf.Sin(angle * (i + 1)) * radius;
                pt += axis * length;
                GL.Vertex(pt);

                GL.Vertex(axis);
            }

            GL.End();
        }

        /// <summary>
        /// Uses OpenGL to draw a square plane for the axis.
        /// </summary>
        /// <param name="size">
        /// Defines size of the plane that will be drawn for the axis.
        /// </param>
        /// <param name="axisU">
        /// Represents the "X" axis to draw the plane against.
        /// </param>
        /// <param name="axisV">
        /// Represents the "Y" axis to draw the plane against.
        /// </param>
        /// <param name="color">
        /// The color to use when drawing the plane.
        /// </param>
        private void DrawQuad(float size, Vector3 axisU, Vector3 axisV, Color color)
        {
            color.a = this.planeTransparency;
            var pts = new Vector3[4];
            pts[0] = Vector3.zero;
            pts[1] = axisU * size;
            pts[2] = (axisU + axisV) * size;
            pts[3] = axisV * size;

            GL.Begin(GL.QUADS);
            GL.Color(color);
            GL.Vertex(pts[0]);
            GL.Vertex(pts[1]);
            GL.Vertex(pts[2]);
            GL.Vertex(pts[3]);
            GL.End();
            color.a = 1f;

            GL.Begin(GL.LINES);
            GL.Color(color);
            GL.Vertex(pts[0]);
            GL.Vertex(pts[1]);
            GL.Vertex(pts[1]);
            GL.Vertex(pts[2]);
            GL.Vertex(pts[2]);
            GL.Vertex(pts[3]);
            GL.Vertex(pts[3]);
            GL.Vertex(pts[0]);
            GL.End();
        }

        /// <summary>
        /// Start is called just before any of the Update methods is called the first time.
        /// </summary>
        public void Start()
        {
            if (this.cameraReference == null)
            {
                this.cameraReference = UnityEngine.Camera.main;
            }
        }

        /// <summary>
        /// Handles dragging along the XU plane.
        /// </summary>
        /// <param name="ray">
        /// The reay used to determine if the XY plane was being interacted with.
        /// </param>
        private void HandleXY(Ray ray)
        {
            float enter;
            this.planeXY.Raycast(ray, out enter);
            var hit = ray.GetPoint(enter);
            hit = this.rotationMatrix.inverse.MultiplyPoint(hit);

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.x > 0f && hit.x <= this.panScale * this.cameraDistance && hit.y > 0
                && hit.y <= this.panScale * this.cameraDistance)
            {
                this.typeMovementBeingMade = TypeOfMove.XYAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.XYAxis)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.z = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.x > 0f && hit.x <= this.cameraDistance
                && Mathf.Abs(hit.y) < this.axisEnvelope * this.cameraDistance)
            {
                // case x
                this.typeMovementBeingMade = TypeOfMove.X2Axis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.X2Axis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.y = 0;
                this.hitMouseMove.z = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.y > 0f && hit.y <= this.cameraDistance
                && Mathf.Abs(hit.x) < this.axisEnvelope * this.cameraDistance)
            {
                // case y
                this.typeMovementBeingMade = TypeOfMove.YAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.YAxis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.z = 0;
                this.hitMouseMove.x = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }
        }

        /// <summary>
        /// Handles dragging along the XZ plane.
        /// </summary>
        /// <param name="ray">
        /// The reay used to determine if the XZ plane was being interacted with.
        /// </param>
        private void HandleXZ(Ray ray)
        {
            float enter;
            this.planeXZ.Raycast(ray, out enter);
            var hit = ray.GetPoint(enter);
            hit = this.rotationMatrix.inverse.MultiplyPoint(hit);

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.x > 0f && hit.x <= this.panScale * this.cameraDistance && hit.z > 0
                && hit.z <= this.panScale * this.cameraDistance)
            {
                this.typeMovementBeingMade = TypeOfMove.XZAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.XZAxis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.y = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.x > 0f && hit.x <= this.cameraDistance
                && Mathf.Abs(hit.z) < this.axisEnvelope * this.cameraDistance)
            {
                // case x
                this.typeMovementBeingMade = TypeOfMove.XAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.XAxis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.y = 0;
                this.hitMouseMove.z = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.z > 0f && hit.z <= this.cameraDistance
                && Mathf.Abs(hit.x) < this.axisEnvelope * this.cameraDistance)
            {
                // case  z
                this.typeMovementBeingMade = TypeOfMove.ZAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.ZAxis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.y = 0;
                this.hitMouseMove.x = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }
        }

        /// <summary>
        /// Handles dragging along the YZ plane.
        /// </summary>
        /// <param name="ray">
        /// The reay used to determine if the YZ plane was being interacted with.
        /// </param>
        private void HandleYZ(Ray ray)
        {
            float enter;
            this.planeYZ.Raycast(ray, out enter);
            var hit = ray.GetPoint(enter);
            hit = this.rotationMatrix.inverse.MultiplyPoint(hit);

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.z > 0 && hit.z <= this.panScale * this.cameraDistance && hit.y > 0
                && hit.y <= this.panScale * this.cameraDistance)
            {
                this.typeMovementBeingMade = TypeOfMove.YZAxis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.YZAxis)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.x = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.z > 0f && hit.z <= this.cameraDistance
                && Mathf.Abs(hit.y) < this.axisEnvelope * this.cameraDistance)
            {
                // case  z
                this.typeMovementBeingMade = TypeOfMove.Z2Axis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.Z2Axis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.y = 0;
                this.hitMouseMove.x = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }

            if (this.typeMovementBeingMade == TypeOfMove.None && hit.y > 0f && hit.y <= this.cameraDistance
                && Mathf.Abs(hit.z) < this.axisEnvelope * this.cameraDistance)
            {
                // case y
                this.typeMovementBeingMade = TypeOfMove.Y2Axis;
                this.hitMouseDown = hit;
                this.isDragging = true;
            }

            if (this.typeMovementBeingMade == TypeOfMove.Y2Axis && this.inputAxisChanged)
            {
                this.hitMouseMove = hit - this.hitMouseDown;
                this.hitMouseMove.z = 0;
                this.hitMouseMove.x = 0;
                this.UpdatePosition(this.worldTranslation ? this.hitMouseMove : this.transform.localRotation * this.hitMouseMove);
            }
        }

        /// <summary>
        /// Called by unity when after camera has rendered the scene.
        /// </summary>
        private void OnRenderObject()
        {         
            // set the material pass is a material has been specified
            var material = this.lineMaterial;
            if (material != null)
            {
                material.SetPass(0);
            }

            // push a matrix on to the stack
            GL.PushMatrix();
            this.rotationMatrix = this.transform.localToWorldMatrix;

            // setup rotation matrix
            if (this.worldTranslation)
            {
                this.rotationMatrix = Matrix4x4.TRS(this.transform.position, Quaternion.identity, Vector3.one);
            }
            else
            {
                this.rotationMatrix = Matrix4x4.TRS(this.transform.position, this.transform.localRotation, Vector3.one);
            }

            GL.MultMatrix(this.rotationMatrix);

            // draw each axis
            this.DrawAxis(this.xAxisColor, Vector3.right, Vector3.up, Vector3.forward);
            this.DrawAxis(this.yAxisColor, Vector3.up, Vector3.right, Vector3.forward);
            this.DrawAxis(this.zAxisColor, Vector3.forward, Vector3.right, Vector3.up);

            // draw each plane quad
            this.DrawQuad(this.panScale * this.cameraDistance, Vector3.forward, Vector3.up, this.xAxisColor);
            this.DrawQuad(this.panScale * this.cameraDistance, Vector3.right, Vector3.forward, this.yAxisColor);
            this.DrawQuad(this.panScale * this.cameraDistance, Vector3.right, Vector3.up, this.zAxisColor);

            GL.PopMatrix();
        }

        /// <summary>
        /// Updates the transform position and raises the <see cref="PositionChanged"/> event.
        /// </summary>
        /// <param name="value">
        /// The value to add on to the existing transform position.
        /// </param>
        private void UpdatePosition(Vector3 value)
        {
            var position = this.transform.position;
            var previousPosition = position;
            position += value;
            this.transform.position = position;
            this.OnPositionChanged(previousPosition, position);
        }

        #endregion
    }

    /// 
    /// Provides event arguments for the  type.
    /// 
    public class Vector3EventArgs : EventArgs
    {
        #region Constructors and Destructors

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// 
        /// The previous value.
        /// 
        /// 
        /// The current value.
        /// 
        public Vector3EventArgs(Vector3 previousValue, Vector3 value)
        {
            this.Value = value;
            this.PreviousValue = previousValue;
        }

        /// 
        /// Initializes a new instance of the  class.
        /// 
        public Vector3EventArgs()
        {
        }

        #endregion

        #region Public Properties

        /// 
        /// Gets or sets a previous  value.
        /// 
        public Vector3 PreviousValue { get; set; }

        /// 
        /// Gets or sets a  value.
        /// 
        public Vector3 Value { get; set; }

        #endregion
    }

Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume