Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一改变立方体网格的大小_C#_Unity3d_Resize_Mesh_Bounds - Fatal编程技术网

C# 统一改变立方体网格的大小

C# 统一改变立方体网格的大小,c#,unity3d,resize,mesh,bounds,C#,Unity3d,Resize,Mesh,Bounds,Unity是否有更改网格宽度、高度和深度的方法? 我知道有办法改变网格顶点。就像A vertice=-5,0,0和B vertice=5,0,0,那么长度应该是10。但这是一条艰难的道路。这是一个简单的方法吗 我试着做: lorry.GetComponent<MeshFilter>().mesh.bounds.size.Set(1000F, 200F, 200F); lorry.GetComponent().mesh.bounds.size.Set(1000F,200F,200F)

Unity是否有更改网格宽度、高度和深度的方法? 我知道有办法改变网格顶点。就像A vertice=-5,0,0和B vertice=5,0,0,那么长度应该是10。但这是一条艰难的道路。这是一个简单的方法吗

我试着做:

lorry.GetComponent<MeshFilter>().mesh.bounds.size.Set(1000F, 200F, 200F);
lorry.GetComponent().mesh.bounds.size.Set(1000F,200F,200F);
但一切都没有改变。我甚至不明白什么是
bound.size.set()
因为当我调用Debug.Log(lorry.GetComponent().mesh.bounds.size)时 它返回(1,1,1)

我试着打电话

lorry.GetComponent().mesh.RecreacteBounds()


但是结果是一样的。

如果你想在运行时改变游戏对象的大小,你可以改变比例。例如,要将游戏对象的大小增加一倍:

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);
}
BoxCollider-boxCol;
无效唤醒(){
boxCol=otherGameObject.GetComponent();
}
void Start(){
//在这里,您可以设置碰撞器的大小
boxCol.size=新矢量3(2,2,2);
}

好的,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();


    }
}
用法: 将长方体基本体添加到场景中。 创建脚本并拖动到框中:

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()
    {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[执行编辑模式]
公共类框:单一行为
{
公共向量3维;
私有void Start()
{
this.GetComponent().sharedMesh.RecreacteMeshByBounds(维度);
}
私有void更新()
{
}
}

现在您可以访问维度
Vector3维度(长度、高度、宽度)。
当您更改值时,vertice的向量也会更改。也许有更好的方法,但直到现在我才发现最简单的方法。

你真的需要更改“网格顶点”的位置,或者缩放网格也没关系?最好是更改网格垂直点。重点是我想制作一个长方体,像一个容器。我必须添加不同尺寸的盒子。我需要检查框边界是否没有超出容器。有了边界函数,我可以轻松准确地实现,我认为改变边界是行不通的(甚至不应该有改变边界的选项)。边界是基于网格计算的,而不是基于边界的网格。Ignacio检查注释,Marius不想更改比例,只想更改网格顶点positions@Lotan谢谢你指出。我回答了一个可能的方法,但没有触及网格的顶点,我不确定是否可以独立于脚本访问网格以进行回放,但使用碰撞器进行检查是一个坏主意。因为对撞机只返回布尔值。@马吕斯,但我知道你们只想检查一个盒子是否在容器外面。这是一个是/否条件。那个么你们还需要什么其他信息呢?若盒子在容器中,那个么总是返回true。例如,若半框在容器外,它将再次返回true。这就是为什么我需要检查一些盒子是否在外面