我正在为我在Unity引擎中工作的游戏创建用户帐户。我认为实现这一点的最好方法是将对象添加到ArrayList(如果我没有弄错的话,使用ArrayList你不需要定义数组的大小)。我设置了对象属性(我希望每个新玩家拥有的基本属性)。当我到达代码accessInfo段中的foreach循环时,我被卡住了,试图引用对象的特定属性(例如,为演示目的将playerAgility设置为30)。任何帮助都太好了。谢谢!
newProfile脚本:
using UnityEngine;
using System.Collections;
public class newProfile : Profile
{
ArrayList newprofile = new ArrayList();
public void newplayer(string input)
{
newprofile.Add(new Profile(10, 20, 30, 40, 50, input));
}
public void accessInfo()
{
foreach (Profile element in newprofile)
{
// Debug.Log(need to access the values set for the profile);
}
}
public newProfile(string input)
{
newplayer(input);
accessInfo();
}
}
Profile脚本(一个不创建大量帖子的示例):
public class Profile
{
private Profile user;
private int PROFILE_STAMINA, PROFILE_STRENGTH, PROFILE_AGILITY;
private int PROFILE_INTELLECT, PROFILE_SKILL,PROFILE_MASTERY;
private string ProfileName;
public Profile(int Stamina, int Strength, int Agility, int Intellect, int Skill, string username)
{
user = new Profile();
profileStamina = Stamina;
profileStrength = Strength;
profileAgility = Agility;
profileIntellect = Intellect;
profileSkill = Skill;
userName = username;
}
public int profileStamina
{
get { return PROFILE_STAMINA; }
set { PROFILE_STAMINA = value; }
}
//Identical setters and getters for the remaining stats
public string userName
{
get { return ProfileName; }
set { ProfileName = value; }
}
}
调试类:
public class debug : MonoBehaviour
{
void Start()
{
new newProfile("working");
}
}
Trevor Ward提供的解决方案:
"在foreach循环中,您将使用element.profileStamina访问耐力属性(例如)。"