在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大小
            }
        }
    }
}
相关推荐
天天进步201512 小时前
UI-TARS 源码解析 #22:二次开发实战:用 UI-TARS 做一个简单的 Windows 自动操作 Agent
windows·ui
腾科IT教育2 天前
HarmonyOS开发|ArkTS UI颜色API通用规则
ui·华为·harmonyos·harmonyos开发·鸿蒙应用开发工程师
小小工匠2 天前
把生成模型接进 Agent:Higgsfield MCP 的架构拆解与工程实践
ui·higgsfield mcp·impeccable
czhaii2 天前
word插入表格排版创建或编辑应用技巧
ui·c#·xhtml
淼澄研学3 天前
OpenAI集成Photoshop API:函数调用机制解析与实操指南
ui·photoshop
学习使我健康3 天前
一个基于 **Jetpack Compose + Material 3** 的 Android 入门示例项目,演示 Compose 声明式 UI 的核心用法
ui·kotlin·android jetpack
一夜白头催人泪3 天前
从0到1搭建AI驱动的UI自动化:无代码DOM设计文档
人工智能·ui·自动化
星辰即远方3 天前
计算器仿写总结
学习·ui·ios·cocoa·xcode
天天进步20154 天前
UI-TARS 源码解析 #21:从 UI-TARS 到本地桌面自动化:如何接入截图、模型推理和动作执行?
运维·ui·自动化
飞凌嵌入式4 天前
告别多端重复开发:Buildroot+Flutter实现全平台UI风格统一
flutter·ui