架构大更
This commit is contained in:
@@ -1,167 +1,185 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom editor for the MMScreenSafeZones component
|
||||
/// Custom editor for the MMScreenSafeZones component
|
||||
/// </summary>
|
||||
[CustomEditor(typeof(MMAspectRatioSafeZones), true)]
|
||||
[CanEditMultipleObjects]
|
||||
public class MMAspectRatioSafeZonesEditor : Editor
|
||||
{
|
||||
static MMAspectRatioSafeZones safeZones;
|
||||
[CanEditMultipleObjects]
|
||||
public class MMAspectRatioSafeZonesEditor : Editor
|
||||
{
|
||||
private static MMAspectRatioSafeZones safeZones;
|
||||
|
||||
/// <summary>
|
||||
/// On enable, registers to the OnSceneGUI hook
|
||||
/// </summary>
|
||||
void OnEnable()
|
||||
{
|
||||
SceneView.duringSceneGui -= OnSceneGUI;
|
||||
safeZones = (MMAspectRatioSafeZones)target;
|
||||
SceneView.duringSceneGui += OnSceneGUI;
|
||||
}
|
||||
/// <summary>
|
||||
/// On enable, registers to the OnSceneGUI hook
|
||||
/// </summary>
|
||||
private void OnEnable()
|
||||
{
|
||||
SceneView.duringSceneGui -= OnSceneGUI;
|
||||
safeZones = (MMAspectRatioSafeZones)target;
|
||||
SceneView.duringSceneGui += OnSceneGUI;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OnSceneGUI, draws center and ratios
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void OnSceneGUI(SceneView sceneView)
|
||||
{
|
||||
DrawFrameCenter(sceneView);
|
||||
DrawRatios(sceneView);
|
||||
}
|
||||
/// <summary>
|
||||
/// OnSceneGUI, draws center and ratios
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void OnSceneGUI(SceneView sceneView)
|
||||
{
|
||||
DrawFrameCenter(sceneView);
|
||||
DrawRatios(sceneView);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a rectangle for each ratio
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void DrawRatios(SceneView sceneView)
|
||||
{
|
||||
if (!safeZones.DrawRatios)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/// <summary>
|
||||
/// Draws a rectangle for each ratio
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void DrawRatios(SceneView sceneView)
|
||||
{
|
||||
if (!safeZones.DrawRatios) return;
|
||||
|
||||
Vector3 center = sceneView.pivot;
|
||||
var center = sceneView.pivot;
|
||||
|
||||
float width = sceneView.position.width;
|
||||
float height = sceneView.position.height;
|
||||
var width = sceneView.position.width;
|
||||
var height = sceneView.position.height;
|
||||
|
||||
Vector3 bottomLeft = new Vector3(center.x - width / 2f, center.y - height / 2f, 0f);
|
||||
Vector3 topRight = new Vector3(center.x + width / 2f, center.y + height / 2f, 0f);
|
||||
|
||||
Vector3 topLeft = bottomLeft;
|
||||
topLeft.y = topRight.y;
|
||||
Vector3 bottomRight = topRight;
|
||||
bottomRight.y = bottomLeft.y;
|
||||
var bottomLeft = new Vector3(center.x - width / 2f, center.y - height / 2f, 0f);
|
||||
var topRight = new Vector3(center.x + width / 2f, center.y + height / 2f, 0f);
|
||||
|
||||
float size = safeZones.CameraSize;
|
||||
|
||||
// dotted lines
|
||||
float spacing = 2f;
|
||||
Color dottedLineColor = Color.white;
|
||||
dottedLineColor.a = 0.4f;
|
||||
Handles.color = dottedLineColor;
|
||||
// top
|
||||
Handles.DrawDottedLine(new Vector3(topLeft.x, center.y + size, 0f), new Vector3(topRight.x, center.y + size, 0f), spacing);
|
||||
// bottom
|
||||
Handles.DrawDottedLine(new Vector3(topLeft.x, center.y - size, 0f), new Vector3(topRight.x, center.y - size, 0f), spacing);
|
||||
var topLeft = bottomLeft;
|
||||
topLeft.y = topRight.y;
|
||||
var bottomRight = topRight;
|
||||
bottomRight.y = bottomLeft.y;
|
||||
|
||||
foreach (Ratio ratio in safeZones.Ratios)
|
||||
{
|
||||
if (ratio.DrawRatio)
|
||||
{
|
||||
float aspectRatio = ratio.Size.x / ratio.Size.y;
|
||||
var size = safeZones.CameraSize;
|
||||
|
||||
Handles.color = ratio.RatioColor;
|
||||
// dotted lines
|
||||
var spacing = 2f;
|
||||
var dottedLineColor = Color.white;
|
||||
dottedLineColor.a = 0.4f;
|
||||
Handles.color = dottedLineColor;
|
||||
// top
|
||||
Handles.DrawDottedLine(new Vector3(topLeft.x, center.y + size, 0f),
|
||||
new Vector3(topRight.x, center.y + size, 0f), spacing);
|
||||
// bottom
|
||||
Handles.DrawDottedLine(new Vector3(topLeft.x, center.y - size, 0f),
|
||||
new Vector3(topRight.x, center.y - size, 0f), spacing);
|
||||
|
||||
// aspect ratio positions
|
||||
Vector3 ratioTopLeft = new Vector3(center.x - size * aspectRatio, center.y + size, 0f);
|
||||
Vector3 ratioTopRight = new Vector3(center.x + size * aspectRatio, center.y + size, 0f);
|
||||
Vector3 ratioBottomLeft = new Vector3(center.x - size * aspectRatio, center.y - size, 0f);
|
||||
Vector3 ratioBottomRight = new Vector3(center.x + size * aspectRatio, center.y - size, 0f);
|
||||
Vector3 ratioLabelPosition = ratioBottomLeft + 0.1f * Vector3.down + 0.1f * Vector3.right;
|
||||
foreach (var ratio in safeZones.Ratios)
|
||||
if (ratio.DrawRatio)
|
||||
{
|
||||
var aspectRatio = ratio.Size.x / ratio.Size.y;
|
||||
|
||||
// draws a label under the rectangle
|
||||
GUIStyle style = new GUIStyle();
|
||||
style.normal.textColor = ratio.RatioColor;
|
||||
style.fontSize = 8;
|
||||
Handles.Label(ratioLabelPosition, ratio.Size.x + ":" + ratio.Size.y, style);
|
||||
Handles.color = ratio.RatioColor;
|
||||
|
||||
// draws a rectangle around the aspect ratio
|
||||
Vector3[] verts = new Vector3[] { ratioTopLeft, ratioTopRight, ratioBottomRight, ratioBottomLeft };
|
||||
Handles.DrawSolidRectangleWithOutline(verts, new Color(0, 0, 0, 0), ratio.RatioColor);
|
||||
// aspect ratio positions
|
||||
var ratioTopLeft = new Vector3(center.x - size * aspectRatio, center.y + size, 0f);
|
||||
var ratioTopRight = new Vector3(center.x + size * aspectRatio, center.y + size, 0f);
|
||||
var ratioBottomLeft = new Vector3(center.x - size * aspectRatio, center.y - size, 0f);
|
||||
var ratioBottomRight = new Vector3(center.x + size * aspectRatio, center.y - size, 0f);
|
||||
var ratioLabelPosition = ratioBottomLeft + 0.1f * Vector3.down + 0.1f * Vector3.right;
|
||||
|
||||
// draws the dead zone of that ratio
|
||||
Color zoneColor = ratio.RatioColor;
|
||||
zoneColor.a = zoneColor.a * safeZones.UnsafeZonesOpacity;
|
||||
// draws a label under the rectangle
|
||||
var style = new GUIStyle();
|
||||
style.normal.textColor = ratio.RatioColor;
|
||||
style.fontSize = 8;
|
||||
Handles.Label(ratioLabelPosition, ratio.Size.x + ":" + ratio.Size.y, style);
|
||||
|
||||
// top rectangle
|
||||
verts = new Vector3[] { topLeft, topRight, new Vector3(topLeft.x, ratioTopLeft.y, 0f), new Vector3(topRight.x, ratioTopRight.y, 0f) };
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
// draws a rectangle around the aspect ratio
|
||||
var verts = new[] { ratioTopLeft, ratioTopRight, ratioBottomRight, ratioBottomLeft };
|
||||
Handles.DrawSolidRectangleWithOutline(verts, new Color(0, 0, 0, 0), ratio.RatioColor);
|
||||
|
||||
// bottom rectangle
|
||||
verts = new Vector3[] { bottomLeft, new Vector3(topLeft.x, ratioBottomLeft.y, 0f), new Vector3(topRight.x, ratioBottomRight.y, 0f), bottomRight };
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
// draws the dead zone of that ratio
|
||||
var zoneColor = ratio.RatioColor;
|
||||
zoneColor.a = zoneColor.a * safeZones.UnsafeZonesOpacity;
|
||||
|
||||
// left rectangle
|
||||
verts = new Vector3[] { new Vector3(topLeft.x, ratioTopLeft.y, 0f), ratioTopLeft, ratioBottomLeft, new Vector3(bottomLeft.x, ratioBottomLeft.y, 0f) };
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
// top rectangle
|
||||
verts = new[]
|
||||
{
|
||||
topLeft, topRight, new Vector3(topLeft.x, ratioTopLeft.y, 0f),
|
||||
new Vector3(topRight.x, ratioTopRight.y, 0f)
|
||||
};
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
|
||||
// right rectangle
|
||||
verts = new Vector3[] { new Vector3(topRight.x, ratioTopRight.y, 0f), new Vector3(bottomRight.x, ratioBottomRight.y, 0f), ratioBottomRight, ratioTopRight};
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
// bottom rectangle
|
||||
verts = new[]
|
||||
{
|
||||
bottomLeft, new Vector3(topLeft.x, ratioBottomLeft.y, 0f),
|
||||
new Vector3(topRight.x, ratioBottomRight.y, 0f), bottomRight
|
||||
};
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
|
||||
// dotted line left
|
||||
Handles.DrawDottedLine(new Vector3(ratioBottomLeft.x, topLeft.y, 0f), new Vector3(ratioTopLeft.x, bottomLeft.y, 0f), spacing);
|
||||
// dotted line right
|
||||
Handles.DrawDottedLine(new Vector3(ratioBottomRight.x, topLeft.y, 0f), new Vector3(ratioBottomRight.x, bottomLeft.y, 0f), spacing);
|
||||
}
|
||||
}
|
||||
}
|
||||
// left rectangle
|
||||
verts = new[]
|
||||
{
|
||||
new Vector3(topLeft.x, ratioTopLeft.y, 0f), ratioTopLeft, ratioBottomLeft,
|
||||
new Vector3(bottomLeft.x, ratioBottomLeft.y, 0f)
|
||||
};
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
|
||||
/// <summary>
|
||||
/// Draws a crosshair at the center
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void DrawFrameCenter(SceneView sceneView)
|
||||
{
|
||||
if (!safeZones.DrawCenterCrosshair)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// right rectangle
|
||||
verts = new[]
|
||||
{
|
||||
new Vector3(topRight.x, ratioTopRight.y, 0f),
|
||||
new Vector3(bottomRight.x, ratioBottomRight.y, 0f), ratioBottomRight, ratioTopRight
|
||||
};
|
||||
Handles.DrawSolidRectangleWithOutline(verts, zoneColor, new Color(0, 0, 0, 0));
|
||||
|
||||
Vector3 center = sceneView.pivot;
|
||||
float crossHairSize = safeZones.CenterCrosshairSize;
|
||||
// dotted line left
|
||||
Handles.DrawDottedLine(new Vector3(ratioBottomLeft.x, topLeft.y, 0f),
|
||||
new Vector3(ratioTopLeft.x, bottomLeft.y, 0f), spacing);
|
||||
// dotted line right
|
||||
Handles.DrawDottedLine(new Vector3(ratioBottomRight.x, topLeft.y, 0f),
|
||||
new Vector3(ratioBottomRight.x, bottomLeft.y, 0f), spacing);
|
||||
}
|
||||
}
|
||||
|
||||
float reticleSize = crossHairSize / 10f;
|
||||
/// <summary>
|
||||
/// Draws a crosshair at the center
|
||||
/// </summary>
|
||||
/// <param name="sceneView"></param>
|
||||
private static void DrawFrameCenter(SceneView sceneView)
|
||||
{
|
||||
if (!safeZones.DrawCenterCrosshair) return;
|
||||
|
||||
Handles.color = safeZones.CenterCrosshairColor;
|
||||
var center = sceneView.pivot;
|
||||
var crossHairSize = safeZones.CenterCrosshairSize;
|
||||
|
||||
Vector3 crosshairTopLeft = new Vector3(center.x - crossHairSize / 2f, center.y + crossHairSize / 2f, 0f);
|
||||
Vector3 crosshairTopRight = new Vector3(center.x + crossHairSize / 2f, center.y + crossHairSize / 2f, 0f);
|
||||
Vector3 crosshairBottomLeft = new Vector3(center.x - crossHairSize / 2f, center.y - crossHairSize / 2f, 0f);
|
||||
Vector3 crosshairBottomRight = new Vector3(center.x + crossHairSize / 2f, center.y - crossHairSize / 2f, 0f);
|
||||
var reticleSize = crossHairSize / 10f;
|
||||
|
||||
// cross
|
||||
Handles.DrawLine(new Vector3(center.x, center.y + crossHairSize / 2f, 0f), new Vector3(center.x, center.y - crossHairSize / 2f, 0f));
|
||||
Handles.DrawLine(new Vector3(center.x - crossHairSize / 2f, center.y, 0f), new Vector3(center.x + crossHairSize / 2f, center.y, 0f));
|
||||
Handles.color = safeZones.CenterCrosshairColor;
|
||||
|
||||
// top left
|
||||
Handles.DrawLine(crosshairTopLeft, new Vector3(crosshairTopLeft.x + reticleSize, crosshairTopLeft.y, 0f));
|
||||
Handles.DrawLine(crosshairTopLeft, new Vector3(crosshairTopLeft.x, crosshairTopLeft.y - reticleSize, 0f));
|
||||
// top right
|
||||
Handles.DrawLine(crosshairTopRight, new Vector3(crosshairTopRight.x - reticleSize, crosshairTopRight.y, 0f));
|
||||
Handles.DrawLine(crosshairTopRight, new Vector3(crosshairTopRight.x, crosshairTopRight.y - reticleSize, 0f));
|
||||
// bottom left
|
||||
Handles.DrawLine(crosshairBottomLeft, new Vector3(crosshairBottomLeft.x + reticleSize, crosshairBottomLeft.y, 0f));
|
||||
Handles.DrawLine(crosshairBottomLeft, new Vector3(crosshairBottomLeft.x, crosshairBottomLeft.y + reticleSize, 0f));
|
||||
// bottom right
|
||||
Handles.DrawLine(crosshairBottomRight, new Vector3(crosshairBottomRight.x - reticleSize, crosshairBottomRight.y, 0f));
|
||||
Handles.DrawLine(crosshairBottomRight, new Vector3(crosshairBottomRight.x, crosshairBottomRight.y + reticleSize, 0f));
|
||||
}
|
||||
}
|
||||
var crosshairTopLeft = new Vector3(center.x - crossHairSize / 2f, center.y + crossHairSize / 2f, 0f);
|
||||
var crosshairTopRight = new Vector3(center.x + crossHairSize / 2f, center.y + crossHairSize / 2f, 0f);
|
||||
var crosshairBottomLeft = new Vector3(center.x - crossHairSize / 2f, center.y - crossHairSize / 2f, 0f);
|
||||
var crosshairBottomRight = new Vector3(center.x + crossHairSize / 2f, center.y - crossHairSize / 2f, 0f);
|
||||
|
||||
// cross
|
||||
Handles.DrawLine(new Vector3(center.x, center.y + crossHairSize / 2f, 0f),
|
||||
new Vector3(center.x, center.y - crossHairSize / 2f, 0f));
|
||||
Handles.DrawLine(new Vector3(center.x - crossHairSize / 2f, center.y, 0f),
|
||||
new Vector3(center.x + crossHairSize / 2f, center.y, 0f));
|
||||
|
||||
// top left
|
||||
Handles.DrawLine(crosshairTopLeft, new Vector3(crosshairTopLeft.x + reticleSize, crosshairTopLeft.y, 0f));
|
||||
Handles.DrawLine(crosshairTopLeft, new Vector3(crosshairTopLeft.x, crosshairTopLeft.y - reticleSize, 0f));
|
||||
// top right
|
||||
Handles.DrawLine(crosshairTopRight,
|
||||
new Vector3(crosshairTopRight.x - reticleSize, crosshairTopRight.y, 0f));
|
||||
Handles.DrawLine(crosshairTopRight,
|
||||
new Vector3(crosshairTopRight.x, crosshairTopRight.y - reticleSize, 0f));
|
||||
// bottom left
|
||||
Handles.DrawLine(crosshairBottomLeft,
|
||||
new Vector3(crosshairBottomLeft.x + reticleSize, crosshairBottomLeft.y, 0f));
|
||||
Handles.DrawLine(crosshairBottomLeft,
|
||||
new Vector3(crosshairBottomLeft.x, crosshairBottomLeft.y + reticleSize, 0f));
|
||||
// bottom right
|
||||
Handles.DrawLine(crosshairBottomRight,
|
||||
new Vector3(crosshairBottomRight.x - reticleSize, crosshairBottomRight.y, 0f));
|
||||
Handles.DrawLine(crosshairBottomRight,
|
||||
new Vector3(crosshairBottomRight.x, crosshairBottomRight.y + reticleSize, 0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,387 +5,371 @@ using UnityEngine;
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
/// <summary>
|
||||
/// a custom editor for the MMGizmo component
|
||||
/// a custom editor for the MMGizmo component
|
||||
/// </summary>
|
||||
[CustomEditor(typeof(MMGizmo), true)]
|
||||
[CanEditMultipleObjects]
|
||||
public class MMGizmoEditor : Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// Lets you press G when in scene view to toggle gizmos on or off
|
||||
/// </summary>
|
||||
[Shortcut("Toggle Gizmos", typeof(SceneView), KeyCode.G, displayName = "ToggleGizmos")]
|
||||
public static void ToggleGizmos()
|
||||
{
|
||||
SceneView.lastActiveSceneView.drawGizmos = !SceneView.lastActiveSceneView.drawGizmos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When the target object is selected, we draw our gizmos
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="gizmoType"></param>
|
||||
[DrawGizmo(GizmoType.Selected)]
|
||||
private static void DrawGizmoSelected(MMGizmo mmGizmo, GizmoType gizmoType)
|
||||
{
|
||||
if (!mmGizmo.DisplayGizmo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
DrawGizmos(mmGizmo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When the target object is not selected, we draw our gizmos if needed
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="gizmoType"></param>
|
||||
[DrawGizmo(GizmoType.NonSelected)]
|
||||
private static void DrawGizmoNonSelected(MMGizmo mmGizmo, GizmoType gizmoType)
|
||||
{
|
||||
if (!mmGizmo.DisplayGizmo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (mmGizmo.DisplayMode != MMGizmo.DisplayModes.Always)
|
||||
{
|
||||
return;
|
||||
}
|
||||
DrawGizmos(mmGizmo);
|
||||
}
|
||||
[CanEditMultipleObjects]
|
||||
public class MMGizmoEditor : Editor
|
||||
{
|
||||
/// <summary>
|
||||
/// On Enable we initialize our gizmo
|
||||
/// </summary>
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
Initialization(target as MMGizmo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws gizmos and text
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawGizmos(MMGizmo mmGizmo)
|
||||
{
|
||||
if (!mmGizmo.Initialized)
|
||||
{
|
||||
Initialization(mmGizmo);
|
||||
}
|
||||
|
||||
if (TestDistance(mmGizmo, mmGizmo.ViewDistance))
|
||||
{
|
||||
Gizmos.color = mmGizmo.GizmoColor;
|
||||
Gizmos.matrix = mmGizmo.transform.localToWorldMatrix;
|
||||
|
||||
switch (mmGizmo.GizmoType)
|
||||
{
|
||||
case MMGizmo.GizmoTypes.Collider:
|
||||
DrawColliderGizmo(mmGizmo);
|
||||
break;
|
||||
case MMGizmo.GizmoTypes.Position:
|
||||
DrawPositionGizmo(mmGizmo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
DrawText(mmGizmo);
|
||||
}
|
||||
/// <summary>
|
||||
/// On validate we initialize our gizmo
|
||||
/// </summary>
|
||||
protected void OnValidate()
|
||||
{
|
||||
Initialization(target as MMGizmo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests whether or not gizmos should be drawn based on distance to the scene camera
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="viewDistance"></param>
|
||||
/// <returns></returns>
|
||||
private static bool TestDistance(MMGizmo mmGizmo, float viewDistance)
|
||||
{
|
||||
float distanceToCamera = 0f;
|
||||
|
||||
if (SceneView.currentDrawingSceneView == null)
|
||||
{
|
||||
distanceToCamera = Vector3.Distance(mmGizmo.transform.position, Camera.main.transform.position);
|
||||
return (distanceToCamera < viewDistance);
|
||||
}
|
||||
else
|
||||
{
|
||||
distanceToCamera = Vector3.Distance(mmGizmo.transform.position, SceneView.currentDrawingSceneView.camera.transform.position);
|
||||
return (distanceToCamera < viewDistance);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On Enable we initialize our gizmo
|
||||
/// </summary>
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
Initialization(target as MMGizmo);
|
||||
}
|
||||
/// <summary>
|
||||
/// Lets you press G when in scene view to toggle gizmos on or off
|
||||
/// </summary>
|
||||
[Shortcut("Toggle Gizmos", typeof(SceneView), KeyCode.G, displayName = "ToggleGizmos")]
|
||||
public static void ToggleGizmos()
|
||||
{
|
||||
SceneView.lastActiveSceneView.drawGizmos = !SceneView.lastActiveSceneView.drawGizmos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On validate we initialize our gizmo
|
||||
/// </summary>
|
||||
protected void OnValidate()
|
||||
{
|
||||
Initialization(target as MMGizmo);
|
||||
}
|
||||
/// <summary>
|
||||
/// When the target object is selected, we draw our gizmos
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="gizmoType"></param>
|
||||
[DrawGizmo(GizmoType.Selected)]
|
||||
private static void DrawGizmoSelected(MMGizmo mmGizmo, GizmoType gizmoType)
|
||||
{
|
||||
if (!mmGizmo.DisplayGizmo) return;
|
||||
DrawGizmos(mmGizmo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the gizmo, caching components, values, and inits the GUIStyle
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void Initialization(MMGizmo mmGizmo)
|
||||
{
|
||||
mmGizmo._sphereCollider = mmGizmo.gameObject.GetComponent<SphereCollider>();
|
||||
mmGizmo._boxCollider = mmGizmo.gameObject.GetComponent<BoxCollider>();
|
||||
mmGizmo._meshCollider = mmGizmo.gameObject.GetComponent<MeshCollider>();
|
||||
#if MM_PHYSICS2D
|
||||
mmGizmo._circleCollider2D = mmGizmo.gameObject.GetComponent<CircleCollider2D>();
|
||||
mmGizmo._boxCollider2D = mmGizmo.gameObject.GetComponent<BoxCollider2D>();
|
||||
#endif
|
||||
/// <summary>
|
||||
/// When the target object is not selected, we draw our gizmos if needed
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="gizmoType"></param>
|
||||
[DrawGizmo(GizmoType.NonSelected)]
|
||||
private static void DrawGizmoNonSelected(MMGizmo mmGizmo, GizmoType gizmoType)
|
||||
{
|
||||
if (!mmGizmo.DisplayGizmo) return;
|
||||
if (mmGizmo.DisplayMode != MMGizmo.DisplayModes.Always) return;
|
||||
DrawGizmos(mmGizmo);
|
||||
}
|
||||
|
||||
mmGizmo._sphereColliderNotNull = (mmGizmo._sphereCollider != null);
|
||||
mmGizmo._boxColliderNotNull = (mmGizmo._boxCollider != null);
|
||||
mmGizmo._meshColliderNotNull = (mmGizmo._meshCollider != null);
|
||||
#if MM_PHYSICS2D
|
||||
mmGizmo._circleCollider2DNotNull = (mmGizmo._circleCollider2D != null);
|
||||
mmGizmo._boxCollider2DNotNull = (mmGizmo._boxCollider2D != null);
|
||||
#endif
|
||||
/// <summary>
|
||||
/// Draws gizmos and text
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawGizmos(MMGizmo mmGizmo)
|
||||
{
|
||||
if (!mmGizmo.Initialized) Initialization(mmGizmo);
|
||||
|
||||
mmGizmo._vector3Zero = Vector3.zero;
|
||||
mmGizmo._textureRect = new Rect(0f, 0f, mmGizmo.TextureSize.x, mmGizmo.TextureSize.y);
|
||||
mmGizmo._positionTextureNotNull = (mmGizmo.PositionTexture != null);
|
||||
if (TestDistance(mmGizmo, mmGizmo.ViewDistance))
|
||||
{
|
||||
Gizmos.color = mmGizmo.GizmoColor;
|
||||
Gizmos.matrix = mmGizmo.transform.localToWorldMatrix;
|
||||
|
||||
mmGizmo._textGUIStyle = new GUIStyle();
|
||||
mmGizmo._textGUIStyle.normal.textColor = mmGizmo.TextColor;
|
||||
mmGizmo._textGUIStyle.fontSize = mmGizmo.TextSize;
|
||||
mmGizmo._textGUIStyle.fontStyle = mmGizmo.TextFontStyle;
|
||||
mmGizmo._textGUIStyle.padding = new RectOffset((int)mmGizmo.TextPadding.x, (int)mmGizmo.TextPadding.y, (int)mmGizmo.TextPadding.z, (int)mmGizmo.TextPadding.w);
|
||||
mmGizmo._textGUIStyle.normal.background = MMGUI.MakeTex(600, 100, mmGizmo.TextBackgroundColor);
|
||||
switch (mmGizmo.GizmoType)
|
||||
{
|
||||
case MMGizmo.GizmoTypes.Collider:
|
||||
DrawColliderGizmo(mmGizmo);
|
||||
break;
|
||||
case MMGizmo.GizmoTypes.Position:
|
||||
DrawPositionGizmo(mmGizmo);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mmGizmo.Initialized = true;
|
||||
}
|
||||
DrawText(mmGizmo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a gizmo for the associated collider
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawColliderGizmo(MMGizmo mmGizmo)
|
||||
{
|
||||
if (mmGizmo._sphereColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
{
|
||||
Gizmos.DrawSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._sphereCollider.center), mmGizmo._sphereCollider.radius);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.DrawWireSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._sphereCollider.center), mmGizmo._sphereCollider.radius);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Tests whether or not gizmos should be drawn based on distance to the scene camera
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="viewDistance"></param>
|
||||
/// <returns></returns>
|
||||
private static bool TestDistance(MMGizmo mmGizmo, float viewDistance)
|
||||
{
|
||||
var distanceToCamera = 0f;
|
||||
|
||||
if (mmGizmo._boxColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
{
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider.center), mmGizmo._boxCollider.size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider.center), mmGizmo._boxCollider.size);
|
||||
}
|
||||
}
|
||||
|
||||
#if MM_PHYSICS2D
|
||||
if (mmGizmo._circleCollider2DNotNull)
|
||||
{
|
||||
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
{
|
||||
Gizmos.DrawSphere((Vector3)ComputeGizmoPosition(mmGizmo, mmGizmo._circleCollider2D.offset), mmGizmo._circleCollider2D.radius);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.DrawWireSphere((Vector3)ComputeGizmoPosition(mmGizmo, mmGizmo._circleCollider2D.offset), mmGizmo._circleCollider2D.radius);
|
||||
}
|
||||
}
|
||||
if (SceneView.currentDrawingSceneView == null)
|
||||
{
|
||||
distanceToCamera = Vector3.Distance(mmGizmo.transform.position, Camera.main.transform.position);
|
||||
return distanceToCamera < viewDistance;
|
||||
}
|
||||
|
||||
if (mmGizmo._boxCollider2DNotNull)
|
||||
{
|
||||
Vector3 gizmoSize = new Vector3();
|
||||
gizmoSize.x = mmGizmo._boxCollider2D.size.x ;
|
||||
gizmoSize.y = mmGizmo._boxCollider2D.size.y ;
|
||||
gizmoSize.z = 0.1f;
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
{
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider2D.offset), gizmoSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider2D.offset), gizmoSize);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
distanceToCamera = Vector3.Distance(mmGizmo.transform.position,
|
||||
SceneView.currentDrawingSceneView.camera.transform.position);
|
||||
return distanceToCamera < viewDistance;
|
||||
}
|
||||
|
||||
if (mmGizmo._meshColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
{
|
||||
Gizmos.DrawMesh(mmGizmo._meshCollider.sharedMesh);
|
||||
}
|
||||
else
|
||||
{
|
||||
Gizmos.DrawWireMesh(mmGizmo._meshCollider.sharedMesh);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes the gizmo, caching components, values, and inits the GUIStyle
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void Initialization(MMGizmo mmGizmo)
|
||||
{
|
||||
mmGizmo._sphereCollider = mmGizmo.gameObject.GetComponent<SphereCollider>();
|
||||
mmGizmo._boxCollider = mmGizmo.gameObject.GetComponent<BoxCollider>();
|
||||
mmGizmo._meshCollider = mmGizmo.gameObject.GetComponent<MeshCollider>();
|
||||
#if MM_PHYSICS2D
|
||||
mmGizmo._circleCollider2D = mmGizmo.gameObject.GetComponent<CircleCollider2D>();
|
||||
mmGizmo._boxCollider2D = mmGizmo.gameObject.GetComponent<BoxCollider2D>();
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Draws a position gizmo
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawPositionGizmo(MMGizmo mmGizmo)
|
||||
{
|
||||
switch (mmGizmo.PositionMode)
|
||||
{
|
||||
case MMGizmo.PositionModes.Point:
|
||||
MMDebug.DrawGizmoPoint(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.GizmoColor, mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Cube:
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), Vector3.one * mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Sphere:
|
||||
Gizmos.DrawSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.WireCube:
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), Vector3.one * mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.WireSphere:
|
||||
Gizmos.DrawWireSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Texture:
|
||||
if (mmGizmo._positionTextureNotNull)
|
||||
{
|
||||
Handles.BeginGUI();
|
||||
mmGizmo._worldToGUIPosition = HandleUtility.WorldToGUIPoint(ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false));
|
||||
mmGizmo._textureRect = new Rect(mmGizmo._worldToGUIPosition.x - mmGizmo.TextureSize.x/2f, mmGizmo._worldToGUIPosition.y - mmGizmo.TextureSize.y/2f, mmGizmo.TextureSize.x, mmGizmo.TextureSize.y);
|
||||
GUI.Label(mmGizmo._textureRect, mmGizmo.PositionTexture);
|
||||
Handles.EndGUI();
|
||||
}
|
||||
break;
|
||||
case MMGizmo.PositionModes.Arrows:
|
||||
Handles.color = Handles.xAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.right, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
Handles.color = Handles.yAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.up, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
Handles.color = Handles.zAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.forward, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.RightArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.right, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.UpArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.up, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.ForwardArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.forward, mmGizmo.transform.up), mmGizmo.PositionSize, EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Lines:
|
||||
Vector3 origin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
Vector3 destination = origin + Vector3.right * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
destination = origin + Vector3.up * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
destination = origin + Vector3.forward * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.RightLine:
|
||||
Vector3 rightOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
Vector3 rightDestination = rightOrigin + Vector3.right * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(rightOrigin, rightDestination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.UpLine:
|
||||
Vector3 upOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
Vector3 upDestination = upOrigin + Vector3.up * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(upOrigin, upDestination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.ForwardLine:
|
||||
Vector3 fwdOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
Vector3 fwdDestination = fwdOrigin + Vector3.forward * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(fwdOrigin, fwdDestination);
|
||||
break;
|
||||
}
|
||||
}
|
||||
mmGizmo._sphereColliderNotNull = mmGizmo._sphereCollider != null;
|
||||
mmGizmo._boxColliderNotNull = mmGizmo._boxCollider != null;
|
||||
mmGizmo._meshColliderNotNull = mmGizmo._meshCollider != null;
|
||||
#if MM_PHYSICS2D
|
||||
mmGizmo._circleCollider2DNotNull = mmGizmo._circleCollider2D != null;
|
||||
mmGizmo._boxCollider2DNotNull = mmGizmo._boxCollider2D != null;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Draws our gizmo text
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawText(MMGizmo mmGizmo)
|
||||
{
|
||||
if (!mmGizmo.DisplayText)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TestDistance(mmGizmo, mmGizmo.TextMaxDistance))
|
||||
{
|
||||
return;
|
||||
}
|
||||
mmGizmo._vector3Zero = Vector3.zero;
|
||||
mmGizmo._textureRect = new Rect(0f, 0f, mmGizmo.TextureSize.x, mmGizmo.TextureSize.y);
|
||||
mmGizmo._positionTextureNotNull = mmGizmo.PositionTexture != null;
|
||||
|
||||
switch (mmGizmo.TextMode)
|
||||
{
|
||||
case MMGizmo.TextModes.GameObjectName:
|
||||
mmGizmo._textToDisplay = mmGizmo.gameObject.name;
|
||||
break;
|
||||
case MMGizmo.TextModes.CustomText:
|
||||
mmGizmo._textToDisplay = mmGizmo.TextToDisplay;
|
||||
break;
|
||||
case MMGizmo.TextModes.Position:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.position.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Rotation:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.rotation.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Scale:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.localScale.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Property:
|
||||
if (mmGizmo.TargetProperty.PropertyFound)
|
||||
{
|
||||
mmGizmo._textToDisplay = mmGizmo.TargetProperty.GetRawValue().ToString();
|
||||
}
|
||||
break;
|
||||
}
|
||||
mmGizmo._textGUIStyle = new GUIStyle();
|
||||
mmGizmo._textGUIStyle.normal.textColor = mmGizmo.TextColor;
|
||||
mmGizmo._textGUIStyle.fontSize = mmGizmo.TextSize;
|
||||
mmGizmo._textGUIStyle.fontStyle = mmGizmo.TextFontStyle;
|
||||
mmGizmo._textGUIStyle.padding = new RectOffset((int)mmGizmo.TextPadding.x, (int)mmGizmo.TextPadding.y,
|
||||
(int)mmGizmo.TextPadding.z, (int)mmGizmo.TextPadding.w);
|
||||
mmGizmo._textGUIStyle.normal.background = MMGUI.MakeTex(600, 100, mmGizmo.TextBackgroundColor);
|
||||
|
||||
if (mmGizmo._textToDisplay != "")
|
||||
{
|
||||
Handles.Label(mmGizmo.transform.position + mmGizmo.TextOffset, mmGizmo._textToDisplay, mmGizmo._textGUIStyle);
|
||||
}
|
||||
}
|
||||
mmGizmo.Initialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the position at which to draw the gizmo
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="relativeLock"></param>
|
||||
/// <returns></returns>
|
||||
private static Vector3 ComputeGizmoPosition(MMGizmo mmGizmo, Vector3 position, bool relativeLock = true)
|
||||
{
|
||||
mmGizmo._newPosition = position + mmGizmo.GizmoOffset;
|
||||
/// <summary>
|
||||
/// Draws a gizmo for the associated collider
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawColliderGizmo(MMGizmo mmGizmo)
|
||||
{
|
||||
if (mmGizmo._sphereColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
Gizmos.DrawSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._sphereCollider.center),
|
||||
mmGizmo._sphereCollider.radius);
|
||||
else
|
||||
Gizmos.DrawWireSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._sphereCollider.center),
|
||||
mmGizmo._sphereCollider.radius);
|
||||
}
|
||||
|
||||
if (mmGizmo.LockX || mmGizmo.LockY || mmGizmo.LockZ)
|
||||
{
|
||||
Vector3 mmGizmoNewPosition = mmGizmo._newPosition;
|
||||
if (mmGizmo.LockX) { mmGizmoNewPosition.x = relativeLock ? - mmGizmo.transform.position.x + mmGizmo.LockedX : mmGizmo.LockedX; }
|
||||
if (mmGizmo.LockY) { mmGizmoNewPosition.y = relativeLock ? - mmGizmo.transform.position.y + mmGizmo.LockedY : mmGizmo.LockedY; }
|
||||
if (mmGizmo.LockZ) { mmGizmoNewPosition.z = relativeLock ? - mmGizmo.transform.position.z + mmGizmo.LockedZ : mmGizmo.LockedZ; }
|
||||
mmGizmo._newPosition = mmGizmoNewPosition;
|
||||
}
|
||||
if (mmGizmo._boxColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider.center),
|
||||
mmGizmo._boxCollider.size);
|
||||
else
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider.center),
|
||||
mmGizmo._boxCollider.size);
|
||||
}
|
||||
|
||||
return mmGizmo._newPosition;
|
||||
}
|
||||
|
||||
}
|
||||
#if MM_PHYSICS2D
|
||||
if (mmGizmo._circleCollider2DNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
Gizmos.DrawSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._circleCollider2D.offset),
|
||||
mmGizmo._circleCollider2D.radius);
|
||||
else
|
||||
Gizmos.DrawWireSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._circleCollider2D.offset),
|
||||
mmGizmo._circleCollider2D.radius);
|
||||
}
|
||||
|
||||
if (mmGizmo._boxCollider2DNotNull)
|
||||
{
|
||||
var gizmoSize = new Vector3();
|
||||
gizmoSize.x = mmGizmo._boxCollider2D.size.x;
|
||||
gizmoSize.y = mmGizmo._boxCollider2D.size.y;
|
||||
gizmoSize.z = 0.1f;
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider2D.offset), gizmoSize);
|
||||
else
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._boxCollider2D.offset), gizmoSize);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (mmGizmo._meshColliderNotNull)
|
||||
{
|
||||
if (mmGizmo.ColliderRenderType == MMGizmo.ColliderRenderTypes.Full)
|
||||
Gizmos.DrawMesh(mmGizmo._meshCollider.sharedMesh);
|
||||
else
|
||||
Gizmos.DrawWireMesh(mmGizmo._meshCollider.sharedMesh);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a position gizmo
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawPositionGizmo(MMGizmo mmGizmo)
|
||||
{
|
||||
switch (mmGizmo.PositionMode)
|
||||
{
|
||||
case MMGizmo.PositionModes.Point:
|
||||
MMDebug.DrawGizmoPoint(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.GizmoColor,
|
||||
mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Cube:
|
||||
Gizmos.DrawCube(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero),
|
||||
Vector3.one * mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Sphere:
|
||||
Gizmos.DrawSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.WireCube:
|
||||
Gizmos.DrawWireCube(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero),
|
||||
Vector3.one * mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.WireSphere:
|
||||
Gizmos.DrawWireSphere(ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero), mmGizmo.PositionSize);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Texture:
|
||||
if (mmGizmo._positionTextureNotNull)
|
||||
{
|
||||
Handles.BeginGUI();
|
||||
mmGizmo._worldToGUIPosition =
|
||||
HandleUtility.WorldToGUIPoint(ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position,
|
||||
false));
|
||||
mmGizmo._textureRect = new Rect(mmGizmo._worldToGUIPosition.x - mmGizmo.TextureSize.x / 2f,
|
||||
mmGizmo._worldToGUIPosition.y - mmGizmo.TextureSize.y / 2f, mmGizmo.TextureSize.x,
|
||||
mmGizmo.TextureSize.y);
|
||||
GUI.Label(mmGizmo._textureRect, mmGizmo.PositionTexture);
|
||||
Handles.EndGUI();
|
||||
}
|
||||
|
||||
break;
|
||||
case MMGizmo.PositionModes.Arrows:
|
||||
Handles.color = Handles.xAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.right, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
Handles.color = Handles.yAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.up, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
Handles.color = Handles.zAxisColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.forward, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.RightArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.right, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.UpArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.up, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.ForwardArrow:
|
||||
Handles.color = mmGizmo.GizmoColor;
|
||||
Handles.ArrowHandleCap(0, ComputeGizmoPosition(mmGizmo, mmGizmo.transform.position, false),
|
||||
Quaternion.LookRotation(mmGizmo.transform.forward, mmGizmo.transform.up), mmGizmo.PositionSize,
|
||||
EventType.Repaint);
|
||||
break;
|
||||
case MMGizmo.PositionModes.Lines:
|
||||
var origin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
var destination = origin + Vector3.right * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
destination = origin + Vector3.up * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
destination = origin + Vector3.forward * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(origin, destination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.RightLine:
|
||||
var rightOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
var rightDestination = rightOrigin + Vector3.right * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(rightOrigin, rightDestination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.UpLine:
|
||||
var upOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
var upDestination = upOrigin + Vector3.up * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(upOrigin, upDestination);
|
||||
break;
|
||||
case MMGizmo.PositionModes.ForwardLine:
|
||||
var fwdOrigin = ComputeGizmoPosition(mmGizmo, mmGizmo._vector3Zero);
|
||||
var fwdDestination = fwdOrigin + Vector3.forward * mmGizmo.PositionSize;
|
||||
Gizmos.DrawLine(fwdOrigin, fwdDestination);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws our gizmo text
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
private static void DrawText(MMGizmo mmGizmo)
|
||||
{
|
||||
if (!mmGizmo.DisplayText) return;
|
||||
|
||||
if (!TestDistance(mmGizmo, mmGizmo.TextMaxDistance)) return;
|
||||
|
||||
switch (mmGizmo.TextMode)
|
||||
{
|
||||
case MMGizmo.TextModes.GameObjectName:
|
||||
mmGizmo._textToDisplay = mmGizmo.gameObject.name;
|
||||
break;
|
||||
case MMGizmo.TextModes.CustomText:
|
||||
mmGizmo._textToDisplay = mmGizmo.TextToDisplay;
|
||||
break;
|
||||
case MMGizmo.TextModes.Position:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.position.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Rotation:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.rotation.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Scale:
|
||||
mmGizmo._textToDisplay = mmGizmo.transform.localScale.ToString();
|
||||
break;
|
||||
case MMGizmo.TextModes.Property:
|
||||
if (mmGizmo.TargetProperty.PropertyFound)
|
||||
mmGizmo._textToDisplay = mmGizmo.TargetProperty.GetRawValue().ToString();
|
||||
break;
|
||||
}
|
||||
|
||||
if (mmGizmo._textToDisplay != "")
|
||||
Handles.Label(mmGizmo.transform.position + mmGizmo.TextOffset, mmGizmo._textToDisplay,
|
||||
mmGizmo._textGUIStyle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the position at which to draw the gizmo
|
||||
/// </summary>
|
||||
/// <param name="mmGizmo"></param>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="relativeLock"></param>
|
||||
/// <returns></returns>
|
||||
private static Vector3 ComputeGizmoPosition(MMGizmo mmGizmo, Vector3 position, bool relativeLock = true)
|
||||
{
|
||||
mmGizmo._newPosition = position + mmGizmo.GizmoOffset;
|
||||
|
||||
if (mmGizmo.LockX || mmGizmo.LockY || mmGizmo.LockZ)
|
||||
{
|
||||
var mmGizmoNewPosition = mmGizmo._newPosition;
|
||||
if (mmGizmo.LockX)
|
||||
mmGizmoNewPosition.x =
|
||||
relativeLock ? -mmGizmo.transform.position.x + mmGizmo.LockedX : mmGizmo.LockedX;
|
||||
if (mmGizmo.LockY)
|
||||
mmGizmoNewPosition.y =
|
||||
relativeLock ? -mmGizmo.transform.position.y + mmGizmo.LockedY : mmGizmo.LockedY;
|
||||
if (mmGizmo.LockZ)
|
||||
mmGizmoNewPosition.z =
|
||||
relativeLock ? -mmGizmo.transform.position.z + mmGizmo.LockedZ : mmGizmo.LockedZ;
|
||||
mmGizmo._newPosition = mmGizmoNewPosition;
|
||||
}
|
||||
|
||||
return mmGizmo._newPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,70 +3,64 @@
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Tools
|
||||
{
|
||||
public class MMFindMissingScriptsRecursively : EditorWindow
|
||||
{
|
||||
static int go_count = 0, components_count = 0, missing_count = 0;
|
||||
|
||||
[MenuItem("Tools/More Mountains/Find missing scripts recursively", false, 505)]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
EditorWindow.GetWindow(typeof(MMFindMissingScriptsRecursively));
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public void OnGUI()
|
||||
{
|
||||
if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
|
||||
{
|
||||
FindInSelected();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private static void FindInSelected()
|
||||
{
|
||||
GameObject[] go = Selection.gameObjects;
|
||||
go_count = 0;
|
||||
components_count = 0;
|
||||
missing_count = 0;
|
||||
foreach (GameObject g in go)
|
||||
{
|
||||
FindInGO(g);
|
||||
}
|
||||
Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
|
||||
}
|
||||
|
||||
private static void FindInGO(GameObject g)
|
||||
{
|
||||
go_count++;
|
||||
Component[] components = g.GetComponents<Component>();
|
||||
for (int i = 0; i < components.Length; i++)
|
||||
{
|
||||
components_count++;
|
||||
if (components[i] == null)
|
||||
{
|
||||
missing_count++;
|
||||
string s = g.name;
|
||||
Transform t = g.transform;
|
||||
while (t.parent != null)
|
||||
{
|
||||
s = t.parent.name +"/"+s;
|
||||
t = t.parent;
|
||||
}
|
||||
Debug.Log (s + " has an empty script attached in position: " + i, g);
|
||||
}
|
||||
}
|
||||
// Now recurse through each child GO (if there are any):
|
||||
foreach (Transform childT in g.transform)
|
||||
{
|
||||
FindInGO(childT.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
public class MMFindMissingScriptsRecursively : EditorWindow
|
||||
{
|
||||
private static int go_count, components_count, missing_count;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public void OnGUI()
|
||||
{
|
||||
if (GUILayout.Button("Find Missing Scripts in selected GameObjects")) FindInSelected();
|
||||
}
|
||||
#endif
|
||||
|
||||
[MenuItem("Tools/More Mountains/Find missing scripts recursively", false, 505)]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
GetWindow(typeof(MMFindMissingScriptsRecursively));
|
||||
}
|
||||
|
||||
private static void FindInSelected()
|
||||
{
|
||||
var go = Selection.gameObjects;
|
||||
go_count = 0;
|
||||
components_count = 0;
|
||||
missing_count = 0;
|
||||
foreach (var g in go) FindInGO(g);
|
||||
Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count,
|
||||
components_count, missing_count));
|
||||
}
|
||||
|
||||
private static void FindInGO(GameObject g)
|
||||
{
|
||||
go_count++;
|
||||
var components = g.GetComponents<Component>();
|
||||
for (var i = 0; i < components.Length; i++)
|
||||
{
|
||||
components_count++;
|
||||
if (components[i] == null)
|
||||
{
|
||||
missing_count++;
|
||||
var s = g.name;
|
||||
var t = g.transform;
|
||||
while (t.parent != null)
|
||||
{
|
||||
s = t.parent.name + "/" + s;
|
||||
t = t.parent;
|
||||
}
|
||||
|
||||
Debug.Log(s + " has an empty script attached in position: " + i, g);
|
||||
}
|
||||
}
|
||||
|
||||
// Now recurse through each child GO (if there are any):
|
||||
foreach (Transform childT in g.transform) FindInGO(childT.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"reference": "GUID:d9dbf313afb206f458581847ac758375"
|
||||
"reference": "GUID:d9dbf313afb206f458581847ac758375"
|
||||
}
|
||||
Reference in New Issue
Block a user