英文:
Are Scriptable Objects linked to a Prefab only instantiated once?
问题
我有三个预制体:
这三个预制体都与一个可编写对象链接,如下所示:
所有三个预制体的链接可编写对象都是类型为 SO_Item
的可编写对象,其代码如下:
using UnityEngine;
[CreateAssetMenu(fileName = "so_Item", menuName = "Scriptable Objects/Item/Item")]
public class SO_Item : ScriptableObject
{
[field: SerializeField] public ItemDetails ItemDetails { get; set; }
}
现在我的问题是这三个预制体,在我的场景中使用如下:
这会为场景中的每个物品实例化一个新的链接可编写对象吗?
英文:
I have three prefabs:
All three the prefabs have a Scriptable Object linked like this:
The Linked Scriptable Object of all three prefabs is of type SO_Item
Scriptable Object which looks like:
using UnityEngine;
[CreateAssetMenu(fileName = "so_Item", menuName = "Scriptable Objects/Item/Item")]
public class SO_Item : ScriptableObject
{
[field: SerializeField] public ItemDetails ItemDetails { get; set; }
}
Now my question is those three prefabs, I use in my scene like:
Does this instantiate for every item in my scene a new instance of the linked Scriptable Objects?
答案1
得分: 1
不行!
它们都会共享相同的 ScriptableObject
实例,因此也会共享相同的 ItemDetails
实例。
如果你更愿意要一个拷贝,你可以最初使用如下方法:
[SerializeField] private SO_Item soItem;
private SO_Item runtimeSOItem;
private void Awake ()
{
runtimeSOItem = Instantiate(soItem);
}
这将创建一个新的独立的 ScriptableObject
实例。
英文:
Nope!
They will all share the same ScriptableObject
instance and hence also share the same ItemDetails
instance.
If you rather want a copy you can initially use e.g.
[SerializeField] private SO_Item soItem;
private SO_Item runtimeSOItem;
private void Awake ()
{
runtimeSOItem = Instantiate(soItem);
}
which will create a new detached individual instance of the ScriptableObject
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论