在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大小
            }
        }
    }
}
相关推荐
qq_452396238 小时前
第二十篇:《UI自动化测试的未来:AI驱动的智能测试与低代码平台》
人工智能·低代码·ui
木风未来19 小时前
四川 UI 与数字视觉设计机构排名:政企定制服务实力榜单
ui·小程序·可视化大屏
ZC跨境爬虫21 小时前
跟着 MDN 学 HTML day_25:(数字音频概念完全解析)
前端·ui·html·edge浏览器·媒体
LNN20221 天前
半导体设备 UI 开发工程师:完整工作执行手册
开发语言·python·ui
IT空门:门主2 天前
Pixso UI + Figma + ui-ux-pro-max +ai idae工作流教程
ui·ux·figma·ai idae
qq_452396232 天前
第十八篇:《移动端UI自动化:Appium入门实战》
ui·appium·自动化
罗西的思考2 天前
【GUI-Agent】阿里通义MAI-UI 代码阅读(1)— 总体
人工智能·机器学习·ui·transformer
码途漫谈2 天前
UI-UX-Pro-Max开源项目介绍
人工智能·ui·ai·开源·ai编程·ux
比特 GOK2 天前
Qt项目ui文件中新添加的控件在代码中不识别的问题解决
开发语言·qt·ui