创建一个值在运行时可加以解析的类型。
ExposedReference 是一个泛型类型,可用于创建对场景对象的引用,以及通过使用上下文对象在运行时解析它们的实际值。ScriptableObject 或 PlayableAsset 等资源可使用它来创建对场景对象的引用。
以下示例显示 PlayableAsset 如何使用暴露的引用来创建对 Scene GameObject 的引用。
这是一个使用 Timeline 的示例,因此您可能想要首先 set up your GameObject in Timeline并make an animation with your 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); } }
将您 Project 中的下一个脚本放在与上述脚本相同的文件夹中。 该脚本通过在 Playable Track 正在播放时移动 Scene 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 上下文对象,通过解析此引用的值来获取该值。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.