在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大小
            }
        }
    }
}
相关推荐
沉默金鱼9 小时前
Unity实用技能-UI进度条
ui·unity·游戏引擎
鸿蒙开发工程师—阿辉15 小时前
HarmonyOS 上下文的使用: 脱离 UI 怎么用 Context?
ui·华为·harmonyos
前端小天才16 小时前
element-ui图标偶现乱码问题的原因和修复方法
开发语言·ui·rust
UI设计兰亭妙微17 小时前
北京兰亭妙微:深耕UI/UX全流程,以大数据可视化与3D场景设计驱动数字体验升级
ui·信息可视化
hunteritself17 小时前
Adobe 把 Photoshop 搬进了 ChatGPT,免费的
gpt·机器学习·ui·adobe·chatgpt·智能手机·photoshop
Larry_Yanan1 天前
Qt多进程(五)QUdpSocket
开发语言·c++·qt·学习·ui
我命由我123451 天前
Photoshop - Photoshop 工具栏(43)标尺工具
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
开心-开心急了2 天前
ai + fluent_ui 实现自定义winUI风格窗口
flutter·ui
工业HMI实战笔记2 天前
HMI “卡成 PPT” 怎么办?—— 性能优化指南
ui·性能优化·自动化·交互
安当加密2 天前
SYP 密码管理器:基于 UI 自动化的 CS 代填如何做到“安全可用”?
安全·ui·自动化