如何使用按钮单击和传递值从列表中删除值?

时间:2018-10-26 08:04:06

标签: c# unity3d

    private List<int> modelid = new List<int>();
    private List<int> modelrefno = new List<int>();
    private List<string> modelname = new List<string>();

int touchescount0 = 0;
int touchescount1 = 0;
int touchescount2 = 0;

当我单击每个按钮时,下面给出了三个按钮方法,一个键值被传递到另一个位置,最后到达Listadd()方法。

public void AddPath()

{

key = 0;

}

public void AddModel()

{

key = 1;

}
public void AddNewmodel()

{

key = 2;

}

该值到达以下方法,并实例化以下模型。将模型信息添加到modelid,modelrefno和modelname的不同列表中。

public GameObject Listadd(ModelInfo info)


{


    key = info.shapeType;

if (key == 0)
        {

            touchescount0 = touchescount0 + 1;


            shape = Instantiate(models[key], new Vector3(info.px, info.py, info.pz), Quaternion.Euler(info.qx, info.qy, info.qz));


            modelid.Add(0);
            modelrefno.Add(touchescount0);
            shape.name = "Path" + " " + touchescount0;
            modelname.Add(shape.name);
            Debug.Log("Model Details " + modelid[0] + "-" + modelrefno[0] + "-" + modelname[0]);



        }
        else if (key == 1)
        {

            touchescount1 = touchescount1 + 1;

            GameObject objs = Instantiate(models[key], new Vector3(info.px, info.py, info.pz), Quaternion.Euler(info.qx, info.qy, info.qz));
            shape = objs;


            modelid.Add(1);
            modelrefno.Add(touchescount1);

            shape.name = "SBlue" + " " + touchescount1;
            modelname.Add(shape.name);


        }



else if (key == 2)
            {

            touchescount2 = touchescount2 + 1;

            GameObject objs = Instantiate(models[key], new Vector3(info.px, info.py, info.pz), Quaternion.Euler(info.qx, info.qy, info.qz));
            shape = objs;


            modelid.Add(1);
            modelrefno.Add(touchescount2);

            shape.name = "SBlue" + " " + touchescount2;
            modelname.Add(shape.name);


        }


}

    }

现在,当我使用删除按钮删除时,将使用以下方法。在这里,我想继续删除最后保存的模型信息。如果我两次单击删除,最后保存的两个模型信息将被删除。当我添加一个模型并删除,因此它将删除,但是如果我添加多个模型,则键值将是最后添加的模型。因此,当我尝试删除信息时,该信息仍然存在。如何从列表中删除多个保留的信息。

 public void DeleteShapes()
    {
            if (key == 0 && touchescount0 > 0)
            {
                touchescount0 = touchescount0 - 1;
            }
            if (key == 1 && touchescount1 > 0)
            {
                touchescount1 = touchescount1 - 1;
                Debug.Log("While Deleting SMblue = " + touchescount1);
            }

            if (key == 2 && touchescount2 > 0)
            {
                touchescount2 = touchescount2 - 1;

            }
            if(modelid.Count>0)
            {
                modelid.RemoveAt(modelid.Count - 1);

            }

            if (modelrefno.Count > 0)
            {
                modelrefno.RemoveAt(modelrefno.Count - 1);
            }

            if (modelname.Count > 0)
            {

                modelname.RemoveAt(modelname.Count - 1);
            }
            //Debug.Log("Model Details " + modelid[0] + "-" + modelrefno[0] + "-" + modelname[0]);


        }

1 个答案:

答案 0 :(得分:1)

您可以使用Stack。很简单。

如果要在顶部添加内容,请使用:

myStack.Push(myobject);

并从堆栈顶部删除某些内容:

myStack.Pop();

它也快很多,所以...没有缺点。