使用先知/ Box2D的的DebugDraw在Unity3D最佳方式吗?

问题描述 投票:0回答:1

的Box2D /先知2D物理具有绘制图元使用(线,多边形,填充,颜色)的物理世界的简单表示的有用成分。下面是一个例子:

enter image description here

是什么在Unity3D完成这一任务的最好方法是什么?有没有一种简单的方法来渲染填充,线,点等多边形?如果是的话,我可以实现DebugDraw与统一的API接口,但我无法找到如何实现基本呈现这样与统一。

我明白这将是在三维空间中,但我只是零出一个轴,基本上把它作为2D。

unity3d box2d
1个回答
1
投票

如果你真正的意思只是显示在SceneView调试箱不在GameView可以使用Gizmos.DrawWireCube

void OnDrawGizmos()
{
    //store original gizmo color
    var color = Gizmos.color; 

    // store original matrix
    var matrix = Gizmos.matrix;

    // set gizmo to local space
    Gizmos.matrix = transform.localToWorldMatrix;

    // Draw a yellow cube at the transform position
    Gizmos.color = Color.yellow;

    // here set the scale e.g. for a "almost" 2d box simply use a very small z value
    Gizmos.DrawWireCube(transform.position, new Vector3(0.5f, 0.2f, 0.001f));

    // restor matrix
    Gizmos.matrix = matrix;

    // restore color
    Gizmos.color = color;
}

您可以使用OnDrawGizmosSelected显示小玩意儿只有选择游戏对象

enter image description here

你也可以通过获取箱尺寸在检查扩展此

[SerializeField] private Vector3 _boxScale;

并使用

Gizmos.DrawWireCube(transform.position, _boxScale);
© www.soinside.com 2019 - 2024. All rights reserved.