在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大小
            }
        }
    }
}
相关推荐
架构文摘JGWZ1 天前
Sun-Panel:简洁且美观的导航首页开源项目!!
ui·开源·开源软件·工具
laimaxgg1 天前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
类人_猿3 天前
PhotoShop批处理
ui·photoshop·批处理·photoshop批处理
PM大明同学3 天前
Axure PR 9 中继器 02 分页提示
ui·交互·产品经理·axure
十秒耿直拆包选手3 天前
cmake:定位Qt的ui文件
c++·qt·ui·cmake
engchina3 天前
使用 Vite + React 19 集成 Tailwind CSS 与 shadcn/ui 组件库完整指南
css·react.js·ui·vite·tailwind·react 19·shadcn
1024小神3 天前
ios苹果手机使用AScript应用程序实现UI自动化操作,非常简单的一种方式
运维·ui·自动化
画月的亮3 天前
element-ui 使用过程中遇到的一些问题及解决方法
javascript·vue.js·ui
Jackson@ML4 天前
React入门 – 1. 学习React的预备知识
javascript·react.js·ui
laimaxgg4 天前
Qt常用控件之按钮QPushButton
开发语言·c++·qt·ui·qt5