Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why when using editor script with handles with DrawSolidArc it's not drawing any arc ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by shamenraze1988, Mar 6, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    This is the editor script is in the Assets/Editor

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(DrawSolidArc))]
    6. public class DrawSolidArcEditor : Editor
    7. {
    8.    public float arrowSize = 1;
    9.  
    10.    void OnSceneGUI()
    11.    {
    12.        DrawSolidArc t = target as DrawSolidArc;
    13.  
    14.        Handles.color = Color.blue;
    15.        Handles.Label(t.transform.position + Vector3.up * 2,
    16.                             t.transform.position.ToString() + "\nShieldArea: " +
    17.                             t.shieldArea.ToString());
    18.  
    19.        Handles.BeginGUI();
    20.        GUILayout.BeginArea(new Rect(Screen.width - 100, Screen.height - 80, 90, 50));
    21.  
    22.        if (GUILayout.Button("Reset Area"))
    23.            t.shieldArea = 5;
    24.  
    25.        GUILayout.EndArea();
    26.        Handles.EndGUI();
    27.  
    28.        Handles.color = new Color(1, 1, 1, 0.2f);
    29.        Handles.DrawSolidArc(t.transform.position, t.transform.up, -t.transform.right,
    30.                                180, t.shieldArea);
    31.  
    32.        Handles.color = Color.white;
    33.        t.shieldArea = Handles.ScaleValueHandle(t.shieldArea,
    34.                        t.transform.position + t.transform.forward * t.shieldArea,
    35.                        t.transform.rotation, 1, Handles.ConeHandleCap, 1);
    36.    }
    37. }
    38.  
    And the mono script for example this script is attached to a simple 3d cube :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class DrawSolidArc : MonoBehaviour
    6. {
    7.    public float shieldArea = 5;
    8. }
    9.  
    but there is nothing around the cube. not also near it nothing. it should draw arch in the editor.

    Later I want to be able to use the mouse with a click down and drag to be able to change the arc size but for not it's not drawing any arc.

    I took the example from the unity official docs :

    https://docs.unity3d.com/540/Documentation/ScriptReference/Handles.DrawSolidArc.html

    I noticed now also for example that I don't see the gui button in the inspector of the object that the mono script is attached to :

    Code (csharp):
    1.  
    2. if (GUILayout.Button("Reset Area"))
    3.  
    not sure what's wrong with the editor script.


    This is working. I still wonder why the unity docs example didn't work but this is working and I don't need the editor script. The problem with this script is that I must to turn on the Gizmos in the editor and I don't want to make it turned on all the time. So it's working but not as I wanted.

    I want to draw the arc without the gizmos.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class DrawSolidArc : MonoBehaviour
    6. {
    7.    public Vector3 offset = Vector3.one;
    8.    public float fieldOfViewAngle = 20;
    9.    public float viewDistance = 4;
    10.    public bool usePhysics2D = false;
    11.  
    12.    public void OnDrawGizmos()
    13.    {
    14.        DrawLineOfSight(this.transform, offset, fieldOfViewAngle, viewDistance, usePhysics2D);
    15.    }
    16.    public static void DrawLineOfSight(Transform transform, Vector3 positionOffset, float fieldOfViewAngle, float viewDistance, bool usePhysics2D)
    17.    {
    18. #if UNITY_EDITOR
    19.        var oldColor = UnityEditor.Handles.color;
    20.        var color = Color.yellow;
    21.        color.a = 0.1f;
    22.        UnityEditor.Handles.color = color;
    23.        var halfFOV = fieldOfViewAngle * 0.5f;
    24.        var beginDirection = Quaternion.AngleAxis(-halfFOV, (usePhysics2D ? Vector3.forward : Vector3.up)) * (usePhysics2D ? transform.up : transform.forward);
    25.        UnityEditor.Handles.DrawSolidArc(transform.TransformPoint(positionOffset), (usePhysics2D ? transform.forward : transform.up), beginDirection, fieldOfViewAngle, viewDistance);
    26.        UnityEditor.Handles.color = oldColor;
    27. #endif
    28.    }
    29. }
    30.  
     
    Last edited: Mar 6, 2021