统一更改立方体网格的大小

时间:2018-11-27 09:49:53

标签: c# unity3d resize mesh bounds

Unity是否有更改网格的宽度,高度和深度的方法? 我知道会有改变网格顶点的方法。就像A顶点= -5,0,0和B顶点= 5,0,0一样,长度将为10。但这是很难的方法。这是一个简单的方法吗?

我试图做:

lorry.GetComponent<MeshFilter>().mesh.bounds.size.Set(1000F, 200F, 200F);

但没有任何变化。我什至不知道bound.size.set()是什么意思 因为当我打电话给Debug.Log(lorry.GetComponent<MeshFilter>().mesh.bounds.size);时 它返回(1,1,1)

我试图打电话

lorry.GetComponent<MeshFilter>().mesh.RecalculateBounds();

但是结果是一样的。

2 个答案:

答案 0 :(得分:0)

如果要在运行时更改gameObject的大小,可以更改比例。例如,将GameObject的大小增加一倍:

transform.localScale = new Vector3(2F, 2F, 2F);

根据您的评论进行编辑:

要点是我想制造一个盒子,就像一个容器。而且我必须在其中添加不同尺寸的盒子。而且我需要检查框线是否不会超出容器。

为此,您可能对使用不需要网格物体的盒子对撞机感兴趣。然后,您可以设置对撞机的大小,例如:

BoxCollider boxCol;

void Awake(){
    boxCol = otherGameObject.GetComponent<BoxCollider>();
}

void Start(){
    //Here you set the size of the collider
    boxCol.size = new Vector3(2,2,2);
}

答案 1 :(得分:0)

好吧,似乎Unity并没有提供调整网格大小的简便方法。所以我做了一个扩展:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class Extensions {
    public static void recalculateMeshByBounds(this Mesh mesh, Vector3 dimensions) {

        mesh.Clear();

        float length = dimensions.x / 2;
        float height = dimensions.y / 2;
        float width  = dimensions.z / 2;

        #region Vertices
        Vector3 p0 = new Vector3(-length, -width,  height);
        Vector3 p1 = new Vector3( length, -width,  height);
        Vector3 p2 = new Vector3( length, -width, -height);
        Vector3 p3 = new Vector3(-length, -width, -height);

        Vector3 p4 = new Vector3(-length, width,  height);
        Vector3 p5 = new Vector3( length, width,  height);
        Vector3 p6 = new Vector3( length, width, -height);
        Vector3 p7 = new Vector3(-length, width, -height);

        Vector3[] vertices = new Vector3[]
        {
            // Bottom
            p0, p1, p2, p3,

            // Left
            p7, p4, p0, p3,

            // Front
            p4, p5, p1, p0,

            // Back
            p6, p7, p3, p2,

            // Right
            p5, p6, p2, p1,

            // Top
            p7, p6, p5, p4
        };
        #endregion

        #region Normales
        Vector3 up = Vector3.up;
        Vector3 down = Vector3.down;
        Vector3 front = Vector3.forward;
        Vector3 back = Vector3.back;
        Vector3 left = Vector3.left;
        Vector3 right = Vector3.right;

        Vector3[] normales = new Vector3[]
        {
            // Bottom
            down, down, down, down,

            // Left
            left, left, left, left,

            // Front
            front, front, front, front,

            // Back
            back, back, back, back,

            // Right
            right, right, right, right,

            // Top
            up, up, up, up
        };
        #endregion

        #region UVs
        Vector2 _00 = new Vector2(0f, 0f);
        Vector2 _10 = new Vector2(1f, 0f);
        Vector2 _01 = new Vector2(0f, 1f);
        Vector2 _11 = new Vector2(1f, 1f);

        Vector2[] uvs = new Vector2[]
        {
            // Bottom
            _11, _01, _00, _10,

            // Left
            _11, _01, _00, _10,

            // Front
            _11, _01, _00, _10,

            // Back
            _11, _01, _00, _10,

            // Right
            _11, _01, _00, _10,

            // Top
            _11, _01, _00, _10,
        };
        #endregion

        #region Triangles
        int[] triangles = new int[]
        {
            // Bottom
            3, 1, 0,
            3, 2, 1,            

            // Left
            3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
            3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,

            // Front
            3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
            3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,

            // Back
            3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
            3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,

            // Right
            3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
            3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,

            // Top
            3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
            3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,

        };
        #endregion

        mesh.vertices = vertices;
        mesh.normals = normales;
        mesh.uv = uvs;
        mesh.triangles = triangles;

        mesh.RecalculateBounds();
        mesh.MarkDynamic();


    }
}

用法:  将Box原语添加到场景。  创建脚本并将其拖到Box中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class Box: MonoBehaviour
{
    public Vector3 dimensions;   

    private void Start()
    {

        this.GetComponent<MeshFilter>().sharedMesh.recalculateMeshByBounds(dimensions);
    }

    private void Update()
    {

    }
}

现在您可以访问尺寸Vector3 dimensions(length, height, width).,并且当您更改值时,顶点的向量也会更改。也许有更好的方法,但是直到现在我为我找到了最简单的方法。