在ui上使用boxcollider的问题

  • 问题就是boxCollider不会随着UI缩放而缩放
  • 这个问题也很简单
  • 下面脚本可以实现随着recttransform来同步设置boxcollider
csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxColliderAdjust : MonoBehaviour
{
    public bool AdjustBoxCollider = false;
    private BoxCollider2D boxCollider2D;
    private RectTransform gameObject;
    // Use this for initialization
    void Start()
    {
        gameObject = this.GetComponent<RectTransform>();
        boxCollider2D = this.GetComponent<BoxCollider2D>();
    }
    // Update is called once per frame
    void Update()
    {
        if (boxCollider2D == null)
        {
            Debug.Log("can't find collider");
            return;
        }
        else
        {
            if (AdjustBoxCollider == true)
            {
                boxCollider2D.offset = gameObject.rect.center;      //把box collider设置到物体的中心
                boxCollider2D.size = new Vector2(gameObject.rect.width, gameObject.rect.height);    //改变collider大小
            }
        }
    }
}
相关推荐
xinyu_Jina11 小时前
Info Flow:大规模列表渲染中的UI虚拟化、数据懒-加载与前端性能工程
前端·ui
鼎道开发者联盟1 天前
构建活的界面:AIGUI底板的动态布局
人工智能·ui·ai·aigc·gui
Lan.W1 天前
element UI + vue2 + html实现堆叠条形图 - 横向分段器
前端·ui·html
尤老师FPGA1 天前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十二讲)
android·java·ui
阿蔹1 天前
UI测试自动化--Web--Python_Selenium-元素定位
前端·ui·自动化
IT古董1 天前
【前端】Headless UI 深度实战:构建可访问、可定制的现代前端组件
前端·ui
hpz12231 天前
对Element UI 组件的二次封装
javascript·vue.js·ui
jllllyuz2 天前
基于ASP.NET Core SignalR实现实时消息提醒与聊天功能
ui·asp.net·xhtml
梦想的旅途22 天前
RPA 脚本的“自愈”能力:应对企微 UI 频繁更新
ui·企业微信·rpa