仅当数组的值已更改时,如何更新 Update 中的方法?
How can i update a method inside Update only if the value of array has changed?
更新内幕:
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 的长度发生变化时执行此操作,然后再次调用更新中的 GeneratePatrolPoint。
例如,如果 TeleportationBooths 的长度第一次是 5,然后在更新中如果它发生了变化,现在它每次都是 7 或 70,如果长度从上一个数字开始发生变化,那么就更新它。
不是统一专家,但从 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;
}
TeleportationBooths
是一个数组,因此它的长度 不能 改变。它也是一个局部变量,因此 不能 在除 GeneratePatrolPoints()
函数之外的任何其他地方使用。
以下是您需要做的事情的清单:
1。使 TeleportationBooths
成为 List
而不是数组。
2.让它成为全局变量.
3。要更改TeleportationBooths
的大小,您必须调用TeleportationBooths.Add
添加对象或调用TeleportationBooths.Remove
删除对象或 TeleportationBooths.Clear();
从该列表中删除所有内容。这是实现 TeleportationBooths.Count
改变的唯一方法。
4。然后您可以使用 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
.
注意:
不要在更新函数中的每一帧调用GameObject.FindGameObjectsWithTag("Teleportation Booth");
。如果您在 tun-time 期间使用 "Teleportation Booth" 标签实例化一个新的游戏对象,只需将其添加到 TeleportationBooths
列表即可。如果你想销毁它,只需先将它从 List
中删除,然后再销毁它。这就是这里的逻辑。
更新内幕:
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 的长度发生变化时执行此操作,然后再次调用更新中的 GeneratePatrolPoint。
例如,如果 TeleportationBooths 的长度第一次是 5,然后在更新中如果它发生了变化,现在它每次都是 7 或 70,如果长度从上一个数字开始发生变化,那么就更新它。
不是统一专家,但从 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;
}
TeleportationBooths
是一个数组,因此它的长度 不能 改变。它也是一个局部变量,因此 不能 在除 GeneratePatrolPoints()
函数之外的任何其他地方使用。
以下是您需要做的事情的清单:
1。使 TeleportationBooths
成为 List
而不是数组。
2.让它成为全局变量.
3。要更改TeleportationBooths
的大小,您必须调用TeleportationBooths.Add
添加对象或调用TeleportationBooths.Remove
删除对象或 TeleportationBooths.Clear();
从该列表中删除所有内容。这是实现 TeleportationBooths.Count
改变的唯一方法。
4。然后您可以使用 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
.
注意:
不要在更新函数中的每一帧调用GameObject.FindGameObjectsWithTag("Teleportation Booth");
。如果您在 tun-time 期间使用 "Teleportation Booth" 标签实例化一个新的游戏对象,只需将其添加到 TeleportationBooths
列表即可。如果你想销毁它,只需先将它从 List
中删除,然后再销毁它。这就是这里的逻辑。