Unity如何实现很多按钮在圆周上动态排列

 
 

实现的效果是:用鼠标点击屏幕,然后以鼠标为中心,按钮均匀的排在四周

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

public class ttt : MonoBehaviour {
    public List<GameObject> jihe = new List<GameObject>();
    public int num = 4;
    public GameObject parentt;
    private GameObject testObj;

    public GameObject qiu;

	// Use this for initialization
	void Start () {

        GameObject btn = Resources.Load("Button") as GameObject;
        for(int i = 0; i < num; i++)
        {
           GameObject m= Instantiate(btn);
            m.transform.parent = parentt.transform;
            m.SetActive(false);
            jihe.Add(m);
        }
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            if (testObj == null)
                testObj = new GameObject();
         
            testObj.transform.position =Input.mousePosition;
            testObj.transform.rotation = Quaternion.Euler(0, 0, 0);

            qiu.transform.position = Input.mousePosition;

            float spacing = 360 / num; //几等分

            List<Vector3> kk = new List<Vector3>();



            for (int i = 0; i < num; i++)
            {
                Vector3 pos = testObj.transform.TransformPoint(new Vector3(0, -48, 0));//局部转世界
                kk.Add(pos);
                testObj.transform.Rotate(Vector3.forward * spacing);
            }



            for(int iii = 0; iii < jihe.Count; iii++)
            {
                jihe[iii].transform.position = kk[iii];
                jihe[iii].SetActive(true);
            }



        }
	}
}

----------------

----------------


之所以写这篇文章的目的,是这篇文章提供一个新的方法,实现一点为中心,物体均匀排在这个中心四周

---------------

用到局部转世界的方法

---------------



FR:徐海涛(hunk Xu)  QQ技术交流群:386476712)

猜你喜欢

转载自blog.csdn.net/qq_15267341/article/details/80327406