希望预制件有一组预定义的变化时,预制件变体非常有用。
例如,您可能希望在游戏中使用几种不同类型的 GermSlimeTarget,这些全都基于同一个基本 GermSlimeTarget 预制件。但是,您可能想要一些 GermSlimeTarget 携带物品,一些以不同的速度移动,或者一些发出额外的声音效果。
为此,您可以设置初始 GermSlimeTarget 预制件来执行您希望所有 GermSlimeTarget 共同执行的所有基本操作,然后可以创建多个预制件变体来实现以下目的:
通过在脚本上使用属性覆盖来更改速度,使 GermSlimeTarget 更快移动。
通过将额外的游戏对象附加到手臂,使 GermSlimeTarget 携带物品。
通过添加一个播放格喳声的 AudioSource 组件,使 GermSlimeTarget 发出子弹般的格喳声。
预制件变体继承另一个称为基础预制件的预制件的属性。对预制件变体进行的覆盖优先于基础预制件的值。预制件变体可以使用任何其他预制件作为其基础预制件(包括模型预制件或其他预制件变体)。
可通过多种方法基于另一个预制件创建预制件变体。
可在 Project 视图中的预制件上右键单击,然后选择 Create > Prefab Variant。这样将创建所选预制件的变体,该变体最初没有任何覆盖。可以在预制件模式下打开预制件变体,然后开始为其添加覆盖。
还可以将预制件实例从 Hierarchy 窗口中拖动到 Project 窗口中。执行此操作时会出现一个对话框,询问是要创建新的原始预制件还是预制件变体。如果选择预制件变体,则会根据拖动的预制件实例创建新的预制件变体。先前位于该实例上的所有覆盖现在都位于新的预制件变体中。可以在预制件模式下打开该变体以添加更多覆盖或者是编辑或删除覆盖。
预制件变体将显示带有箭头的蓝色预制件图标。
在预制件模式下打开预制件变体时,根将显示为带有蓝色预制件图标的预制件实例。此预制件实例表示产生预制件变体的基础预制件,而不表示预制件变体本身。对预制件变体所做的任何编辑都将成为存在于变体中的该基础预制件的覆盖。
在上面的截屏中,即使选择 GermSlimeTarget With GermOBlaster 根游戏对象并单击 Inspector 中的 Select 按钮,也将选择基础预制件 GermSlimeTarget 而不是变体 GermSlimeTarget With GermOBlaster__,因为预制件实例是基础预制件 GermSlimeTarget__ 的一个实例,而 Select 按钮始终选择实例来自的预制件资源。
与任何预制件实例一样,可以在预制件变体中使用预制件覆盖,例如修改的属性值、添加的组件、删除的组件以及添加的子游戏对象。也存在相同的限制:无法更改预制件变体中来自基础预制件的游戏对象的父级。也无法从预制件变体中删除存在于基础预制件中的游戏对象。但是,可以停用游戏对象(作为属性覆盖)来达到与删除游戏对象相同的效果。
注意:在预制件模式下编辑预制件变体时,应了解应用这些覆盖(通过 Overrides 下拉窗口或上下文菜单)会使变体的变化应用于基础预制件资源。这通常不是您想要的效果。预制件变体的要点是提供一种方便的方法来存储有意义且可重复使用的覆盖集合,这就是为什么这些覆盖通常应该保留为覆盖而不要应用于基本预制件资源的原因。为了说明这一点,如果将额外的 GermOBlaster 游戏对象应用于基础预制件资源(“GermSlimeTarget”),那么预制件资源也将具有该 GermOBlaster。GermSlimeTarget With GermOBlaster 变体的关键在于,只有此变体携带 GermOBlaster,所以添加的 GermOBlaster 游戏对象应该留在预制件变体内作为覆盖。
打开 Overrides 下拉窗口时,始终可以在其标题中看到覆盖所针对的对象,以及覆盖存在于哪个背景中。对于预制件变体,标题将显示覆盖是针对基础预制件并存在于预制件变体中。为了使这一点更加清晰,__Apply All__ 按钮也会显示 Apply All to Base。
2018–07–31 页面已发布
在 2018.3 版中添加了嵌套预制件和预制件变体
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.