unity中将多个图片进行椭圆排序

  //保存需要排序的精灵容器  
    public GameObject[] Sprites;
    public Transform centrePoint;//椭圆的中心点  
    public float anglecheap = 25;//每个方块间的角度偏移  
                                 //保存位置点  
    private List<Vector3> location = new List<Vector3>();
    private float angle = 270;//第一个保证是中心位置的,当前到的角度  
    private float firstangle = 270;//记录第一个角度,用以左右对称  
    public float r = 90;//椭圆的两个弦长  
    public float R = 450;
    int size = 0;//即对象数组的一半,奇数为总长度+1/2,偶数为一半<pre name="code" class="csharp">// Use this for initialization  
    void Start()
    {//初始化size  
        if (Sprites.Length % 2 == 0)
        {
            size = Sprites.Length / 2;
        }
        else
        {
            size = (Sprites.Length + 1) / 2;
        }
        //排序分级显示  
        makespriteSort();
        //重置渲染层级  
        ResetDeep();
    }
    //给这些精灵排序显示  
    void makespriteSort()
    {
        //取出椭圆的中心点  
        Vector3 center = centrePoint.position;
        //判断该数组的个数奇偶性,如果是偶数,那么需要留出一个来放到对面  
        if (Sprites.Length % 2 == 0)
        {
            //右半边  
            for (int i = 0; i < size; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
               
                angle += anglecheap;
            }
            //第一个已经得是左边了  
            angle = firstangle - anglecheap;
            //左半边  
            for (int i = size; i < Sprites.Length - 1; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                angle -= anglecheap;
               
            }
            //最后一个  
            Sprites[Sprites.Length - 1].transform.position = getPosition(firstangle - 180, center);
           
            return;
        }
        //如果不是偶数,那么出去中间那个,正好正常显示  
        else
        {
            //右半边  
            for (int i = 0; i < size; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
              
                angle += anglecheap;
            }
            //第一个已经得是左边了  
            angle = firstangle - anglecheap;
            //左半边  
            for (int i = size; i < Sprites.Length; i++)
            {
                Sprites[i].transform.position = getPosition(angle, center);
                
                angle -= anglecheap;
            }
            return;
        }

    }
    //获取当前角度的坐标  
    Vector3 getPosition(float _angle, Vector3 _centerposition)
    {
        float hudu = (_angle / 180f) * Mathf.PI;
        float cosx = Mathf.Cos(hudu);
        float sinx = Mathf.Sin(hudu);
        float x = _centerposition.x + R * cosx;
        float y = _centerposition.y + r * sinx;
        Vector3 point = new Vector3(x, y, 0);
        //添加到容器保存  
        location.Add(point);
        return point;
    }

    //根据当前左右容器调整所有控件的渲染层级  
    void ResetDeep()
    {
       int  dep = 0;
        //右半边  
        for (int i = size - 1; i >= 0; i--)
        {
            Sprites[i].GetComponent<Transform>().SetSiblingIndex(dep);
            dep++;
        }
        //dep = 0;
        //左半边  
        for (int i = Sprites.Length - 1; i >= size; i--)
        {
            Sprites[i].GetComponent<Transform>().SetSiblingIndex(dep);
            dep++;
        }
    }

 

posted @ 2017-11-10 16:19  U3DEngineer  阅读(735)  评论(0编辑  收藏  举报