记下我学untiy踩过的坑

记下我学unity踩过的坑

private List<Vector2> postionelist = new List<Vector2>();

这个东西及其难搞,好像是vector2不能使用数组类型,可能是因为他是坐标系变量的问题,数组只能满足一个整数输出,List<>则可以
写一下list的用法吧

设置list

private List<Vector2> postionelist = new List<Vector2>();

调用list

     postionelist.Clear();
        for (int x = 2; x < hang - 2; x++)
        {
            for (int y = 2; y < lie - 2; y++)
            {
                postionelist.Add(new Vector2(x, y));
            }


        }

这里是在取随机坐标

    private Vector2 Randompostion()
    {
        int positionidex = Random.Range(0, postionelist.Count);
        Vector2 pos = postionelist[positionidex];
        postionelist.RemoveAt(positionidex);
        return pos;
    }

list的自定义函数,看调出方法和数组一样

    for (int i=0;i<4;i++) {

            Vector2 poss = Randompostion();
            GameObject.Instantiate(food2[1],poss , Quaternion.identity);


        }

猜你喜欢

转载自blog.csdn.net/qq_45541497/article/details/102641808