Unity C# 记住 WaitForSeconds Yield 之前的变量
Unity C# Remember variables from before WaitForSeconds Yield
我正在尝试在 Portal 位置所在的位置生成一个平台,所以它看起来像是从 Portal 出来的。
我在使用这一行时遇到问题:Vector3 PlatformPosition = m_PortalPosition;
,因为 SpawnPortal
等待 1 秒然后调用生成平台方法,m_PortalPosition
被覆盖了开始分配 PlatformPosition
。
在调用 WaitForSeconds(1f);
之前,如何 'remember' m_PortalPosition
的变量?
void Awake()
{
for (int i = 0; i < 10; i++)
{
StartCoroutine(SpawnPortal());
}
}
private IEnumerator SpawnPortal()
{
// Do portal stuff
// Get an Platform from the object pool.
GameObject PortalGameObject = m_PortalObjectPool.GetGameObjectFromPool();
// Generate a position at a distance forward from the camera within a random sphere and put the Platform at that position.
m_PortalPosition = m_Cam.position + Vector3.forward * m_SpawnZoneDistance + new Vector3(UnityEngine.Random.insideUnitSphere.x * m_PlatFormZoneRadius.x, UnityEngine.Random.insideUnitSphere.y * m_PlatFormZoneRadius.y, UnityEngine.Random.insideUnitSphere.z * m_PlatFormZoneRadius.z);
PortalGameObject.transform.position = m_PortalPosition;
yield return new WaitForSeconds(1f);
SpawnPlatform();
}
private void SpawnPlatform()
{
// Get an Platform from the object pool.
GameObject PlatformGameObject = m_PlatformObjectPool.GetGameObjectFromPool();
//Set the platform position to the portal position, problem with this line
Vector3 PlatformPosition = m_PortalPosition;
PlatformGameObject.transform.position = PlatformPosition;
// Get the Platform component and add it to the collection.
Platform Platform = PlatformGameObject.GetComponent<Platform>();
m_platforms.Add(Platform);
// Subscribe to the Platforms events.
Platform.OnPlatformRemovalDistance += HandlePlatformRemoval;
m_lowestPlatformPositionY = m_platforms.Min(x => x.transform.position.y);
}
您需要使用局部变量而不是 class 字段。
这样,每次调用函数都会有自己的独立变量。
使 m_PortalPosition
成为 SpawnPortal
协程中的局部变量,而不是使其成为 class 变量。每次调用它时将它作为参数传递给 SpawnPlatform
并使用传递的参数代替。您的代码将更改为:
private IEnumerator SpawnPortal()
{
//...
// Note the local variable?
Vector3 m_PortalPosition = //...
PortalGameObject.transform.position = m_PortalPosition;
yield return new WaitForSeconds(1f);
SpawnPlatform(m_PortalPosition);
}
private void SpawnPlatform(Vector3 PlatformPosition)
{
//...
// Commented out, not needed as we are using the argument
//Vector3 PlatformPosition = m_PortalPosition;
PlatformGameObject.transform.position = PlatformPosition;
//...
}
我正在尝试在 Portal 位置所在的位置生成一个平台,所以它看起来像是从 Portal 出来的。
我在使用这一行时遇到问题:Vector3 PlatformPosition = m_PortalPosition;
,因为 SpawnPortal
等待 1 秒然后调用生成平台方法,m_PortalPosition
被覆盖了开始分配 PlatformPosition
。
在调用 WaitForSeconds(1f);
之前,如何 'remember' m_PortalPosition
的变量?
void Awake()
{
for (int i = 0; i < 10; i++)
{
StartCoroutine(SpawnPortal());
}
}
private IEnumerator SpawnPortal()
{
// Do portal stuff
// Get an Platform from the object pool.
GameObject PortalGameObject = m_PortalObjectPool.GetGameObjectFromPool();
// Generate a position at a distance forward from the camera within a random sphere and put the Platform at that position.
m_PortalPosition = m_Cam.position + Vector3.forward * m_SpawnZoneDistance + new Vector3(UnityEngine.Random.insideUnitSphere.x * m_PlatFormZoneRadius.x, UnityEngine.Random.insideUnitSphere.y * m_PlatFormZoneRadius.y, UnityEngine.Random.insideUnitSphere.z * m_PlatFormZoneRadius.z);
PortalGameObject.transform.position = m_PortalPosition;
yield return new WaitForSeconds(1f);
SpawnPlatform();
}
private void SpawnPlatform()
{
// Get an Platform from the object pool.
GameObject PlatformGameObject = m_PlatformObjectPool.GetGameObjectFromPool();
//Set the platform position to the portal position, problem with this line
Vector3 PlatformPosition = m_PortalPosition;
PlatformGameObject.transform.position = PlatformPosition;
// Get the Platform component and add it to the collection.
Platform Platform = PlatformGameObject.GetComponent<Platform>();
m_platforms.Add(Platform);
// Subscribe to the Platforms events.
Platform.OnPlatformRemovalDistance += HandlePlatformRemoval;
m_lowestPlatformPositionY = m_platforms.Min(x => x.transform.position.y);
}
您需要使用局部变量而不是 class 字段。
这样,每次调用函数都会有自己的独立变量。
使 m_PortalPosition
成为 SpawnPortal
协程中的局部变量,而不是使其成为 class 变量。每次调用它时将它作为参数传递给 SpawnPlatform
并使用传递的参数代替。您的代码将更改为:
private IEnumerator SpawnPortal()
{
//...
// Note the local variable?
Vector3 m_PortalPosition = //...
PortalGameObject.transform.position = m_PortalPosition;
yield return new WaitForSeconds(1f);
SpawnPlatform(m_PortalPosition);
}
private void SpawnPlatform(Vector3 PlatformPosition)
{
//...
// Commented out, not needed as we are using the argument
//Vector3 PlatformPosition = m_PortalPosition;
PlatformGameObject.transform.position = PlatformPosition;
//...
}