在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大小
            }
        }
    }
}
相关推荐
黄思搏14 小时前
Unity坐标转换指南 - 3D与屏幕UI坐标互转
ui·3d·unity
覆东流2 天前
Photoshop合成的核心知识
ui·photoshop
清风wxy2 天前
Duilib_CEF桌面软件实战之Duilib编译与第一个界面程序
c++·笔记·ui·mfc
ftpeak2 天前
《Rust+Slint:跨平台GUI应用》第八章 窗体
开发语言·ui·rust·slint
知识分享小能手2 天前
jQuery 入门学习教程,从入门到精通, jQuery在HTML5中的应用(16)
前端·javascript·学习·ui·jquery·html5·1024程序员节
私人珍藏库3 天前
Adobe Photoshop CS6 Lite:PS极端简化版,压缩后大小仅50M,Photoshop精简版
ui·adobe·photoshop
再希3 天前
React+Tailwind CSS+Shadcn UI
前端·react.js·ui
覆东流4 天前
Photoshop通道的应用
ui·photoshop
我命由我123454 天前
Photoshop - Photoshop 工具栏(24)磁性套索工具
学习·ui·职场和发展·求职招聘·职场发展·课程设计·美工