首页 > 解决方案 > 缩小模型后 Bounds.Encapsulate 的问题

问题描述

我有 3 个滑块,它们显示了 X、Y、Z 轴的 bounds.encapsulated 值。您可以按比例缩小或放大模型,并与游戏对象的 Unity 3D 局部比例 1 成比例。

注意GetScale()方法获取 bounds.size 并显示到滑块。

注意: SetScale()每次更改滑块的值时都会调用该方法并保存游戏对象 localScale。

public Vector3 GetScale()
{
    combineBounds = _obj.transform.GetComponent<Renderer>().bounds;
    Renderer[] renderers = _obj.transform.GetComponentsInChildren<Renderer>();
    foreach (Renderer render in renderers)
    {
        combineBounds.Encapsulate(render.bounds);
    }

    Debug.Log(combineBounds.size.x);

    return combineBounds.size;
}

public void SetScale()
{
    float xAxis = 0.0f;
    float yAxis = 0.0f;
    float zAxis = 0.0f;

    if (sca.x != combineBounds.size.x )
    {
        xAxis = 1 - (sca.x / combineBounds.size.x);
        xAxis *= -1;
    }

    if (sca.y != combineBounds.size.y)
    {
        zAxis = 1 - (sca.y / combineBounds.size.y);
        zAxis *= -1;
    }

    if (sca.z != combineBounds.size.z)
    {
        yAxis = 1 - (sca.z / combineBounds.size.z);
        yAxis *= -1;
    }

    _obj.transform.localScale = new Vector3(_obj.transform.localScale.x + xAxis, 
    _obj.transform.localScale.y + zAxis, 
    _obj.transform.localScale.z + yAxis);
}

放大效果非常好。但是按比例缩小效果并不好。

标签: c#unity3d

解决方案


推荐阅读