在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大小
            }
        }
    }
}
相关推荐
Autumn_ing13 小时前
2026国内外主流设计工具大对比:Axure、墨刀、Figma、Pixso
ui·aigc·axure·figma·墨刀
岁岁种桃花儿13 小时前
Flink从入门到上天系列第二十二篇:Flink中通过UI查看检查点
大数据·ui·flink
阿旭哟嘿14 小时前
鸿蒙 UI 语法摘要2
ui
风酥糖16 小时前
Godot游戏练习01-第14节-Theme,字体,游戏UI
游戏·ui·godot
UXbot1 天前
为什么 AI 正在重新定义 UI 设计工具的入门门槛
前端·人工智能·低代码·ui·交互·ai编程·ux
IT go1 天前
用 Trae + Stitch MCP 打造 UI 自动化产出工作流(从设计到本地预览)
ui·自动化
熙胤2 天前
Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)
spring boot·后端·ui
lpfasd1232 天前
Kubernetes UI 管理全景指南
ui·容器·kubernetes
武藤一雄2 天前
WPF UI 开发深度指南:资源 (Resources)、样式 (Style) 与触发器 (Trigger) 全解析
ui·c#·.net·wpf·.netcore·avalonia
Swift社区2 天前
ArkUI 的 UI 复用机制解析
人工智能·ui·arkui