更新内部:
private void Update()
{
GeneratePatrolPoints();
}
和GeneratePatrolPoint:
public GameObject[] GeneratePatrolPoints()
{
GameObject[] TeleportationBooths = GameObject.FindGameObjectsWithTag("Teleportation Booth");
patrolPoints = new PatrolData[TeleportationBooths.Length];
for (int i = 0; i < patrolPoints.Length; i++)
{
patrolPoints[i] = new PatrolData();
patrolPoints[i].target = TeleportationBooths[i].transform;
patrolPoints[i].minDistance = 30f;
patrolPoints[i].lingerDuration = 3f;
patrolPoints[i].desiredHeight = 20f;
patrolPoints[i].flightSmoothTime = 10f;
patrolPoints[i].maxFlightspeed = 10f;
patrolPoints[i].flightAcceleration = 3f;
patrolPoints[i].levelingSmoothTime = 0.5f;
patrolPoints[i].maxLevelingSpeed = 10000f;
patrolPoints[i].levelingAcceleration = 2f;
}
return TeleportationBooths;
}
我希望只有当TeleportationBooths数组的长度发生变化,然后再次在Update中调用GeneratePatrolPoint时才会这样做。
例如,如果TeleportationBooths长度是第一次5,然后在更新中,如果它已被更改,现在每次7或70,如果长度已从最后一个数字改变,则更新它。
答案 0 :(得分:1)
不是统一专家,但从C#的角度来看,你唯一需要做的就是创建全局变量来存储上一步的数组大小并用它来与新的变量进行比较。
private int oldLength = 0;
private void Update()
{
GameObject[] TeleportationBooths = GameObject.FindGameObjectsWithTag("Teleportation Booth");
int newLenght = TeleportationBooths.Length;
if (newLenght > 0 && newLenght != oldLength)
{
GeneratePatrolPoints(TeleportationBooths);
oldLength = newLenght;
}
}
public GameObject[] GeneratePatrolPoints(GameObject[] TeleportationBooths)
{
patrolPoints = new PatrolData[TeleportationBooths.Length];
for (int i = 0; i < patrolPoints.Length; i++)
{
patrolPoints[i] = new PatrolData();
patrolPoints[i].target = TeleportationBooths[i].transform;
patrolPoints[i].minDistance = 30f;
patrolPoints[i].lingerDuration = 3f;
patrolPoints[i].desiredHeight = 20f;
patrolPoints[i].flightSmoothTime = 10f;
patrolPoints[i].maxFlightspeed = 10f;
patrolPoints[i].flightAcceleration = 3f;
patrolPoints[i].levelingSmoothTime = 0.5f;
patrolPoints[i].maxLevelingSpeed = 10000f;
patrolPoints[i].levelingAcceleration = 2f;
}
return TeleportationBooths;
}
答案 1 :(得分:1)
TeleportationBooths
是一个数组,因此的长度不能更改。它也是一个局部变量,因此不能在GeneratePatrolPoints()
函数内部以外的任何地方使用。
以下是您需要执行的操作列表:
1 。TeleportationBooths
List
而不是数组。
2 。将其设为全局变量。
3 。要更改TeleportationBooths
的大小,您必须致电TeleportationBooths.Add
添加对象或TeleportationBooths.Remove
删除对象或{{1}从该列表中删除所有内容。这是进行TeleportationBooths.Clear();
更改的唯一方法。
4 。然后,您可以使用TeleportationBooths.Count
检查变量是否随时间发生变化。
这样的事情:
TeleportationBooths.Count
要更改列表,请致电List<GameObject> TeleportationBooths = new List<GameObject>();
int oldLength;
void Start()
{
GameObject[] tempObj = GameObject.FindGameObjectsWithTag("Teleportation Booth");
for (int i = 0; i < tempObj.Length; i++)
{
//Add to list only if it does not exist
if (!TeleportationBooths.Contains(tempObj[i]))
{
TeleportationBooths.Add(tempObj[i]);
}
}
//Get the current Size
if (tempObj != null)
{
oldLength = tempObj.Length;
}
}
void Update()
{
//Check if oldLength has changed
if (oldLength != TeleportationBooths.Count)
{
//Update oldLength
oldLength = TeleportationBooths.Count;
//Call your the function
GeneratePatrolPoints();
}
}
,
来自某处的TeleportationBooths.Add(objToAdd)
或TeleportationBooths.Remove(objToRemove);
,具体取决于您是否要添加,删除或清除TeleportationBooths.Clear()
。
注意强>:
不在更新功能中每一帧调用List
。如果您在tun-time期间使用“Teleportation Booth”标记实例化新的GameObject,只需将其添加到GameObject.FindGameObjectsWithTag("Teleportation Booth");
列表即可。如果你想破坏它,只需先从TeleportationBooths
删除它然后将其销毁。这就是这里的逻辑。