Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEditor.IMGUI.Controls;
  4.  
  5. [CustomEditor(typeof(Perception))]
  6. public class PerceptionEditor : Editor
  7. {
  8.     private ArcHandle arcHandle = new ArcHandle();
  9.  
  10.     private void OnEnable()
  11.     {
  12.         arcHandle.fillColor = new Color(0f, 1f, 0f, 0.25f);
  13.         arcHandle.wireframeColor = arcHandle.fillColor;
  14.         arcHandle.angleHandleColor = Color.red;
  15.         arcHandle.radiusHandleColor = Color.red;
  16.     }
  17.  
  18.     private void OnSceneGUI()
  19.     {
  20.         if (!target)
  21.         { return; }
  22.  
  23.         Color oldColor = Handles.color;
  24.         Perception perception = target as Perception;
  25.  
  26.         float halfFOV = perception.fovAngle * 0.5f;
  27.         float coneDirection = -90f;
  28.         Quaternion leftRayRotation = Quaternion.AngleAxis(-halfFOV + coneDirection, Vector3.up);
  29.         Quaternion rightRayRotation = Quaternion.AngleAxis(halfFOV + coneDirection, Vector3.up);
  30.  
  31.         Vector3 leftRayDirection = leftRayRotation * perception.transform.right * perception.perceptionRadius;
  32.         Vector3 rightRayDirection = rightRayRotation * perception.transform.right * perception.perceptionRadius;
  33.  
  34.         arcHandle.angle = perception.fovAngle;
  35.         arcHandle.radius = perception.perceptionRadius;
  36.  
  37.         Vector3 handleDirection = leftRayDirection;
  38.         Vector3 handleNormal = Vector3.Cross(handleDirection, Vector3.forward);
  39.         Matrix4x4 handleMatrix = Matrix4x4.TRS(
  40.             perception.transform.position,
  41.             Quaternion.LookRotation(handleDirection, handleNormal),
  42.             Vector3.one
  43.             );
  44.  
  45.         using (new Handles.DrawingScope(handleMatrix))
  46.         {
  47.             EditorGUI.BeginChangeCheck();
  48.             arcHandle.DrawHandle();
  49.             if (EditorGUI.EndChangeCheck())
  50.             {
  51.                 Undo.RecordObject(perception, "Change perception properties");
  52.  
  53.                 perception.fovAngle = Mathf.Clamp(arcHandle.angle, 0f, 359.999f);
  54.                 perception.perceptionRadius = Mathf.Max(arcHandle.radius, 1f);
  55.             }
  56.         }
  57.  
  58.         Handles.color = new Color(1f, 0f, 0f, 0.25f);
  59.         Handles.DrawSolidArc(perception.transform.position, Vector3.up, rightRayDirection, 360f - perception.fovAngle, perception.perceptionRadius);
  60.  
  61.         Handles.color = oldColor;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement