目前,我创建了一个对象列表,具体取决于是否在一系列对象中找到了任何子对象。然后应该将此列表传递给一个Function,该函数应该遍历此列表,缩小每个tile,然后逐个删除它们。
到目前为止,这是我的代码:
void OnMouseUp(){
subobj = new GameObject[transform.childCount];
RaycastHit hit;
hitCheck = true;
for (int i = 0; i < transform.childCount; i++) {
subobj [i] = transform.GetChild (i).gameObject;
}//store sub objects in array
foreach (GameObject child in subobj){
if (Physics.Raycast (child.transform.position, Vector3.down, out hit)) {
if (hit.collider.tag != "Tile")
hitCheck = false;
} else {
hitCheck = false;
}
// if any one of the sub objects doesnt hit a tile then return to original position
}
if (hitCheck) {
foreach (GameObject child in subobj) {
Physics.Raycast (child.transform.position, Vector3.down, out hit);
child.transform.position = hit.transform.position;
child.transform.parent = hit.transform;
// if a object hit something, child each sub object to what it hit
GameObject.Find("Spawns").GetComponent<CreatingItems>().score += 1f;
}
transform.parent = null;
GameObject.Destroy (this.gameObject);
// destroy the old parent container
} else {
transform.position = startPoint;
transform.localScale = originalScale;
// return to original position
}
if (GameObject.Find ("Spawn1").transform.childCount == 0 && GameObject.Find ("Spawn2").transform.childCount == 0 && GameObject.Find ("Spawn3").transform.childCount == 0) {
CreatingItems ci = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
ci.Spawn ();
}// if spawn position is empty spawn new items
tileList = new GameObject[GameObject.Find ("Generated Map").transform.childCount];
for (int i = 0; i < GameObject.Find ("Generated Map").transform.childCount; i++) {
tileList [i] = GameObject.Find ("Generated Map").transform.GetChild (i).gameObject;
}// stores all tiles as gameobjects in a list.
destroyList.Clear (); // clears the list of items to be destroyed
for (int i = 0; i < 9; i++) {
ChildCheckY = true;
ChildCheckX = true;
for (int y = 0; y <= 9; y++) {
if (tileList [(i*10) + y].transform.childCount == 0) {
ChildCheckY = false;
// checks if y values has children, if no child is found false is returned
}
}
for (int x = 0; x <= 9; x++) {
if (tileList[i + (10*x)].transform.childCount == 0) {
ChildCheckX = false;
// checks if x values has children, if no child is found false is returned
}
}
if (ChildCheckY) {
for (int y = 0; y <= 9; y++) {
destroyList.Add(tileList [(i * 10) + y].transform.GetChild (0).gameObject);
//Scale (tileList [(i * 10) + y].transform.GetChild (0).gameObject);
// destroys 10 y values in a row
}
}
if (ChildCheckX) {
for (int x = 0; x <= 9; x++) {
destroyList.Add(tileList [i + (10 * x)].transform.GetChild (0).gameObject);
//Scale (tileList [i + (10 * x)].transform.GetChild (0).gameObject);
// destroys 10 x values in a row
}
}
}
if(destroyList.Count>=10)
StartCoroutine (Scale (destroyList));
}
IEnumerator Scale(List<GameObject> listdestroy){
CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
foreach(GameObject gameobj in listdestroy){
while (gameobj.transform.localScale.x > 0.2f){
gameobj.transform.localScale -= new Vector3 (0.1f, 0.05f, 0.1f) * Time.deltaTime * speed.deletespeed;
yield return new WaitForSeconds (0.1f);
}
}
yield return null;
}
删除列表中的第一个对象减少了0.04,并且协程“Scale”在此之后退出并且不会缩放其余项目,当我更改代码时,所有项目在1帧内缩放而不是随着时间的推移。 这是我的完整代码
答案 0 :(得分:0)
您的代码:
IEnumerator destroyer(){
CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
for (int i = 0; i < destroyList.Count; i++) {
do {
destroyList [i].transform.localScale = Vector3.MoveTowards(destroyList[i].transform.localScale, Vector3.zero,Time.deltaTime*speed.deletespeed);
} while (destroyList [i].transform.localScale.x > 0.1f);
Destroy (destroyList [i].gameObject);
}
yield return null;
}
尽快删除所有对象。你应该在循环中使用一些延迟,如:
IEnumerator destroyer(){
CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
for (int i = 0; i < destroyList.Count; i++) {
do {
destroyList [i].transform.localScale = Vector3.MoveTowards(destroyList[i].transform.localScale, Vector3.zero,Time.deltaTime*speed.deletespeed);
yield return new WaitForEndOfFrame(); // < == HERE! wait for end frame
} while (destroyList [i].transform.localScale.x > 0.1f);
Destroy (destroyList [i].gameObject);
yield return new WaitForEndOfFrame(); // < == HERE! wait for end frame
}
yield return null;
}
答案 1 :(得分:0)
我想出了一个解决方案,我基本上不允许协程在移动到下一个对象之前完成,因为我只在OnMouseUp内部调用它一次,因此我将代码更改为以下代码来修复它
void Update(){
StartCoroutine (Scale());
}
...
IEnumerator Scale(){
CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems> ();
foreach (GameObject gameobj in destroyList) {
yield return null;
while (Vector3.Distance (gameobj.transform.localScale, new Vector3 (0.1f, 0.05f, 0.1f)) > 0.1f) {
gameobj.transform.localScale -= new Vector3 (0.1f, 0.05f, 0.1f) * Time.deltaTime * speed.deletespeed;
yield return null;
}
Destroy (gameobj);
}
}