using System.Collections; using System.Collections.Generic; using UnityEngine; public class CloneGameObject: MonoBehaviour { public static void CloneGameObjects(GameObject clone,int cloneNumber) { for (int i = 0; i < cloneNumber; i++) { GameObject newImage = Instantiate(clone); newImage.transform.parent = clone.transform.parent; // 移动新复制的图片对象到右边 newImage.transform.position = new Vector3(clone.transform.position.x, clone.transform.position.y, clone.transform.position.z); // 确保复制对象的缩放与原对象一致 newImage.transform.localScale = clone.transform.localScale; } } // 删除当前对象的所有子物体,除了指定的一个 public static void DeleteChildrenExceptOne(GameObject content,string childToKeepName) { // 获取当前物体的 Transform 组件 Transform parentTransform = content.transform; // 遍历所有子物体并删除 foreach (Transform child in parentTransform) { // 如果子物体的名称不等于要保留的名称,则销毁它 if (child.name != childToKeepName) { DestroyImmediate(child.gameObject); } } } public static IEnumerator DeleteChildrenExceptOne(GameObject content, string childToKeepName, System.Action onComplete) { Transform parentTransform = content.transform; // 遍历所有子物体并删除 foreach (Transform child in parentTransform) { // 如果子物体的名称不等于要保留的名称,则销毁它 if (child.name != childToKeepName) { // 销毁子物体 Destroy(child.gameObject); } } // 等待物体销毁后再执行后续操作 yield return new WaitForEndOfFrame(); // 确保销毁操作在这一帧完成 // 检查所有子物体是否已经被销毁 while (parentTransform.childCount > 1) { yield return null; // 等待下一帧继续检查 } // 所有子物体已经被销毁,执行后续操作 onComplete?.Invoke(); } // 启动销毁操作的例子 public void StartDeletion() { StartCoroutine(DeleteChildrenExceptOne(gameObject, "ChildToKeep", () => { // 这里是销毁完成后的回调 Debug.Log("All children are destroyed! Now you can proceed."); // 执行后续操作 })); } }