在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大小
            }
        }
    }
}
相关推荐
程序媛_MISS_zhang_01102 天前
vant-ui中List 组件可以与 PullRefresh 组件结合使用,实现下拉刷新的效果
java·linux·ui
霍格沃兹测试学院-小舟畅学2 天前
AI智能体实现自主化UI回归测试全解析 Playwright+MCP
人工智能·ui
MediaTea2 天前
Photoshop 调整命令使用详解(合集 · 2026版)
ui·photoshop
霍格沃兹测试开发学社测试人社区3 天前
揭开帷幕:如何实现UI回归测试的全面自主化
人工智能·ui·自动化
半吊子全栈工匠3 天前
软件产品的10个UI设计技巧及AI 辅助
人工智能·ui
Vanranrr4 天前
表驱动编程实战:让 UI 逻辑既清晰又好维护
c++·ui
lsp程序员0104 天前
使用 Web Workers 提升前端性能:让 JavaScript 不再阻塞 UI
java·前端·javascript·ui
咖啡の猫4 天前
PC 端常用 UI 组件库
ui
我的xiaodoujiao4 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
top_designer5 天前
Firefly 样式参考:AI 驱动的 UI 资产“无限”生成
前端·人工智能·ui·aigc·ux·设计师