Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

OnInspectorGUI for BasicPlayableBehavior

Discussion in 'Timeline' started by fdgfdgffdgdfg, Jul 4, 2017.

  1. fdgfdgffdgdfg

    fdgfdgffdgdfg

    Joined:
    Jul 8, 2014
    Posts:
    7
    Hi all,

    I've been playing around with Timeline, it is a wonderful feature with a huge potential!
    There are some stuff I don't fully understand so I have few questions.

    I have made a custom BasicPlayableBehaviour based on the official examples. First thing I noticed is that when a create a new clip in my custom track, the function OnPlayableCreate is called twice (as well as the GraphStart). Is it a normal behavior?

    But my main question is that I want to create a customeditor to be able to edit more precisely my playable. But the target I have in the OnInspectorGUI is not the object corresponding to my playable (attributes not initialized, different GetInstanceID). So I cannot edit my playable from the GUI.

    Please find below some code to illustrate my questions.

    Code (CSharp):
    1. public class TestPlayable : BasicPlayableBehaviour
    2. {
    3.     public float[] values_ = null;
    4.  
    5.     public void OnPlayableCreate(Playable playable)
    6.     {
    7. //Call twice!
    8. Debug.Log("OnPlayableCreate "+GetInstanceID());
    9. values_ = new float[100];
    10. }
    Code (CSharp):
    1. [CustomEditor(typeof(TestPlayable))]
    2. public class TestInspector : Editor {
    3.  
    4.     public override void OnInspectorGUI()
    5.     {
    6.         DrawDefaultInspector();
    7.  
    8.         if(GUILayout.Button("Click Button"))
    9.         {          
    10. //target.GetInstanceID() is different from the one instanciated. I cannot edit values_ for instance
    11.             Debug.Log (target.GetInstanceID ());
    12.  
    13.         }          
    14.     }          
    15. }
    PS: I am using Unity 2017.1.0f1 Linux.
     
  2. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    123
    First, I would advise not to use BasicPlayableBehaviour for your case. BasicPlayableBehaviour merges PlayableAsset and a PlayableBehaviour. It is fine for simple implementations, but can cause performance issues with more complex setup and can also cause other problems, like those you encountered. It is not your fault though, since BasicPlayableBehaviour is used in the Timeline examples :( (I updated the examples again recently to remove all usage of BasicPlayableBehaviour). I will fix that soon.

    Anyway, to have the right target in you custom editor, you need to get rid of BasicPlayableBehaviour and use PlayableAsset and PlayableBehaviour.

    Your custom editor will need to be a custom editor of your custom asset. You will then have a target instance ID that will match the asset.

    Note that since PlayableBehaviour doesn't derive from Object, it doesn't have an instance ID.

    Code (CSharp):
    1. public class TestPlayable : PlayableBehaviour
    2. {
    3.     public float[] values_ = null;
    4.     public override void OnPlayableCreate(Playable playable)
    5.     {
    6.         values_ = new float[100];
    7.     }
    8. }
    9.  
    10. public class TestPlayableAsset : PlayableAsset
    11. {
    12.     public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    13.     {
    14.         Debug.Log("Asset instance ID: " +  GetInstanceID());
    15.         return ScriptPlayable<TestPlayable>.Create(graph);
    16.     }
    17. }
    18.  
    19. [CustomEditor(typeof(TestPlayableAsset))]
    20. public class TestInspector : Editor {
    21.     public override void OnInspectorGUI()
    22.     {
    23.         DrawDefaultInspector();
    24.         if(GUILayout.Button("Click Button"))
    25.         {        
    26.             Debug.Log (target.GetInstanceID ());
    27.         }        
    28.     }        
    29. }
    30.  

     
  3. fdgfdgffdgdfg

    fdgfdgffdgdfg

    Joined:
    Jul 8, 2014
    Posts:
    7
    Thanks for the answer. I'll stop using BasicPlayableBehaviour then.
    One remaining question though. If I my custom editor is linked to my PlayableAsset, how can I access to the PlayableBehavior and edit values_? At the end this is this variable that I want to edit through the custom editor. (of course in my real use case I have more complex variables to edit).
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
  5. fdgfdgffdgdfg

    fdgfdgffdgdfg

    Joined:
    Jul 8, 2014
    Posts:
    7
    Thanks for the tip. I was looking for these new examples.