在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大小
            }
        }
    }
}
相关推荐
悟空爬虫-彪哥1 天前
2026 Python UI 框架选择指南:从 Streamlit 到 Pyside6 的四层体系
开发语言·python·ui
RReality1 天前
【Unity UGUI】Toggle / ToggleGroup 与 Dropdown
ui·unity·游戏引擎·图形渲染·材质
ai_coder_ai1 天前
自动化脚本ui编程之线性布局(linear)
ui·autojs·自动化脚本·冰狐智能辅助·easyclick
HYNuyoah1 天前
3X-UI Reality 搭建指南
ubuntu·ui·docker
小陈的进阶之路2 天前
web ui自动化测试
测试工具·ui
早起傻一天~G2 天前
vue2+element-UI表格封装
javascript·vue.js·ui
早起傻一天~G2 天前
vue2+element-UI上传图片封装
开发语言·javascript·ui
二妹的三爷2 天前
私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)
spring boot·后端·ui
ai_coder_ai2 天前
自动化脚本ui编程之列表框(listview)控件
ui·autojs·自动化脚本·冰狐智能辅助·easyclick
RReality3 天前
【Unity Shader URP】Matcap 材质捕捉实战教程
java·ui·unity·游戏引擎·图形渲染·材质