在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 天前
【设计模式精讲】25.状态模式(State)
c++·ui·设计模式·状态模式·uml
插件开发3 天前
Illustrator 插件开发:链接文件存在检查的原理与实战
ui·illustrator
xy34533 天前
axure9.0 如何打造一个计时器(简单版)
前端·ui·html·axure·原型·产品设计
图扑软件3 天前
下篇・换墨|主题/多语言/移动端,一套组件全覆盖
前端·javascript·ui·性能优化·数据可视化
object not found5 天前
从 Page Design 到 System Design:我对 UI/UX 的一次认知变化
ui·ux
xy34535 天前
Axure9.0中继器遮罩实现方法
前端·ui·html·axure·原型·产品设计
晴天165 天前
Ant Design UI 库核心原理与应用
ui
玫瑰互动GEO5 天前
腾讯AnswerBit(GEO优化监测平台)技术拆解:UI自动化如何采集真实AI回答
大数据·人工智能·ui·ai·自动化·geo优化
摸鱼仙人~5 天前
Qwen-Code ACP与AG-UI协议深度解析:区别、场景与实战报文对比
ui
图扑软件6 天前
中篇・运笔|统一 DataModel 底座,HT UI 组件万物同源
javascript·低代码·ui·性能优化·数据可视化