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.
public class TestPlayable : BasicPlayableBehaviour
{
public float[] values_ = null;
public void OnPlayableCreate(Playable playable)
{
//Call twice!
Debug.Log("OnPlayableCreate "+GetInstanceID());
values_ = new float[100];
}
[CustomEditor(typeof(TestPlayable))]
public class TestInspector : Editor {
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(GUILayout.Button("Click Button"))
{
//target.GetInstanceID() is different from the one instanciated. I cannot edit values_ for instance
Debug.Log (target.GetInstanceID ());
}
}
}
PS: I am using Unity 2017.1.0f1 Linux.
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.
public class TestPlayable : PlayableBehaviour
{
public float[] values_ = null;
public override void OnPlayableCreate(Playable playable)
{
values_ = new float[100];
}
}
public class TestPlayableAsset : PlayableAsset
{
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
Debug.Log("Asset instance ID: " + GetInstanceID());
return ScriptPlayable<TestPlayable>.Create(graph);
}
}
[CustomEditor(typeof(TestPlayableAsset))]
public class TestInspector : Editor {
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if(GUILayout.Button("Click Button"))
{
Debug.Log (target.GetInstanceID ());
}
}
}
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).
The default playables examples on the asset store (Unity Asset Store - The Best Assets for Game Making) have an example of this. Instead on an editor they use an embedded PlayableBehaviour and custom Property Drawers.
Thanks for the tip. I was looking for these new examples.