|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +namespace MccDev260.GizmoTool |
| 4 | +{ |
| 5 | +#if UNITY_EDITOR |
| 6 | + [ExecuteInEditMode] |
| 7 | + public class GizmoDrawer : MonoBehaviour |
| 8 | + { |
| 9 | + [SerializeField] private bool drawOnSelectOnly; |
| 10 | + |
| 11 | + #region Properties |
| 12 | + [HideInInspector] |
| 13 | + public enum GizmoType |
| 14 | + { |
| 15 | + Sphere, |
| 16 | + WireSphere, |
| 17 | + |
| 18 | + Cube, |
| 19 | + WireCube, |
| 20 | + |
| 21 | + Mesh, |
| 22 | + WireMesh, |
| 23 | + |
| 24 | + Line, |
| 25 | + LineList, |
| 26 | + LineStrip, |
| 27 | + |
| 28 | + Icon, |
| 29 | + GuiTexture, |
| 30 | + } |
| 31 | + |
| 32 | + //Shared |
| 33 | + [HideInInspector] public Transform originTransform; |
| 34 | + [HideInInspector] public Vector3 originPos; |
| 35 | + [HideInInspector] public GizmoType gizmoType; |
| 36 | + [HideInInspector] public Color gizmoColor = Color.blue; |
| 37 | + |
| 38 | + //Icon |
| 39 | + [HideInInspector] public string filePathString; |
| 40 | + [HideInInspector] public bool allowScaling; |
| 41 | + |
| 42 | + //Sphere |
| 43 | + [HideInInspector] public float floatRadius = 1f; |
| 44 | + //Cube |
| 45 | + [HideInInspector] public bool uniformScale; |
| 46 | + [HideInInspector] public Vector3 scale = Vector3.one; |
| 47 | + |
| 48 | + // Mesh |
| 49 | + [HideInInspector] public Mesh mesh; |
| 50 | + [HideInInspector] public Vector3 meshRotation; |
| 51 | + |
| 52 | + //Line |
| 53 | + [HideInInspector] public Transform targetTransform; |
| 54 | + [HideInInspector] public Vector3 targetPosition; |
| 55 | + |
| 56 | + //LineList & LineStrip |
| 57 | + [HideInInspector] public Vector3[] points = new Vector3[0]; |
| 58 | + |
| 59 | + [HideInInspector] public bool useTransformArray; |
| 60 | + [HideInInspector] public Transform[] transformPoints = new Transform[0]; |
| 61 | + |
| 62 | + [HideInInspector] public bool looped; |
| 63 | + |
| 64 | + // GUI Texture |
| 65 | + /// <summary> |
| 66 | + /// The size and position of the texture on the "screen" defined by the XY plane. |
| 67 | + /// </summary> |
| 68 | + [HideInInspector] public Rect screenRect; |
| 69 | + /// <summary> |
| 70 | + /// The texture to be displayed. |
| 71 | + /// </summary> |
| 72 | + [HideInInspector] public Texture texture; |
| 73 | + /// <summary> |
| 74 | + /// An optional material to apply the texture. |
| 75 | + /// </summary> |
| 76 | + [HideInInspector] public Material mat; |
| 77 | + #endregion |
| 78 | + |
| 79 | + private void OnDrawGizmosSelected() |
| 80 | + { |
| 81 | + if (drawOnSelectOnly) |
| 82 | + DrawGizmo(this); |
| 83 | + } |
| 84 | + |
| 85 | + private void OnDrawGizmos() |
| 86 | + { |
| 87 | + if (!drawOnSelectOnly) |
| 88 | + DrawGizmo(this); |
| 89 | + } |
| 90 | + |
| 91 | + static Vector3 LineTargetPosition(GizmoDrawer drawer) |
| 92 | + { |
| 93 | + if (drawer.targetTransform != null) |
| 94 | + { |
| 95 | + return drawer.targetTransform.position; |
| 96 | + } |
| 97 | + |
| 98 | + return drawer.targetPosition; |
| 99 | + } |
| 100 | + |
| 101 | + static Vector3 OriginPosition(GizmoDrawer drawer) |
| 102 | + { |
| 103 | + if (drawer.originTransform != null) |
| 104 | + { |
| 105 | + return drawer.originTransform.position; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + return drawer.originPos; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + static void SyncPositionArrays(GizmoDrawer drawer, out Vector3[] points, out Transform[] transforms) |
| 114 | + { |
| 115 | + points = drawer.points; |
| 116 | + transforms = drawer.transformPoints; |
| 117 | + |
| 118 | + if (transforms.Length > 0) |
| 119 | + { |
| 120 | + points = new Vector3[transforms.Length]; |
| 121 | + |
| 122 | + for (int i = 0; i < transforms.Length; i++) |
| 123 | + { |
| 124 | + if (transforms[i] != null) |
| 125 | + { |
| 126 | + points[i] = transforms[i].position; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + static void DrawGizmo(GizmoDrawer drawer) |
| 133 | + { |
| 134 | + drawer.originPos = OriginPosition(drawer); |
| 135 | + var origin = drawer.originPos; |
| 136 | + |
| 137 | + var scale = drawer.scale; |
| 138 | + |
| 139 | + switch (drawer.gizmoType) |
| 140 | + { |
| 141 | + case GizmoType.Sphere: |
| 142 | + GizmoUtil.DrawSphere(origin, drawer.floatRadius, drawer.gizmoColor); |
| 143 | + break; |
| 144 | + |
| 145 | + case GizmoType.Cube: |
| 146 | + GizmoUtil.DrawCube(origin, scale, drawer.gizmoColor); |
| 147 | + break; |
| 148 | + |
| 149 | + case GizmoType.WireSphere: |
| 150 | + GizmoUtil.DrawWireSphere(origin, drawer.floatRadius, drawer.gizmoColor); |
| 151 | + break; |
| 152 | + |
| 153 | + case GizmoType.WireCube: |
| 154 | + GizmoUtil.DrawWireCube(origin, scale, drawer.gizmoColor); |
| 155 | + break; |
| 156 | + |
| 157 | + case GizmoType.Icon: |
| 158 | + GizmoUtil.DrawIcon(origin, drawer.filePathString, drawer.allowScaling, drawer.gizmoColor); |
| 159 | + break; |
| 160 | + |
| 161 | + case GizmoType.Mesh: |
| 162 | + var rotation = Quaternion.Euler(drawer.meshRotation); |
| 163 | + GizmoUtil.DrawMesh(drawer.mesh, origin, rotation, scale, drawer.gizmoColor); |
| 164 | + break; |
| 165 | + |
| 166 | + case GizmoType.WireMesh: |
| 167 | + rotation = Quaternion.Euler(drawer.meshRotation); |
| 168 | + GizmoUtil.DrawWireMesh(drawer.mesh, origin, rotation, scale, drawer.gizmoColor); |
| 169 | + break; |
| 170 | + |
| 171 | + case GizmoType.Line: |
| 172 | + drawer.targetPosition = LineTargetPosition(drawer); |
| 173 | + GizmoUtil.DrawLine(origin, drawer.targetPosition, drawer.gizmoColor); |
| 174 | + break; |
| 175 | + |
| 176 | + case GizmoType.LineList or GizmoType.LineStrip: |
| 177 | + |
| 178 | + if (drawer.useTransformArray) |
| 179 | + { |
| 180 | + SyncPositionArrays(drawer, out drawer.points, out drawer.transformPoints); |
| 181 | + } |
| 182 | + |
| 183 | + if (drawer.gizmoType == GizmoType.LineStrip) |
| 184 | + GizmoUtil.DrawLineStrip(drawer.points, drawer.gizmoColor, drawer.looped); |
| 185 | + else |
| 186 | + GizmoUtil.DrawLineList(drawer.points, drawer.gizmoColor); |
| 187 | + break; |
| 188 | + |
| 189 | + case GizmoType.GuiTexture: |
| 190 | + if (drawer.texture != null) |
| 191 | + GizmoUtil.DrawGuiTexture(drawer.screenRect, drawer.texture, drawer.mat); |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +#endif |
| 197 | +} |
0 commit comments