如何在APK构建版本中像编辑器模式一样显示碰撞体?
在APK构建版本中显示碰撞体的可行方案
Unity 实现方案
方案1:自定义Gizmos绘制
- 编写脚本在运行时绘制碰撞体轮廓,替代编辑器Gizmos的显示逻辑:
- 创建脚本
DrawColliders.cs:using UnityEngine; public class DrawColliders : MonoBehaviour { public bool showColliders = true; public Color colliderColor = Color.cyan; void Update() { if (!showColliders) return; Gizmos.color = colliderColor; // 遍历场景中所有带碰撞体的对象 foreach (var col in FindObjectsOfType<Collider>()) { DrawSpecificCollider(col); } } void DrawSpecificCollider(Collider col) { switch (col) { case BoxCollider box: Gizmos.matrix = col.transform.localToWorldMatrix; Gizmos.DrawWireCube(box.center, box.size); break; case SphereCollider sphere: Gizmos.DrawWireSphere(col.transform.position + sphere.center, sphere.radius); break; case CapsuleCollider capsule: Vector3 top = col.transform.TransformPoint(capsule.center + Vector3.up * (capsule.height/2 - capsule.radius)); Vector3 bottom = col.transform.TransformPoint(capsule.center - Vector3.up * (capsule.height/2 - capsule.radius)); Gizmos.DrawWireSphere(top, capsule.radius); Gizmos.DrawWireSphere(bottom, capsule.radius); Gizmos.DrawLine(top + col.transform.right*capsule.radius, bottom + col.transform.right*capsule.radius); Gizmos.DrawLine(top - col.transform.right*capsule.radius, bottom - col.transform.right*capsule.radius); Gizmos.DrawLine(top + col.transform.forward*capsule.radius, bottom + col.transform.forward*capsule.radius); Gizmos.DrawLine(top - col.transform.forward*capsule.radius, bottom - col.transform.forward*capsule.radius); break; // 可扩展支持MeshCollider、WheelCollider等类型 } } } - 将脚本挂载到场景根物体,构建APK前确保
showColliders为true,运行时就会渲染碰撞体轮廓。
- 创建脚本
方案2:使用Runtime Gizmos插件
- 直接选用Unity Asset Store中的第三方插件(如Runtime Gizmos),这类插件已封装好运行时显示Gizmos的逻辑,只需简单配置就能开启碰撞体可视化,无需大量自定义代码。
Unreal Engine 实现方案
方案1:自定义Debug绘制
- 通过引擎自带的Debug接口在运行时绘制碰撞体:
- 在Actor头文件中添加配置项:
#include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "DrawDebugHelpers.h" #include "ColliderDrawer.generated.h" UCLASS() class YOURPROJECT_API AColliderDrawer : public AActor { GENERATED_BODY() public: AColliderDrawer(); UPROPERTY(EditAnywhere, Category = "Debug") bool bShowColliders = true; UPROPERTY(EditAnywhere, Category = "Debug") FColor ColliderColor = FColor::Cyan; protected: virtual void Tick(float DeltaTime) override; }; - 在cpp文件中实现绘制逻辑:
#include "ColliderDrawer.h" AColliderDrawer::AColliderDrawer() { PrimaryActorTick.bCanEverTick = true; } void AColliderDrawer::Tick(float DeltaTime) { Super::Tick(DeltaTime); if (!bShowColliders) return; TArray<UPrimitiveComponent*> AllColliders; GetWorld()->GetAllComponentsOfClass(UPrimitiveComponent::StaticClass(), AllColliders); for (auto Collider : AllColliders) { if (Collider->IsCollisionEnabled()) { FBounds Bounds = Collider->GetBounds(); DrawDebugBox(GetWorld(), Bounds.GetCenter(), Bounds.GetExtent(), Collider->GetComponentQuat(), ColliderColor, false, -1.f, 0, 2.f); // 针对球体、胶囊体可替换为DrawDebugSphere、DrawDebugCapsule } } } - 打包APK时开启
bShowColliders,运行后就能看到碰撞体轮廓。
- 在Actor头文件中添加配置项:
方案2:使用控制台命令
- 打包时在项目设置中开启
Allow Console,APK运行时通过控制台输入stat collision,即可快速开启引擎自带的碰撞体显示功能,无需修改代码。
通用注意事项
- 调试完成后务必关闭碰撞体显示,避免额外性能开销影响游戏体验。
- 可添加触发开关(比如双击屏幕、特定按键),方便在APK中随时开启/关闭可视化。
内容的提问来源于stack exchange,提问作者Phạm Hoài Lâm
相关产品推荐
相关产品推荐

