在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大小
            }
        }
    }
}
相关推荐
信徒favor13 小时前
我用AI一次性生成3种UI原型(附提示词)
ui
ll_god1 天前
android compose ui 结合 ViewModel适配方案
android·ui
SoraLuna1 天前
KuiklyUI for OpenHarmony 实战 01:源码构建与运行(Mac)
macos·ui·鸿蒙
雨季6661 天前
构建交互式响应式页面:Flutter 在 OpenHarmony 上的动态 UI 实践
flutter·ui
紫雾凌寒1 天前
【 HarmonyOS 高频题】2026 最新 ArkUI 开发与组件面试题
ui·华为·面试·程序员·职场发展·harmonyos·ark-ui
bugcome_com1 天前
WPF 数据模板(DataTemplate):优雅实现数据与 UI 的解耦
ui·wpf
是席木木啊1 天前
RuoYi-Vue-Plus UI前端页面布局调整
前端·vue.js·ui
UXbot1 天前
UI设计工具推荐合集
前端·人工智能·ui
雨季6662 天前
使用 Flutter for OpenHarmony 构建基础 UI 组件布局:从 Scaffold 到 Container 的实战解析
flutter·ui
会一点设计2 天前
工作总结PPT模板设计指南:从结构到排版的完整解析
ui·powerpoint·ux·ppt