在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大小
            }
        }
    }
}
相关推荐
未定义.22139 分钟前
第4篇:企业级框架搭建,Pytest+PO模式从0到1实战
python·ui·自动化·jenkins·集成测试·pytest
尤老师FPGA9 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十四讲)
ui
小雨下雨的雨12 小时前
Flutter 框架跨平台鸿蒙开发 —— SingleChildScrollView 控件之长内容滚动艺术
flutter·ui·华为·harmonyos·鸿蒙
zFox13 小时前
五、开始项目实战(搭建完整项目)
ui·kotlin
guangzan17 小时前
AI → JSON → UI
ui·ai·zod
2501_9447114319 小时前
A2UI : 以动态 UI 代替 LLM 文本输出的方案
开发语言·前端·ui
lili-felicity19 小时前
React Native for Harmony 企业级折叠面板 (Accordion) 组件
react native·react.js·ui
Larry_Yanan19 小时前
Qt安卓开发(三)双摄像头内嵌布局
android·开发语言·c++·qt·ui
我最厉害。,。20 小时前
内网对抗-横向移动篇&入口差异&切换上线&IPC管道&AT&SC任务&Impacket套件&UI插件
ui
世人万千丶1 天前
鸿蒙跨端框架Flutter学习day 1、变量与基本类型-智能家居监控模型
学习·flutter·ui·智能家居·harmonyos·鸿蒙·鸿蒙系统