我搜索了其中的内容,但无法解决。 我有一个像这样的程序设置(它在Unity和Visual Studio 2019 for C#中): 请注意,CSV加载正常,在调试代码时,我可以看到所有填充有corect数据的数据。
df['sales_percent'] = df.groupby(['state'])['sales'].transform(lambda x: x/x.sum())
我几乎不了解对象,所以如果我做错了我很抱歉。
我的问题是:
为什么要在“开始”中显示“对象列表”?我似乎无法在自己的类中这样做。
如何从“角色对象列表”中获取一个对象,其中包含ID = 100和Name =“ John” ,然后从另一个类或方法访问它。
我通常弗兰肯斯坦(Frankenstein)都不会使用代码,并且对我来说足够好了,但是现在我想做点好事,似乎无法到达对象。
提前谢谢!
///主要的问题是在类内部声明对象,当在类外部声明对象时,列表对象可用于外部。 List <_Character>字符=新List <_Character>();移至外部开始{}
我没有编辑问题来更正代码,因为问题需要保持清晰。 //
答案 0 :(得分:1)
为什么必须在
Start
中生成对象列表?我似乎无法在自己的课堂上这样做。如何从“字符对象列表”中获取一个对象,该对象包含ID = 100和Name =“ John”,并从另一个类或方法中访问它。
如果要从类外部检索字符,则必须声明Start
函数的外部列表,否则,该列表将被销毁。函数的局部变量。
// 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 );
}
}