英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论