【发布时间】:2019-04-04 15:38:31
【问题描述】:
我有一个字符 Scriptable Object 'Character',它有一个 bool IsMale。我还有一个“团队”脚本对象,它有一个字符脚本对象类中的字符列表。现在,我想在 Team Class 中创建一个自定义方法,用于循环遍历此列表并检查有多少角色是男性,有多少不是。
Character.cs
using UnityEngine;
// Personal Attributes
public string firstName;
public string middleName;
public string lastName;
public string fullName;
public bool isMale;
团队.cs
using UnityEngine;
public List<Character> characters;
// For adding ten players.
public void AddPlayer(Character p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
{
characters.Add(p1);
characters.Add(p2);
characters.Add(p3);
characters.Add(p4);
characters.Add(p5);
characters.Add(p6);
characters.Add(p7);
characters.Add(p8);
characters.Add(p9);
characters.Add(p10);
}
// I want to loop through these ten characters in the list and tell how many are males and how many are not
再次,我想在 Team Class 中创建一个自定义方法,用于循环遍历此列表并检查有多少角色是男性,有多少不是。
对于 cmets 中的问题@derHugo 字符.cs
using UnityEngine;
using System;
[CreateAssetMenu()]
public class Character : ScriptableObject
{
// Personal Attributes
public string firstName;
public string middleName;
public string lastName;
public string fullName;
public bool isMale;
private int age;
public int personalMoney;
public Sprite image;
// Game Attributes
public int totalRuns;
public int salary;
public enum characterTypes { PlayerCharacter, Manager, Player, Staff };
public characterTypes characterType;
public enum battingHands { LeftHanded, RightHanded };
public battingHands battingHand;
public enum bowlingHands { LeftHanded, RightHanded };
public bowlingHands bowlingHand;
public enum battingMentalities { Aggressive, Balanced, Defensive };
public battingMentalities battingMentality;
public enum bowlingMentalities { Aggressive, Balanced, Defensive };
public bowlingMentalities bowlingMentality;
// Skills
// Technical Skills
public int technical; // Overall Technical
public int judgement; // Batting
public int agility; // Running, Low means if player accidentaly falls down, the time he will take to get back up
public int cardioFitness; // Injury
public int muscleFitness; // Injury and Hitting Power
public int runSpeed; // Running
public int strength; // Hitting Power
// Methods
// Personal Attributes Methods
public void CalculateAge(DateTime dateOfBirth)
{
age = 0;
age = DateTime.Now.Year - dateOfBirth.Year;
if (DateTime.Now.DayOfYear < dateOfBirth.DayOfYear)
age = age - 1;
}
// Starting Game Methods Required
public void SetCharacterType(characterTypes cT)
{
Debug.Log("Setting Character Type for " + fullName);
cT = characterType;
if (cT == characterTypes.PlayerCharacter)
{
Debug.Log(fullName + " is a Player Character");
}
else if (cT == characterTypes.Manager)
{
Debug.Log(fullName + " is a Manager");
}
else if (cT == characterTypes.Player)
{
Debug.Log(fullName + " is a Player");
}
else if (cT == characterTypes.Staff)
{
Debug.Log(fullName + " is a Staff");
}
else
{
Debug.Log("No Character Type");
}
}
public void TechnicalAverage()
{
technical = (judgement + agility + cardioFitness + muscleFitness + runSpeed + strength / 600) * 100;
}
}
【问题讨论】:
-
如果我是你,我会将
AddPlayer(Character p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)更改为AddPlayer(Character p)。然后以不同的方式添加它们,并为男性/女性保留一个计数器,并在每次添加角色时检查并增加女性的男性计数器。这样您就不必每次需要此信息时都循环列表。 -
@AliKanat 也许他想确保它总是以正好 10 个字符调用
-
@derHugo 是的,实际上这是有道理的。感谢您指出。
-
我正在制作板球比赛。这就是为什么我添加了 10 名玩家(我知道有 11 名玩家,但这只是为了测试目的)。我想确保添加了玩家。必须的。我也是 15 岁,也是 Unity 的初学者。这也可能是这种业余爱好者的原因。
-
或者你可以使用
Character[] characters = new Character[10];,因为你不希望大小是动态的
标签: c# visual-studio unity3d