随机生成UI不重叠

文章介绍了如何在Unity中创建一个脚本,用于在屏幕上随机生成不重叠的文字或图像对象。使用CellInfo类存储对象位置和尺寸,通过TwoPointDistance2D2函数检查新生成位置与已有对象的碰撞,确保不重叠。
摘要由CSDN通过智能技术生成

注释

简单的随机生成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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值