如何通过C#从主要的Unity预制体更新所有预制体?

huangapple go评论64阅读模式
英文:

How to update all unity prefabs from the main one through c#?

问题

我有一个更新主预制件的精灵渲染器的脚本(位于Assets中的那个),但这不会改变或更新主预制件的实例的精灵渲染器。如何通过C#更改所有该预制件的实例?

英文:

I have a script that updates the Sprite Renderer of the main prefab (the one that is in Assets) but this does not change or update the Sprite Renderer of the instances of the main Prefab. How can I change all instances of that prefab through c#?

答案1

得分: 0

如果您尝试在运行时进行更改,将不起作用,因为预制体的实例是独立的对象,如果要更改精灵,应该对每个预制体实例进行操作。

预制体是预先制作的游戏对象,可与模板进行比较。一个游戏对象可以在所有组件和属性设置完毕的情况下创建一次,并存储为预制体资产。然后可以实例化此预制体,这意味着在场景中生成预制体的实例或副本。

如果您想要在编辑器中替换精灵,可以直接在预制体上执行此操作,或者通过编辑器脚本进行更改。

英文:

If you try to made the changes at runtime will not work because the instances of the prefab are independant objects, if you want to change the sprite you should do it to each instance of the prefab.

> Prefabs are ready-made GameObjects, comparable to templates. A
> GameObject can be created once with all of its components and
> properties set up and stored as a prefab asset. This prefab can then
> be instantiated, which means an instance or copy of the prefab is
> spawned into the scene.

If you want to replace that sprites on the editor you could do it directly on the prefab, or via a editor script that made the changes.

huangapple
  • 本文由 发表于 2023年5月18日 03:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定