在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大小
            }
        }
    }
}
相关推荐
CoderIsArt1 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
TomEval2 天前
【Web UI 自动化】05 - KDT 模式原理与实现
前端·ui·自动化
简单Janeee2 天前
[Vue 3 从零到上线]-第九篇:更美更强——引入 UI 库与网络请求 (Axios)
网络·vue.js·ui
中国软件测试质量协会2 天前
奇林测试平台(kylinTOP)UI自动化测试能力深度解析
ui
greasyfork3 天前
Mac Sketch v2026.3 矢量图设计工具
macos·ui·sketch
zhchyun20083 天前
# 【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(06)
ui·unity·游戏引擎
中国软件测试质量协会3 天前
国产UI自动化测试工具的“技术代际“演进与自主可控之路
测试工具·ui·自动化
深念Y3 天前
NativeScript 移动端开发踩坑记录
前端·ui·vue·安卓·移动端·native·原生
晴天163 天前
从生成式 UI 到 Agent 驱动界面-Day32
ui
淼澄研学3 天前
Photoshop集成Firefly Image 3模型解析与API实操指南
ui·photoshop