Created
May 18, 2021 19:26
-
-
Save mminer/09c0e6e5127e1c9678def1175efe4c64 to your computer and use it in GitHub Desktop.
Unity scene view handles to rotate an object around a circle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEditor.IMGUI.Controls; | |
using UnityEngine; | |
public class RotateAroundCircle : MonoBehaviour | |
{ | |
public Color color = Color.cyan; | |
public Vector3 pivot; | |
} | |
[CustomEditor(typeof(RotateAroundCircle))] | |
public class RotateAroundCircleEditor : Editor | |
{ | |
ArcHandle handle; | |
void OnEnable() | |
{ | |
var targetComponent = target as RotateAroundCircle; | |
if (targetComponent == null) | |
{ | |
return; | |
} | |
handle = new ArcHandle(); | |
handle.SetColorWithoutRadiusHandle(targetComponent.color, 0); | |
handle.wireframeColor = Color.clear; | |
} | |
void OnSceneGUI() | |
{ | |
var targetComponent = target as RotateAroundCircle; | |
if (targetComponent == null) | |
{ | |
return; | |
} | |
var transform = targetComponent.transform; | |
var position = transform.position; | |
var levelPivot = new Vector3(targetComponent.pivot.x, position.y, targetComponent.pivot.z); | |
var angle = Mathf.Atan2(position.x, position.z) * Mathf.Rad2Deg; | |
var radius = Vector3.Distance(levelPivot, position); | |
var matrix = Matrix4x4.TRS(levelPivot, Quaternion.identity, Vector3.one); | |
handle.angle = angle; | |
handle.radius = radius; | |
using (new Handles.DrawingScope(targetComponent.color, matrix)) | |
{ | |
EditorGUI.BeginChangeCheck(); | |
handle.DrawHandle(); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
transform.RotateAround(levelPivot, Vector3.up, handle.angle - angle); | |
} | |
Handles.DrawWireDisc(Vector3.zero, Vector3.up, radius); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment