栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity 在2D区域 随机生成2D物体 并且不重叠

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity 在2D区域 随机生成2D物体 并且不重叠

一 2d区域为父物体(涉及到UI变动自适应等)

 2d区域和2d物体 Itme的锚点设置  看自己  ,设置不同 算法 取值不同

public   Transform PosList;
        public GameObject Item;// 
void Start()
        {
         StartCoroutine( CreateTest());
        }


 List vectorY = new List();
        List vectorX = new List();
        List vector2s = new List();
        Vector2 pos = new Vector2();
        float _mapWidth;
        float _mapHight;
        public  IEnumerator CreateTest()
        {
            _mapWidth = PosList.GetComponent().rect.width;//获取ui的实际宽度
            _mapHight = PosList.GetComponent().rect.height;//长度
            Vector2 pos2D = PosList.position;
            Debug.Log("_mapWidth=" + _mapWidth + "_mapHight=" + _mapHight + "  pos2Dx=" + pos2D.x + "  pos2Dy=" + pos2D.y);
            Debug.Log("Item.transform.GetComponent().rect.width=" + Item.transform.GetComponent().rect.width );
            float ItmeWidth = Item.transform.GetComponent().rect.width/2;
            float ItmeHeigh = Item.transform.GetComponent().rect.height/2;
            for (int i = 0; i <100; i++)
            {
                do
                {
                    Debug.Log("执行do-----"+ "------i="+i);
                    Debug.Log("pos2D.x+ ItmeWidth=" + (pos2D.x + ItmeWidth)+ " _mapWidth + pos2D.x - ItmeWidth="+ (_mapWidth + pos2D.x - ItmeWidth));
                    Debug.Log("pos2D.y+ ItmeHeigh=" + (pos2D.y + ItmeHeigh) + " _mapHight + pos2D.y - ItmeHeigh=" + (_mapHight + pos2D.y - ItmeHeigh));
                    //随机 最小位置 左下角 (2d区域 x或者y + Itme的宽或者高) :保证 最小位置不出2d区域 左下角
                    //随机 最打位置 右上角 (2d区域 x或者y + 2d区域的宽或者高+Itme的宽或者高) :保证 最大位置不出2d区域 右上角
//DataTime.Now.Ticks 的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔//的间隔数,可用于较精确的计时
                     Random.InitState((int)System.DateTime.Now.Ticks);
                    pos.x = Random.Range(pos2D.x+ ItmeWidth, _mapWidth + pos2D.x - ItmeWidth);
                    pos.y = Random.Range(pos2D.y+ ItmeHeigh, _mapHight + pos2D.y - ItmeHeigh);
                    Debug.Log("pos  y=" + pos.y + "  pos  x=" + pos.x);
                    //Debug.Log("Random.seed=" + Random.seed);
                    yield return null;
                } while (test02(pos));//(!vectorY.Contains(pos.y) && !vectorX.Contains(pos.x));   || BubbleSortY(vectorX, pos.x)

                Debug.Log("pos  y="+ pos.y+ "  pos  x="+ pos.x);
                if (pos.y<0){ Debug.LogError("pos.y<0  "+ pos.y);}
                if (pos.x < 0) { Debug.LogError("pos.x<0  " + pos.x); }

                vectorY.Add(pos.y);
                vectorX.Add(pos.x);
                vector2s.Add(pos);//不重复不重叠的 添加进List
                CreateItemPrefab(Item, UILevels.Low, pos);
            }
            
        }
       
        private bool test02(Vector2 p1)
        {
            for (int i = 0; i < vector2s.Count; i++)
            {
                if (TwoPointDistance2D(p1, vector2s[i])<145)//两个 中心点  必须大于这个数  //才不重叠 Itme 正方形 宽100,(圆形最适合这个计算)
                {
                    return true;
                }
            }
            return false;
        }
        /// 
        /// 2维中如何计算两点之间的距离
        /// 
        /// 
        /// 
        /// 
        private float TwoPointDistance2D(Vector2 p1, Vector2 p2)
        {

            float i = Mathf.Sqrt((p1.x - p2.x) * (p1.x - p2.x)
                                + (p1.y - p2.y) * (p1.y - p2.y));

            return i;
        }

最后一个问题 为解决:随机到一定个数后,伪随机进入循环了  ,一直重复相近值, 无法完成100次随机

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号