Setting Exposed References From Script

Hey guys,

Is this how you’re supposed to set exposed references from script? I’m having an issue where all the exposed references take on the value of the last call to setReferenceValue

using UnityEditor;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class TestBehavior : PlayableBehaviour
{
    public GameObject testObjectOne;
    public GameObject testObjectTwo;
}

public class TestPlayable : PlayableAsset
{
    public ExposedReference<GameObject> testObjectOne;
    public ExposedReference<GameObject> testObjectTwo;

    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        ScriptPlayable<TestBehavior> testPlayable = ScriptPlayable<TestBehavior>.Create(graph);
        TestBehavior testBehavior = testPlayable.GetBehaviour();
        testBehavior.testObjectOne = testObjectOne.Resolve(graph.GetResolver());
        testBehavior.testObjectTwo = testObjectTwo.Resolve(graph.GetResolver());

        return testPlayable;
    }
}

public class TestExample : Editor
{
    [MenuItem("Test/TestReferenceSetting")]
    public static void TestReferenceSetting()
    {
        GameObject directorObject = new GameObject("Playable Director");
        PlayableDirector playableDirector = directorObject.AddComponent<PlayableDirector>();

        TimelineAsset timeline = CreateInstance<TimelineAsset>();;
        AssetDatabase.CreateAsset(timeline, "Assets/testAsset.playable");

        playableDirector.playableAsset = timeline;

        PlayableTrack playableTrack = timeline.CreateTrack<PlayableTrack>(null, "Test Track");

        TimelineClip timelineClip = playableTrack.CreateClip<TestPlayable>();
        TestPlayable testPlayable = timelineClip.underlyingAsset as TestPlayable;

        GameObject objectOne = new GameObject("one");
        GameObject objectTwo = new GameObject("two");
     
        playableDirector.SetReferenceValue(testPlayable.testObjectOne.exposedName, objectOne );
        playableDirector.SetReferenceValue(testPlayable.testObjectTwo.exposedName, objectTwo );
    }
}

3469500--275548--exampleimage.PNG

1 Like

Yes, that’s will work. You can also set the exposedName to be the same or different values. By default they should be unique. If they are the same name, then you can use the exposed references more like a variable, and set it once on the playable director, and have it effect all references. .

Found the solution here: Set ControlPlayableAsset clip's Source Game Object

You have to set the exposedName to something unique.

2 Likes