Search Unity

Custom Editors For Custom Components Doesn't Get Onscenegui

Discussion in 'Cinemachine' started by Baste, Apr 12, 2019.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I've got a custom component, with, among other things, a bezier curve used to help position the camera:

    Code (csharp):
    1. public class MyComponent : CinemachineComponentBase {
    2.     public BezierCurve curve;
    3. }
    With a custom editor that tried to draw this bezier:

    Code (csharp):
    1. [CustomEditor(typeof(MyComponent))]
    2. public class MyComponentEditor : Editor {
    3.     private SerializedField p0, p1, p2, p3; // fetched in OnEnable()
    4.  
    5. ...
    6.  
    7.     void OnSceneGUI() {
    8.         var basePos = target.transform.position;
    9.  
    10.         var p0Pos = basePos + p0.vector3Value;
    11.         var p1Pos = basePos + p1.vector3Value;
    12.         var p2Pos = basePos + p2.vector3Value;
    13.         var p3Pos = basePos + p3.vector3Value;
    14.  
    15.         Handles.DrawBezier(p0Pos, p3Pos, p1Pos, p2Pos, Color.white, null, 1f);
    16.     }
    17. }
    And this doesn't work! My custom inspector draws correctly, but OnSceneGUI doesn't get called. I believe this is because you're not really attaching components to the vCam's GameObject, but rather to an invisible "CM" GameObject?

    Now, I can fix this by assigning to SceneView.onSceneGUIDelegate:

    Code (csharp):
    1. private void OnEnable() {
    2.     SceneView.onSceneGUIDelegate += OnSceneGUI;
    3. }
    4.  
    5. private void OnDisable() {
    6.     SceneView.onSceneGUIDelegate -= OnSceneGUI;
    7. }
    8.  
    9. private void OnSceneGUI(SceneView sceneview) {
    10.     OnSceneGUI();
    11. }
    , but this feels hacky. Any chance you could handle this internally, so OnSceneGUI for custom component inspectors works as intended?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Yeah, you're stuck with adding the callback. As you surmised, the component is added to a hidden gameObject, and this fools the editor. Future versions of CM won't have this issue.

    If you're feeling brave, you can try the experimental CinemachineNewVirtualCamera, which does not make use of hidden objects. That might work better for you (your component will still work).