为什么在设置脚本执行顺序时仍然先执行一个脚本?
Why when setting the order of scripts executing still one script is executing first?
我有两个脚本附加到同一个空游戏对象。
第一个脚本是:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
public GameObject objectToPlace;
public Vector3 newObjectsSize = new Vector3(5, 5, 5);
public float spawnSpeed = 0.1f;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
private int currentObjects;
private List<GameObject> objects = new List<GameObject>();
void Start()
{
var wi = GetComponent<Walls>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
for (int i = 0; i < numberOfObjects; i++)
{
GameObject newObject = (GameObject)Instantiate(objectToPlace);
newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
newObject.transform.localPosition = GenerateRandomPositions(newObject);
newObject.name = "Spawned Object";
newObject.tag = "Spawned Object";
objects.Add(newObject);
yield return new WaitForSeconds(spawnSpeed);
currentObjects += 1;
}
}
private Vector3 GenerateRandomPositions(GameObject newObject)
{
float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
float originX = wallsPosX + paddingX - wallsLengthX / 2f;
float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
return new Vector3(posx, posy, posz);
}
}
第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WayPoints : MonoBehaviour
{
public GameObject[] waypoints;
public Transform target;
public float moveSpeed = 10f;
public float moveSpeed1 = 10f;
public float slowDownSpeed = 3f;
public float reverseSlowDownSpeed = 3f;
public float rotationSpeed = 1f;
private Transform myTransform;
private int targetsIndex = 0;
private Vector3 originalPosition;
private GameObject[] robots;
public Transform reverseTarget;
private int reverseTargetsIndex = 0;
private Vector3 reverseOriginalPosition;
public bool random = false;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("Spawned Object");
第一个脚本产生了 10 个新的立方体。
但是第二个脚本 waypoints 变量只得到 1 "Spawned Object" 而不是 10.
当我使用断点时,我看到它正在执行循环中的第一个脚本:
for (int i = 0; i < numberOfObjects; i++)
但随后它跳转到第二个脚本得到一个 "Spawned Object" 然后回到第一个脚本并完成循环的其余部分。
我想要的是,它首先使整个循环产生 10 个立方体,然后用第二个脚本获得它们。
我什至在编辑器中做了:编辑 > 项目设置 > 脚本执行顺序
但它仍然没有首先执行整个循环,只迭代一次。
你在 Spawn()
协程中有收益:
yield return new WaitForSeconds(spawnSpeed);
Start()
函数正在以正确的顺序调用,但第一个脚本只是启动一个协程,该协程在每次迭代后产生。在第一次 yield 之后,它会等到 WaitForSeconds
结束后再继续;与此同时,第二个脚本将 运行 它的 Start()
.
作为解决方案,您无法通过脚本执行顺序来解决此问题。您的第二个脚本必须等到第一个脚本完成 运行 其 Spawn()
协程。有很多方法可以做到这一点,但最简单的方法是公开 StartCoroutine()
返回的 Coroutine
对象,或者在 SpawnObjects
class.
我有两个脚本附加到同一个空游戏对象。 第一个脚本是:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
public GameObject objectToPlace;
public Vector3 newObjectsSize = new Vector3(5, 5, 5);
public float spawnSpeed = 0.1f;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
private int currentObjects;
private List<GameObject> objects = new List<GameObject>();
void Start()
{
var wi = GetComponent<Walls>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
for (int i = 0; i < numberOfObjects; i++)
{
GameObject newObject = (GameObject)Instantiate(objectToPlace);
newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
newObject.transform.localPosition = GenerateRandomPositions(newObject);
newObject.name = "Spawned Object";
newObject.tag = "Spawned Object";
objects.Add(newObject);
yield return new WaitForSeconds(spawnSpeed);
currentObjects += 1;
}
}
private Vector3 GenerateRandomPositions(GameObject newObject)
{
float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
float originX = wallsPosX + paddingX - wallsLengthX / 2f;
float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
return new Vector3(posx, posy, posz);
}
}
第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WayPoints : MonoBehaviour
{
public GameObject[] waypoints;
public Transform target;
public float moveSpeed = 10f;
public float moveSpeed1 = 10f;
public float slowDownSpeed = 3f;
public float reverseSlowDownSpeed = 3f;
public float rotationSpeed = 1f;
private Transform myTransform;
private int targetsIndex = 0;
private Vector3 originalPosition;
private GameObject[] robots;
public Transform reverseTarget;
private int reverseTargetsIndex = 0;
private Vector3 reverseOriginalPosition;
public bool random = false;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("Spawned Object");
第一个脚本产生了 10 个新的立方体。 但是第二个脚本 waypoints 变量只得到 1 "Spawned Object" 而不是 10.
当我使用断点时,我看到它正在执行循环中的第一个脚本:
for (int i = 0; i < numberOfObjects; i++)
但随后它跳转到第二个脚本得到一个 "Spawned Object" 然后回到第一个脚本并完成循环的其余部分。
我想要的是,它首先使整个循环产生 10 个立方体,然后用第二个脚本获得它们。
我什至在编辑器中做了:编辑 > 项目设置 > 脚本执行顺序 但它仍然没有首先执行整个循环,只迭代一次。
你在 Spawn()
协程中有收益:
yield return new WaitForSeconds(spawnSpeed);
Start()
函数正在以正确的顺序调用,但第一个脚本只是启动一个协程,该协程在每次迭代后产生。在第一次 yield 之后,它会等到 WaitForSeconds
结束后再继续;与此同时,第二个脚本将 运行 它的 Start()
.
作为解决方案,您无法通过脚本执行顺序来解决此问题。您的第二个脚本必须等到第一个脚本完成 运行 其 Spawn()
协程。有很多方法可以做到这一点,但最简单的方法是公开 StartCoroutine()
返回的 Coroutine
对象,或者在 SpawnObjects
class.