Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using UnityEngine;
- using UnityEditor.IMGUI.Controls;
- [CustomEditor(typeof(Perception))]
- public class PerceptionEditor : Editor
- {
- private ArcHandle arcHandle = new ArcHandle();
- private void OnEnable()
- {
- arcHandle.fillColor = new Color(0f, 1f, 0f, 0.25f);
- arcHandle.wireframeColor = arcHandle.fillColor;
- arcHandle.angleHandleColor = Color.red;
- arcHandle.radiusHandleColor = Color.red;
- }
- private void OnSceneGUI()
- {
- if (!target)
- { return; }
- Color oldColor = Handles.color;
- Perception perception = target as Perception;
- float halfFOV = perception.fovAngle * 0.5f;
- float coneDirection = -90f;
- Quaternion leftRayRotation = Quaternion.AngleAxis(-halfFOV + coneDirection, Vector3.up);
- Quaternion rightRayRotation = Quaternion.AngleAxis(halfFOV + coneDirection, Vector3.up);
- Vector3 leftRayDirection = leftRayRotation * perception.transform.right * perception.perceptionRadius;
- Vector3 rightRayDirection = rightRayRotation * perception.transform.right * perception.perceptionRadius;
- arcHandle.angle = perception.fovAngle;
- arcHandle.radius = perception.perceptionRadius;
- Vector3 handleDirection = leftRayDirection;
- Vector3 handleNormal = Vector3.Cross(handleDirection, Vector3.forward);
- Matrix4x4 handleMatrix = Matrix4x4.TRS(
- perception.transform.position,
- Quaternion.LookRotation(handleDirection, handleNormal),
- Vector3.one
- );
- using (new Handles.DrawingScope(handleMatrix))
- {
- EditorGUI.BeginChangeCheck();
- arcHandle.DrawHandle();
- if (EditorGUI.EndChangeCheck())
- {
- Undo.RecordObject(perception, "Change perception properties");
- perception.fovAngle = Mathf.Clamp(arcHandle.angle, 0f, 359.999f);
- perception.perceptionRadius = Mathf.Max(arcHandle.radius, 1f);
- }
- }
- Handles.color = new Color(1f, 0f, 0f, 0.25f);
- Handles.DrawSolidArc(perception.transform.position, Vector3.up, rightRayDirection, 360f - perception.fovAngle, perception.perceptionRadius);
- Handles.color = oldColor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement