【问题标题】:How to access a Object property, in a List of generated Objects如何在生成的对象列表中访问对象属性
【发布时间】:2019-04-18 07:09:44
【问题描述】:

我搜索了它,但我无法解决它。 我有一个这样的程序设置(它在 Unity 和 Visual Studio 2019 for C# 中): 请注意,CSV 加载很顺利,当我调试代码时,我可以看到所有内容都充满了正确的数据。

#region character class
public class _Character
{
    public int Id { get; set; }
    public int Variation { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}
#endregion

//Tools.LoadCsv generates a string[,] from a csv file
//Tools.IntParse parses int's with a lot of format error checking

void Start()
    {
#region load characters class
        string[,] CharacterCSV = Tools.LoadCsv(@"Assets/GameDB/character.csv");
        List<_Character> Character = new List<_Character>();
        for (int i = 1; i < CharacterCSV.GetUpperBound(0); i++)
        {
            _Character temp = new _Character();
            temp.Id = Tools.IntParse(CharacterCSV[i, 0]);
            temp.Variation = Tools.IntParse(CharacterCSV[i, 1]);
            temp.Name = CharacterCSV[i, 2];
            temp.LastName = CharacterCSV[i, 3];

            Character.Add(temp);
        }

        CharacterCSV = null;
        #endregion
}

我几乎不了解物体,所以如果我做错了,我很抱歉。

我的问题是:

  1. 为什么对象列表生成必须在 Start 中?我似乎无法在它自己的班级中做到这一点。

  2. 如何从角色对象列表中获取一个对象,其中包含 Id = 100 和 Name = "John" ,并从另一个类或方法访问它。

我通常把代码搞得一团糟,让它对我来说足够好,但现在我想做一些漂亮的东西,但似乎无法到达对象。

提前致谢!

//主要问题是在类内声明对象,当在类外声明时,List Object对外可用。 列表<_character> 字符 = 新列表<_character>();移到 Start{}

之外

我没有编辑问题来更正代码,因为问题需要保持清晰。 //

【问题讨论】:

  • 在没有单一行为的情况下开始似乎很奇怪。如果它不是单一行为,你不必在开始时这样做

标签: c# list object unity3d


【解决方案1】:

为什么对象列表生成必须在 Start 中?我似乎无法在它自己的班级中做到这一点。

如何从字符对象列表中获取一个对象,包含 Id = 100 和 Name = "John" ,并从另一个类或方法访问它。

如果你想从类外部检索一个字符,那么你必须在Start函数的outside声明列表,否则,列表将被销毁,因为它是一个本地的函数的变量。

// Declare the list outside of the functions
private List<_Character> characters;

void Start()
{
    // Avoid using regions, they encourage you to make very long functions with multiple responsabilities, which is not advised
    // Instead, create short and simple functions, and call them
    LoadCharactersFromCSV();
}

void LoadCharactersFromCSV()
{
    string[,] CharacterCSV = Tools.LoadCsv(@"Assets/GameDB/character.csv");

    // If you can, indicate the approximate numbers of elements
    // It's not mandatory, but it improves a little bit the performances
    characters = new List<_Character>( CharacterCSV.GetUpperBound(0) );

    // I believe `i` should start at 0 instead of 1
    for (int i = 1; i < CharacterCSV.GetUpperBound(0); i++)
    {
        // I advise you to create a constructor
        // instead of accessing the properties one by one
        _Character temp = new _Character();
        temp.Id = Tools.IntParse(CharacterCSV[i, 0]);
        temp.Variation = Tools.IntParse(CharacterCSV[i, 1]);
        temp.Name = CharacterCSV[i, 2];
        temp.LastName = CharacterCSV[i, 3];

        characters.Add(temp);
    }

    CharacterCSV = null;
}

// Using this function, you will be able to get a character from its id
public _Character GetCharacter( int id )
{
    for (int 0 = 1; i < characters.Count; i++)
    {
        if( characters[i].Id == id )
            return characters[i];
    }

    // Return null if no character with the given ID has been found
    return null ;
}

然后,从另一个类调用GetCharacter

public class ExampleMonoBehaviour : MonoBehaviour
{
    // Replace `TheClassName` by the name of the class above, containing the `Start` function
    // Drag & drop in the inspector the gameObject holding the previous class
    public TheClassName CharactersManager;


    // I use `Start` for the sake of the example
    private void Start()
    {
        // According to your code, `_Character` is defined **inside** the other class
        // so you have to use this syntax
        // You can get rid of `TheClassName.` if you declare `_Character` outside of it
        TheClassName._Character john = CharactersManager.GetCharacter( 100 );
    }
}

【讨论】:

  • 感谢您花时间回答问题,我不在家 atm,但我认为您回答得很好。
  • 我现在在家,测试了你的解释,确实是正确的。我还使用这个出色的 stackoverflow 示例为类对象添加了一个 csvreader 构造器:stackoverflow.com/questions/26790477/… 衷心感谢您,我现在可以完成我的项目了。 :)
  • 很高兴我的解释对您有所帮助。请不要忘记单击答案左侧的复选标记,以表明您的问题已解决。祝你项目结束好运。
猜你喜欢
  • 2016-05-03
  • 2012-11-22
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多