简体   繁体   English

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

[英]How can I create and destroy objects using a slider?

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]);
            }
        }
    }
}

Without this part it will work fine only for creating objects: 没有这部分,它将仅对创建对象有效:

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

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

But I want to do that when the slider is moving to the right more amount of objects create more new and this is working. 但是我想这样做,当滑块向右移动时,更多的对象会创建更多的新对象,并且可以正常工作。

When moving the slider to the left less amount of objects then destroy objects for example if there are 90 objects and the slider is on value 90 and now I'm moving the slider to the left start destroy objects if I moved it from 90 to 70 it should destroy 20 objects if moved to the right again create new again. 当将滑块向左移动较少的对象时,则销毁对象,例如,如果有90个对象且滑块位于值90上,那么现在我将滑块向左移动,如果将滑块从90移至70,则开始销毁对象如果再次移到右边,它应该销毁20个对象。

I'm not sure how to do the destroy part. 我不确定如何执行销毁部分。

I tried: 我试过了:

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

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

But once there is even one object with tag "DrawPrefab" the object will be destroy even if it's not 0. I messed it up. 但是,即使有一个带有标签“ DrawPrefab”的对象,即使该对象不为0,该对象也将被销毁。我把它弄乱了。

UPDATE UPDATE

This is what I tried: 这是我尝试的:

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);
            }
        }
    }
}

When moving the slider to the right it's creating and adding new objects but when moving to the left it's giving exception: ArgumentOutOfRangeException: Argument is out of range. 向右移动滑块会创建并添加新对象,但向左移动则会产生异常:ArgumentOutOfRangeException:参数超出范围。 parameter name index. 参数名称索引。

On the lines: 在线上:

DestroySomeObjects(amountOfObjects);

And

DestroyImmediate(gos[i]);

If you always have the same amount of objects, I suggest that you enable/disable them instead of destroying them, as this is more performance friendly. 如果您总是有相同数量的对象,建议您启用/禁用它们而不是销毁它们,因为这样对性能更友好。

I would also store the created objects in a list to have a reference to them, instead of finding them afterwards with another method. 我还将创建的对象存储在列表中以对其进行引用,而不是随后使用其他方法查找它们。

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;
    }
}

That way you can use the list when destroying or doing other things. 这样,您可以在销毁或执行其他操作时使用列表。

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);
            }
        } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM