创建一种类型,其值可以在运行时解析。
ExposedReference 是一个泛型类型,可用于创建对场景对象的引用并在运行时以及使用上下文对象时解析其实际值。资产(例如 ScriptableObject 或 PlayableAsset)可以使用它来创建对场景对象的引用。
以下示例展示了 PlayableAsset 如何使用公开引用来创建对场景 GameObject 的引用。这是一个使用 Timeline 的示例,因此您可能需要 在 Timeline 中设置您的 GameObject 以及 用您的 GameObject 制作动画。
//Put both of these scripts in your Project, then go to your Timeline, click the Add dropdown and choose Playable Track. Place this script on your Timeline as a Playable Track //Click on the track and choose a GameObject from your Scene in the "My Scene Object" field. Also set the velocity.
using UnityEngine; using UnityEngine.Playables;
[System.Serializable] public class ExposedReferenceExample : PlayableAsset { //This allows you to use GameObjects in your Scene public ExposedReference<GameObject> m_MySceneObject; //This variable allows you to decide the velocity of your GameObject public Vector3 m_SceneObjectVelocity;
public override Playable CreatePlayable(PlayableGraph graph , GameObject myGameObject) { //Get access to the Playable Behaviour script TestExample playableBehaviour = new TestExample(); //Resolve the exposed reference on the Scene GameObject by returning the table used by the graph playableBehaviour.m_MySceneObject = m_MySceneObject.Resolve(graph.GetResolver());
//Make the PlayableBehaviour velocity variable the same as the variable you set playableBehaviour.m_SceneObjectVelocity = m_SceneObjectVelocity;
//Create a custom playable from this script using the Player Behaviour script return ScriptPlayable<TestExample>.Create(graph, playableBehaviour); } }
将下一个脚本放在您的项目中,与上面的脚本位于同一个文件夹中。此脚本通过在 Playable Track 播放时移动场景 GameObject 来更改时间线的行为。
using UnityEngine; using UnityEngine.Playables;
public class TestExample : PlayableBehaviour { public GameObject m_MySceneObject; public Vector3 m_SceneObjectVelocity;
public override void PrepareFrame(Playable playable, FrameData frameData) { //If the Scene GameObject exists, move it continuously until the Playable pauses if (m_MySceneObject != null) //Move the GameObject using the velocity you set in your Playable Track's inspector m_MySceneObject.transform.Translate(m_SceneObjectVelocity); } }
defaultValue | 默认值,如果无法解析该值。 |
exposedName | ExposedReference 的名称。 |
Resolve | 通过给定 ExposedPropertyResolver 上下文对象解析来获取引用的值。 |