如何使用滑块创建和销毁对象?

时间:2018-03-30 11:39:14

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class GameManager : MonoBehaviour
{
    public GameObject prefabToDraw;
    [Range(0, 100)]
    public int amountOfObjects;

    private int amountCopy;

    // Use this for initialization
    void Start()
    {
        amountCopy = amountOfObjects;
        prefabToDraw.tag = "DrawPrefab";
    }

    // Update is called once per frame
    void Update()
    {
        if (amountOfObjects > amountCopy && amountCopy != amountOfObjects)
        {
            for (int i = 0; i < amountOfObjects; i++)
            {
                Instantiate(prefabToDraw);
            }

            amountCopy = amountOfObjects;
        }

        if (amountOfObjects == 0)
        {
            var gos = GameObject.FindGameObjectsWithTag("DrawPrefab");

            for (int i = 0; i < gos.Length; i++)
            {
                DestroyImmediate(gos[i]);
            }
        }
    }
}

如果没有这部分,它只能用于创建对象:

if (amountOfObjects == 0)
            {
                var gos = GameObject.FindGameObjectsWithTag("DrawPrefab");

                for (int i = 0; i < gos.Length; i++)
                {
                    DestroyImmediate(gos[i]);
                }
            }

但是当滑块向右移动时,我希望这样做,更多的对象会创建更多的新对象,这是有效的。

当滑块向左移动较少量的物体然后销毁物体,例如,如果有90个物体且滑块的值为90,现在我将滑块向左移动,如果我移动则开始销毁物体它从90到70它应该销毁20个对象,如果向右移动再次创建新的。

我不知道怎么做破坏部分。

我试过了:

if (amountOfObjects == 0)
        {
            var gos = GameObject.FindGameObjectsWithTag("DrawPrefab");

            for (int i = 0; i < gos.Length; i++)
            {
                DestroyImmediate(gos[i]);
            }
        }

但是一旦有一个带有标签的对象&#34; DrawPrefab&#34;即使它不是0,物体也会被破坏。我搞砸了。

更新

这就是我的尝试:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class GameManager : MonoBehaviour
{
    public GameObject prefabToDraw;
    [Range(0, 100)]
    public int amountOfObjects;

    private int amountCopy;
    private List<GameObject> gos = new List<GameObject>();

    // Use this for initialization
    void Start()
    {
        amountCopy = amountOfObjects;
    }

    // Update is called once per frame
    void Update()
    {
        CreateObjects();
        DestroySomeObjects(amountOfObjects);
    }

    private void CreateObjects()
    {
        if (amountOfObjects > amountCopy && amountCopy != amountOfObjects)
        {
            for (int i = 0; i < amountOfObjects; i++)
            {
                GameObject go = Instantiate(prefabToDraw);
                gos.Add(go);
            }

            amountCopy = amountOfObjects;
        }
    }

    private void DestroySomeObjects(int amountFromRightSide)
    {
        if (gos.Count != 0)
        {
            for (int i = gos.Count - 1; i <= gos.Count - amountFromRightSide; i--)
            {
                DestroyImmediate(gos[i]);
            }
        }
    }

    private void DestroyAllObjects()
    {
        if (gos.Count != 0)
        {
            foreach (GameObject go in gos)
            {
                DestroyImmediate(go);
            }
        }
    }
}

当向右移动滑块时,它会创建并添加新对象,但当向左移动时,它会给出异常:ArgumentOutOfRangeException:参数超出范围。参数名称索引。

在线:

DestroySomeObjects(amountOfObjects);

DestroyImmediate(gos[i]);

1 个答案:

答案 0 :(得分:1)

如果您总是拥有相同数量的对象,我建议您启用/禁用它们而不是销毁它们,因为这样会更加友好。

我还会将创建的对象存储在列表中以引用它们,而不是之后使用其他方法查找它们。

List<GameObject> gos = new List<GameObject>();
void CreateObjects()
{
    if (amountOfObjects > amountCopy && amountCopy != amountOfObjects)
    {
        for (int i = 0; i < amountOfObjects; i++)
        {
            GameObject go = Instantiate(prefabToDraw);
            gos.Add(go);
        }

        amountCopy = amountOfObjects;
    }
}

这样你就可以在销毁或做其他事情时使用这个列表。

void DestroySomeObjects(int amountFromRightSide)
{
    if (gos.Count != 0)
        {
            for (int i = gos.Count-1; i <= gos.Count-amountFromRightSide; i--)
            {
                DestroyImmediate(gos[i]);
            }
        } 
}

void DestroyAllObjects()
{
    if (gos.Count != 0)
        {
            foreach (GameObject go in gos)
            {
                DestroyImmediate(go);
            }
        } 
}