在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 小时前
极其灵活且敏捷的WPF组态控件ConPipe 2026
前端·物联网·ui·wpf
小笔学长3 小时前
React 入门:构建交互式 UI 的基础
ui·项目实战·前端开发·react框架·jsx语法
zhaocarbon3 小时前
VUE 4向云台 8向云台UI
css·vue.js·ui
阿蔹3 小时前
UI测试自动化-Web-Python-Appium
前端·python·ui·appium·自动化
lifewange12 小时前
UI自动化页面元素定位有几种方式
前端·ui·自动化
我送炭你添花1 天前
Pelco KBD300A 模拟器:06+4.KBD300A 键盘 UI 布局的设计与响应式实现(二次迭代)
ui·计算机外设
qq_428639612 天前
ue无双割草项目:4.让AI受伤及添加UI
ui·ue·无双割草
清水迎朝阳2 天前
Qt 小白成长系列 2 QUiLoader 动态加载UI
ui
sandwu2 天前
AI自动化测试(二)—— Playwright-MCP搭建自动化UI测试(browser-use&midscene对比)
人工智能·ui·自动化·playwright
weixin_439706252 天前
flowable 6.8 + flowable ui + spring boot的例子
spring boot·后端·ui