【问题标题】:Get list from another script in unity统一从另一个脚本中获取列表
【发布时间】:2021-05-06 23:32:07
【问题描述】:

我想从另一个脚本上的序列化列表中获取一个值。我怎样才能做到这一点?这是我的清单:

[System.Serializable]
public class Level
{
    public string Name;
    public Sprite Icon;
    public bool Unlocked;
    public bool Interactable;
}

public List<Level> levelList = new List<Level>();

【问题讨论】:

  • 这个序列化成xml了吗?
  • @3xGuy 不,不是
  • 这能回答你的问题吗? Access other script's references in Unity3D
  • @Ruzihm 不是真的:(
  • 序列化与从另一个脚本访问列表或其他项目无关。序列化是从 C# 类型转换为 json、xml(因此有关于它的注释)、yaml、二进制或其他。在这里,您要考虑使用 GetComponent 作为提到的其他评论的公共变量。

标签: c# list unity3d


【解决方案1】:

首先,您需要将您的列表放入类的范围内,如下所示:

using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Level {

    public List<Level> levelList = new List<Level>(); //List inside the class

    public string Name;
    public Sprite Icon;
    public bool Unlocked;
    public bool Interactable;
}

在您的访问器类上,您需要有一个类变量,以便您可以访问该级别实例。然后在某处获取该级别实例的列表,例如下面代码中的Start

using System.Collections.Generic;
using UnityEngine;

public class ListGetter : MonoBehaviour
{
    public Level level;
    private List<Level> levelList;
    
    void Start()
    {
        levelList = level.levelList; //get level list
    }
}

您需要定义您的 Level 实例的创建方式,以及保存该类实例的类以提供更准确的方式来访问您的 Level 实例的 levelList,我认为这是您的在问

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多