Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

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

Are Scriptable Objects linked to a Prefab only instantiated once?

问题

我有三个预制体:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

这三个预制体都与一个可编写对象链接,如下所示:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

所有三个预制体的链接可编写对象都是类型为 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; }
}

现在我的问题是这三个预制体,在我的场景中使用如下:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

这会为场景中的每个物品实例化一个新的链接可编写对象吗?

英文:

I have three prefabs:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

All three the prefabs have a Scriptable Object linked like this:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

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:

Scriptable Objects(脚本化对象)是否仅与一个预制体相关,只被实例化一次?

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.

huangapple
  • 本文由 发表于 2023年2月23日 19:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544217.html
匿名

发表评论

匿名网友

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

确定