/// <summary> /// Convenience function to get an opposing handle /// This could be done by assigning an inspector value but this is cleaner /// </summary> /// <param name="type"></param> /// <returns></returns> public static HandleTypeFlattenedEnum GetOpposingHandle(HandleTypeFlattenedEnum type) { switch (type) { case HandleTypeFlattenedEnum.Drag: default: // Only type with no opposite return(HandleTypeFlattenedEnum.Drag); case HandleTypeFlattenedEnum.Scale_LB: return(HandleTypeFlattenedEnum.Scale_RT); case HandleTypeFlattenedEnum.Scale_LT: return(HandleTypeFlattenedEnum.Scale_RB); case HandleTypeFlattenedEnum.Scale_RT: return(HandleTypeFlattenedEnum.Scale_LB); case HandleTypeFlattenedEnum.Scale_RB: return(HandleTypeFlattenedEnum.Scale_LT); case HandleTypeFlattenedEnum.Rotate_LB_LT: return(HandleTypeFlattenedEnum.Rotate_RT_RB); case HandleTypeFlattenedEnum.Rotate_LT_RT: return(HandleTypeFlattenedEnum.Rotate_RB_LB); case HandleTypeFlattenedEnum.Rotate_RB_LB: return(HandleTypeFlattenedEnum.Rotate_LT_RT); case HandleTypeFlattenedEnum.Rotate_RT_RB: return(HandleTypeFlattenedEnum.Rotate_LB_LT); } }
void OnDrawGizmos() { if (Application.isPlaying) { return; } if (HandleTypeFlattened == HandleTypeFlattenedEnum.Drag) { HandleType = HandleTypeEnum.Drag; return; } if (UnityEditor.Selection.activeGameObject == gameObject) { BoundingBoxHandle[] bbhs = transform.parent.GetComponentsInChildren <BoundingBoxHandle>(); if (HandleTypeFlattened != HandleTypeFlattenedEnum.None) { HandleType = HandleTypeEnum.None; name = HandleTypeFlattened.ToString(); HandleTypeFlattenedEnum oppositeHandle = GetOpposingHandle(HandleTypeFlattened); foreach (BoundingBoxHandle bbh in bbhs) { if (bbh.HandleTypeFlattened == oppositeHandle) { Gizmos.color = Color.red; Gizmos.DrawSphere(transform.position, 0.025f); Gizmos.DrawLine(transform.position, bbh.transform.position); break; } } transform.localPosition = GetHandlePositionFromType(HandleTypeFlattened, BoundsExtentions.Axis.Y); } else { name = HandleType.ToString(); HandleTypeEnum oppositeHandle = GetOpposingHandle(HandleType); foreach (BoundingBoxHandle bbh in bbhs) { if (bbh.HandleType == oppositeHandle) { Gizmos.color = Color.red; Gizmos.DrawSphere(transform.position, 0.025f); Gizmos.DrawLine(transform.position, bbh.transform.position); break; } } transform.localPosition = GetHandlePositionFromType(HandleType); } } }
/// <summary> /// Convenience function used to correctly place handles. /// Helps prevent prefab corruption. /// </summary> /// <param name="flattenedType"></param> /// <param name="axis"></param> /// <returns></returns> public static Vector3 GetHandlePositionFromType(HandleTypeFlattenedEnum flattenedType, BoundsExtentions.Axis axis) { float left = 0f; float right = 0f; switch (flattenedType) { case HandleTypeFlattenedEnum.None: default: break; case HandleTypeFlattenedEnum.Scale_LT: left = -0.5f; right = 0.5f; break; case HandleTypeFlattenedEnum.Scale_LB: left = -0.5f; right = -0.5f; break; case HandleTypeFlattenedEnum.Scale_RT: left = 0.5f; right = 0.5f; break; case HandleTypeFlattenedEnum.Scale_RB: left = 0.5f; right = -0.5f; break; case HandleTypeFlattenedEnum.Rotate_LT_RT: left = 0.0f; right = 0.5f; break; case HandleTypeFlattenedEnum.Rotate_RT_RB: left = 0.5f; right = 0.0f; break; case HandleTypeFlattenedEnum.Rotate_RB_LB: left = 0.0f; right = -0.5f; break; case HandleTypeFlattenedEnum.Rotate_LB_LT: left = -0.5f; right = 0.0f; break; } Vector3 newPos = Vector3.zero; switch (axis) { case BoundsExtentions.Axis.X: newPos.x = 0; newPos.y = left; newPos.z = right; break; case BoundsExtentions.Axis.Y: newPos.x = left; newPos.y = 0; newPos.z = right; break; case BoundsExtentions.Axis.Z: newPos.x = left; newPos.y = right; newPos.z = 0; break; } return(newPos); }