2016-09-30 62 views
0

在这个函数中,我制作了一个循环遍历孩子并销毁它们。 问题是,例如,如果我有1个孩子,并且在脚本中的检查器中,我将孩子的值从1改为10,那么它会将10个新孩子添加到已存在的孩子中。所以,我现在已经11然后,如果我将值改为从10到100,我将有111,如果我变回1,那么我将有112如何在创建新的对象之前销毁所有childs对象?

我希望它同时在游戏运行,如果做些什么我先改变这个值,然后根据最后一次改变的值,重新创建一次。所以如果当我运行游戏时的值是10并且我将它改为100首先删除10然后添加新100.如果我从10改变为1,先删除10然后加1。

我想要什么一般来说,当游戏运行时更改_birdscount的值会改变孩子的数量。

在脚本的顶部:在将检查功能

void Start() 
    { 
    if(globalSettings == null) 
    { 
     settings = LoadSettings(); 
     globalSettings = settings; 
    } 
    else 
     settings = globalSettings; 

    InstantiateBirds(); 
    cameraControl.Enabled = !settings.showSettingsWindow; 

    StartCoroutine(_WatchBirdCount()); 
    } 

这时如果值_birdscount改变或不毁:

public CameraControl cameraControl; 
public Object birdPrefab; 
public Transform birdParent; 
private Transform cameraObj; 
public Transform[] instancePoints; 
public int _birdscount; 

在开始功能我添加了一个StartCoroutine孩子们并创建一次:

int prevBirdCount; 
    List<Transform> transforms = new List<Transform>(); 
    IEnumerator _WatchBirdCount() 
    { 
     prevBirdCount = _birdscount; 

     while(true) 
     { 
      if(_birdscount != prevBirdCount) 
      { 
       prevBirdCount = _birdscount; 
       //Do something 
       foreach (Transform child in birdParent) 
       { 
        //child is your child transform 
        Destroy(child); 
       } 
       InstantiateBirds(); 
      } 
      yield return 0; //Wait a frame before checking again 
      { 
      } 
     } 
    } 

InstantiateBirds()函数t帽子让孩子的: 我加入到这一功能的行:

sts.BirdsCount = prevBirdCount; 

这里:

void InstantiateBirds() 
    { 
    var ip = instancePoints[settings.instancePointNum]; 
    var sts = settings.boidSettings[settings.instancePointNum]; 

    sts.Trace = ip.GetComponent<Trace>(); 

    const float size = 0.1f; 

    cameraObj = InstantiateBird(ip.position, ip.rotation, sts).transform; 
    cameraControl.Target = cameraObj; 
     sts.BirdsCount = prevBirdCount; 
    MathTools.FillSquareUniform(sts.BirdsCount, delegate (int x, int y) 
    { 
     if(x != 0 || y != 0) 
     InstantiateBird(
      cameraObj.position + new Vector3(size * x, size * y, Random.Range(-size, size)), 
      MathTools.RandomYawPitchRotation(), 
      sts 
     ); 
    }); 
    } 

,如果它需要的InstantiateBird功能:

private GameObject InstantiateBird(Vector3 position, Quaternion rotation, Boid.Settings boidSettings) 
    { 
    var obj = (GameObject)Instantiate(birdPrefab, position, rotation); 
    var boid = obj.GetComponent<Boid>(); 

    obj.transform.parent = birdParent; 
    boid.SettingsRef = boidSettings; 
    boid.DebugSettingsRef = settings.debugSettings; 

    return obj; 
    } 

回答

1

沿着这些线路的东西应该这样做:

public void ClearChilds(Transform parent) 
{ 
    foreach(Transform child in parent) 
    { 
     Destroy(child.gameObject); 
    } 
} 

你做在你的这几行代码有一点区别:你只能呼吁变换(childDestroy,而不是游戏对象(child.gameObject)。

编辑:
我写了一个小的代码,将更新对象的儿童人数的剧本是根据公共领域(childCount)的数量上。 currentChildCount仅供公众测试(这是您的prevBirdCount)。 这个脚本将首先销毁所有的老孩子,然后实例化新的孩子。

为了测试这段代码,你只需要一些对象作为你放置这个脚本的父对象和一些作为子对象的预制对象。 (I不与定位乱动。)

using UnityEngine; 
using System.Collections; 

public class ChildrenHandler : MonoBehaviour { 

    public GameObject childPrefab; 

    public int childCount; 

    public int currentChildCount; 

    // Use this for initialization 
    void Start() 
    { 
     InstantiateChildren(childCount); 

     StartCoroutine(CheckChildCount()); 
    } 

    IEnumerator CheckChildCount() 
    { 
     currentChildCount = childCount; 

     while(true) 
     { 
      if(childCount != currentChildCount) 
      { 
       foreach(Transform child in transform) 
       { 
        Destroy(child.gameObject); 
       } 

       InstantiateChildren(childCount); 
      } 
      yield return null; 
     } 
    } 

    private void InstantiateChildren(int num) 
    { 
     for(int i = 0; i < num; i++) 
     { 
      GameObject go = (GameObject)Instantiate(childPrefab, transform.position, Quaternion.identity); 
      go.transform.SetParent(transform); 
     } 

     currentChildCount = childCount; 
    } 
} 
+0

首先我改变现在的线sts.BirdsCount = _birdscount;在InstantiateBirds()现在,当它是= _birdscount时,我键入值2它将添加2个新的孩子,如果我键入5它将添加5个新的孩子。但仍然不是先删除旧的。它不断添加新的孩子到已经存在的孩子。 –

+0

你确实改变了'_WatchBirdCount'中的'foreach'循环来说'Destroy(child.gameObject)'? –

+0

它似乎现在正在工作,我的意思是当我键入例如11,并且已经有1它会添加新的10然后如果我键入5它会保持第一个并添加4.所以关于计数数字似乎是罚款。但由于某种原因,第一个人永远不会毁灭。如果我输入例如0,所以第一个将停留。第一个从不摧毁。 –