在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大小
            }
        }
    }
}
相关推荐
小樱花的樱花2 小时前
4 文件选择对话框 QFileDialog
开发语言·c++·ui
jingxindeyi5 小时前
electron 配置 shadcn-ui
javascript·ui·electron
可达鸭小栈5 小时前
易语言自绘UI实战:基于美易模块的登录界面快速开发(可换肤)
ui
lpfasd1235 小时前
Flutter 2026:从跨平台UI到AI原生全栈开发平台的蜕变
flutter·ui·ai-native
HwJack207 小时前
HarmonyOS UI 开发中的 EventHub:终结“回调地狱”的通信轻骑兵
ui·华为·harmonyos
余人于RenYu11 小时前
Claude + Figma MCP
前端·ui·ai·figma
yiruwanlu1 天前
乡村文旅设计师推荐:建筑设计能力筛选要点解析
python·ui
jf加菲猫1 天前
第12章 数据可视化
开发语言·c++·qt·ui
麒麟ZHAO1 天前
鸿蒙flutter第三方库适配 - 服务端驱动UI应用
flutter·ui·华为·harmonyos
报错小能手1 天前
SwiftUI 框架 认识 SwiftUI 视图结构 + 布局
ui·ios·swift