Search Unity

UI Handles disappear when using OnSceneGui

Discussion in 'Immediate Mode GUI (IMGUI)' started by mate_veres, Mar 17, 2022.

  1. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Hey,

    I followed this youtube tutorial on Bézier curves. But when I add the OnSceneGui method, for some reason the FreeMoveHandles won't show up, and also all the handles, for moving objects in the scene disappear as well. I have gizmos enabled, all the methods are being called correctly, my camera is behind the object so I don't know what might be the problem.

    These are the scripts for the tutorial. I checked and they are the same as mine until a point, where I had to stop because I couldn't follow along anymore, thanks to the problem.

    Regardless, this is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Path
    7. {
    8.     [SerializeField, HideInInspector]
    9.     List<Vector2> points;
    10.    
    11.     public Path(Vector2 centre)
    12.     {
    13.         points = new List<Vector2>
    14.         {
    15.             centre + Vector2.left,
    16.             centre + (Vector2.left + Vector2.up) * 0.5f,
    17.             centre + (Vector2.right + Vector2.down) * 0.5f,
    18.             centre + Vector2.right
    19.  
    20.         };
    21.  
    22.     }
    23.  
    24.     public void AddSegment(Vector2 anchorPos)
    25.     {
    26.         points.Add(points[points.Count - 1] * 2 - points[points.Count - 2]);
    27.         points.Add((points[points.Count - 1] + anchorPos) / 2);
    28.         points.Add(anchorPos);
    29.     }
    30.  
    31.     public Vector2[] GetPointsInSegment(int i)
    32.     {
    33.         return new Vector2[]
    34.         {
    35.             points[i * 3],
    36.             points[i * 3 + 1],
    37.             points[i * 3 + 2],
    38.             points[i * 3 + 3],
    39.         };
    40.     }
    41.  
    42.     public Vector2 this[int i]
    43.     {
    44.         get
    45.         {
    46.             return points[i];
    47.         }
    48.     }
    49.  
    50.     public int NumSegments
    51.     {
    52.         get
    53.         {
    54.             return (points.Count - 1) / 3;
    55.         }
    56.     }
    57.  
    58.     public int NumPoints
    59.     {
    60.         get
    61.         {
    62.             return points.Count;
    63.         }
    64.     }
    65.  
    66.     public void MovePoint(int i, Vector2 pos)
    67.     {
    68.         points[i] = pos;
    69.     }
    70. }
    71.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PathCreator : MonoBehaviour
    6. {
    7.     [HideInInspector]
    8.     public Path path;
    9.  
    10.     public void CreatePath()
    11.     {
    12.         path = new Path(transform.position);
    13.     }
    14. }
    15.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(PathCreator))]
    7. public class PathEditor : Editor
    8. {
    9.     PathCreator creator;
    10.     Path path;
    11.  
    12.  
    13.     private void OnSceneGUI()
    14.     {
    15.         Draw();
    16.     }
    17.     void Draw()
    18.     {
    19.  
    20.         Handles.color = Color.red;
    21.         for (int i = 0; i < path.NumPoints; i++)
    22.         {
    23.  
    24.             Vector2 v2 = creator.transform.position;
    25.             Vector2 newPos = Handles.FreeMoveHandle(path[i] + v2, Quaternion.identity, 0.5f, Vector2.zero, Handles.SphereHandleCap);
    26.             if (path[i] != newPos)
    27.             {
    28.                 Undo.RecordObject(creator, "Move point");
    29.                 path.MovePoint(i, newPos);
    30.             }
    31.         }
    32.     }
    33.  
    34.     private void OnEnable()
    35.     {
    36.  
    37.         creator = (PathCreator)target;
    38.         if (creator.path == null)
    39.         {
    40.             creator.CreatePath();
    41.         }
    42.         path = creator.path;
    43.     }
    44. }
    45.  
     
  2. lghfoo

    lghfoo

    Joined:
    Jul 10, 2019
    Posts:
    4
    It seems that you should wrap the code with Handles.BeginGUI() and Handles.EndGUI().
    Code (CSharp):
    1. public void OnSceneGUI()
    2. {
    3.     Handles.BeginGUI();
    4.     // YOUR CUSTOM GUI CODE
    5.     Handles.EndGUI();
    6. }