【发布时间】:2018-10-02 02:29:41
【问题描述】:
我有一个组件列表,我想给它一些像这样的值。
public LoadButtonData loadButtonData;
GameObject clone = Instantiate(buttonPrefab, transform);
LoadButtonProperties properties = clone.GetComponent<LoadButtonProperties>();
properties.filePath = filePath;
properties.Date.text = time.ToShortDateString();
properties.Name.text = fileName;
properties.Place.text = DataLoaded.sceneName;
properties.Thumbnail.sprite = newSprite;
loadButtonData.loadButtons.Add(properties);
LoadButtonData 是使用 DontDestroyOnLoad() 包含在另一个对象中的对象,并在多个场景中使用。但是,切换场景后,会发生这种情况:
我知道这是因为属性作为引用添加到列表中,并且在更改场景时,按钮克隆被破坏并且不能再被引用。
是否有解决方法,让我在整个场景中重用我的值作为实际值而不是引用?如何添加克隆的LoadButtonProperties 数据的副本,而不是对克隆的引用?
编辑:重新表述上述问题以进行澄清
编辑:这是 LoadButtonProperties 类
public class LoadButtonProperties : MonoBehaviour
{
/// <summary>
/// Path to the json file for loading
/// </summary>
public string filePath;
[Tooltip("Place Date here")]
public TextMeshProUGUI Date;
[Tooltip("Place Name here")]
public TextMeshProUGUI Name;
[Tooltip("Place Place here")]
public TextMeshProUGUI Place;
[Tooltip("Place Thumbnail here")]
public Image Thumbnail;
}
尝试进行深度克隆,但由于Image 变量,我无法做到这一点
【问题讨论】:
-
你有没有研究过附加关卡加载?
-
是否建议为 VR 应用加载多个关卡?仅加载 1 占用接近 1gb 的内存
-
附加加载关卡是避免这种情况的一种已知方法!拆分您的关卡,加载您现在正在使用的部分,并卸载您不使用的部分。你会在 AAA 游戏中看到很多这种情况,通常会有电梯之类的东西来拆分关卡并允许异步加载。您可以在冒险游戏教程youtube.com/watch?v=Vi8S2PgMvds 中找到持久关卡的示例
标签: list unity3d pass-by-reference gameobject cloning