当前位置: 首页 > news >正文

随机生成UI不重叠

注释

简单的随机生成UI且不发生重叠,可以修改算法进行更深入的探索

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CellInfo
{/// <summary>/// 物体位置/// </summary>public Vector2 pos;/// <summary>/// 物体宽/// </summary>public float width;/// <summary>/// 物体高/// </summary>public float height;
}/// <summary>
/// 屏幕随机生成文字并不叠加
/// </summary>public class TextTest : MonoBehaviour
{/// <summary>/// 外面的父级/// </summary>public RectTransform parent;/// <summary>/// 想要显示的子物体集合/// </summary>[Header("想要显示的子物体集合")]public List<GameObject> cells = new List<GameObject>();/// <summary>/// 已经存在的子物体信息/// </summary>private List<CellInfo> hadCells = new List<CellInfo>();/// <summary>/// 最大尝试的次数/// </summary>[Header("最大尝试的次数")]public int maxIndex;private void Update(){if (Input.GetKeyDown(KeyCode.Space)){StartCoroutine(CreateGameObject());}}/// <summary>/// 生成图片/// </summary>/// <returns></returns>public IEnumerator CreateGameObject(){int i = 0;while (i < cells.Count){float ItmeWidth = cells[i].GetComponent<RectTransform>().rect.width / 2;float ItmeHeigh = cells[i].GetComponent<RectTransform>().rect.height / 2;Vector2 cellPos = new Vector2(Random.Range(ItmeWidth, parent.rect.width - ItmeWidth),Random.Range(ItmeHeigh, parent.rect.height - ItmeHeigh));//尝试更新新坐标的次数int index = 0;while (index < maxIndex){if (i == 0 || (i != 0 && TwoPointDistance2D2(cellPos, ItmeWidth, ItmeHeigh))){CellInfo cellinfo = new CellInfo();cellinfo.pos = cellPos;cellinfo.width = ItmeWidth;cellinfo.height = ItmeHeigh;hadCells.Add(cellinfo);GameObject obj = Instantiate<GameObject>(cells[i], parent);obj.GetComponent<RectTransform>().position = cellPos;break;}index++;}i++;yield return null;}}/// <summary>/// 进行距离比较/// </summary>/// <param name="p1"></param>/// <param name="p2"></param>/// <returns></returns>private bool TwoPointDistance2D2(Vector2 currentPos, float w, float h){float x1 = currentPos.x - w;float x2 = currentPos.x + w;float y1 = currentPos.y - h;float y2 = currentPos.y + h;for (int i = 0; i < hadCells.Count; i++){float x11 = hadCells[i].pos.x - hadCells[i].width;float x22 = hadCells[i].pos.x + hadCells[i].width;float y11 = hadCells[i].pos.y - hadCells[i].height;float y22 = hadCells[i].pos.y + hadCells[i].height;if ((x2 < x11 || x1 > x22) && (y1 > y22 || y2 < y11)){continue;}else{return false;}}return true;}
}

http://www.lryc.cn/news/290722.html

相关文章:

  • 【C/C++】C/C++编程——第一个 C++ 程序:HelloWorld
  • 扩散视觉反事实算法 DVC:对抗性鲁棒分类器 + 扩散模型,跨模态对比原始的 fundus 图 VS 生成的 OCT 图
  • C++(6) 继承
  • 【Servlet】Smart Tomcat插件简化Servlet开发流程及解决常见问题
  • 解决Qt连接不上mysql数据库
  • kubernetes-快速部署一套k8s集群
  • Windows Server 安装 Docker
  • 智能分析网关V4智慧机房:视频AI智能安全监管方案
  • 一些反序列化总结
  • 分披萨(100%用例)C卷(JavaPythonC++Node.jsC语言)
  • SQL字符串截取函数【简笔记】
  • 会话技术复习笔记
  • 我用Rust开发Rocketmq name server
  • 【Deep Dive: Al Webinar】开源人工智能中赋能、透明性和可重复性三者之间的关系...
  • 将Html页面转换为Wordpress页面
  • Next.js 学习笔记(七)——样式
  • 金线检测步骤
  • 电池-电量监测基础知识
  • 西瓜书学习笔记——层次聚类(公式推导+举例应用)
  • 深度视觉目标跟踪进展综述-论文笔记
  • 【数据结构:顺序表】
  • android tts播报破音解决方案汇总
  • 2024年新提出的算法:一种新的基于数学的优化算法——牛顿-拉夫森优化算法|Newton-Raphson-based optimizer,NRBO
  • 笔记 | Clickhouse 命令行连接及查询
  • 设计模式—行为型模式之责任链模式
  • 如何使用Python+Flask搭建本地Web站点并结合内网穿透公网访问?
  • 【C语言】【力扣】刷题小白的疑问
  • 【Python】03快速上手爬虫案例三:搞定药师帮
  • C++异步编程
  • dfs专题(记忆化搜索)P1141 01迷宫——洛谷(题解)