如何在Unity中编辑网格/顶点

时间:2015-09-23 07:16:31

标签: c# unity3d vertex

我想在立方体上编辑1个顶点,但我不知道如何操作。我试着到处寻找这个功能,但我无法找到解决方案。

这是我想要实现的目标的图像:

enter image description here

5 个答案:

答案 0 :(得分:1)

Unity编辑器目前没有内置的网格编辑器功能。 我可以建议您使用Prototype插件。

答案 1 :(得分:1)

您可以通过迭代顶点来轻松完成此操作,Unity将通过Vector3[]字段将您作为someObject.GetComponent<MeshFilter>().vertices提供给您。有关顶点随时间向上移动的示例,请参阅http://docs.unity3d.com/ScriptReference/Mesh-vertices.html

答案 2 :(得分:1)

http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html

这段代码不是我的。 Bellow与上面的链接代码相同。我把它分成两个文件。 (每班一个)

它工作得很好。但是在使用之前一定要保存你的场景,这有点儿麻烦。

  • 完成修改后,不要忘记退出编辑模式。

  • 您不需要添加&#34; editMesh&#34;标记到您正在修改的游戏对象,或者当您退出编辑模式时它将被删除。

  • 最后,如果您从unity修改基元,则修改将应用于该基元的每个实例! (如果更改金字塔的立方体,则每个立方体都将成为金字塔)

为避免复制原始网格物体,请在副本中更改网格渲染器中使用的网格,然后进行修改。 (下面是一个脚本,在统一菜单中添加简单的复制功能)

EditMesh.cs

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;

/// <summary>
/// http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html
/// </summary>
[AddComponentMenu("Mesh/Vert Handler")]
[ExecuteInEditMode]
public class EditMesh : MonoBehaviour {

    public bool _destroy;

    private Mesh mesh;
    private Vector3[] verts;
    private Vector3 vertPos;
    private GameObject[] handles;

    private const string TAG_HANDLE = "editMesh";

    void OnEnable() {
        mesh = GetComponent<MeshFilter>().sharedMesh; // sharedMesh seem equivalent to .mesh
        verts = mesh.vertices;
        foreach (Vector3 vert in verts) {
            vertPos = transform.TransformPoint(vert);
            GameObject handle = new GameObject(TAG_HANDLE);
            //         handle.hideFlags = HideFlags.DontSave;
            handle.transform.position = vertPos;
            handle.transform.parent = transform;
            handle.tag = TAG_HANDLE;
            handle.AddComponent<EditMeshGizmo>()._parent = this;

        }
    }

    void OnDisable() {
        GameObject[] handles = GameObject.FindGameObjectsWithTag(TAG_HANDLE);
        foreach (GameObject handle in handles) {
            DestroyImmediate(handle);
        }
    }

    void Update() {
        if (_destroy) {
            _destroy = false;
            DestroyImmediate(this);
            return;
        }

        handles = GameObject.FindGameObjectsWithTag(TAG_HANDLE);

        for (int i = 0; i < verts.Length; i++) {
            verts[i] = handles[i].transform.localPosition;
        }

        mesh.vertices = verts;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();


    }

}

#endif

EditMeshGizmo.cs

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;

/// <summary>
/// http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html
/// </summary>
[ExecuteInEditMode]
public class EditMeshGizmo : MonoBehaviour {

    private static float CURRENT_SIZE = 0.1f;

    public float _size = CURRENT_SIZE;
    public EditMesh _parent;
    public bool _destroy;

    private float _lastKnownSize = CURRENT_SIZE;

    void Update() {
        // Change the size if the user requests it
        if (_lastKnownSize != _size) {
            _lastKnownSize = _size;
            CURRENT_SIZE = _size;
        }

        // Ensure the rest of the gizmos know the size has changed...
        if (CURRENT_SIZE != _lastKnownSize) {
            _lastKnownSize = CURRENT_SIZE;
            _size = _lastKnownSize;
        }

        if (_destroy)
            DestroyImmediate(_parent);
    }

    void OnDrawGizmos() {
        Gizmos.color = Color.red;
        Gizmos.DrawCube(transform.position, Vector3.one * CURRENT_SIZE);
    }

}
#endif

CopyMesh.cs(将其放在名为&#34;编辑器&#34;的目录中)(然后您应该在菜单栏中找到它)

using UnityEditor;
using UnityEngine;

namespace Assets {

    /// <summary>
    /// 
    /// </summary>
    public class CopyMesh : MonoBehaviour {

        [MenuItem("Assets/CopyMesh")]
        static void DoCopyMesh() {
            Mesh mesh = Selection.activeObject as Mesh;
            Mesh newmesh = new Mesh();
            newmesh.vertices = mesh.vertices;
            newmesh.triangles = mesh.triangles;
            newmesh.uv = mesh.uv;
            newmesh.normals = mesh.normals;
            newmesh.colors = mesh.colors;
            newmesh.tangents = mesh.tangents;
            AssetDatabase.CreateAsset(newmesh, AssetDatabase.GetAssetPath(mesh) + " copy.asset");
        }

        [MenuItem("Assets/CopyMeshGameObject")]
        static void DoCopyMeshGameObject() {
            Mesh mesh = (Selection.activeGameObject.GetComponent<MeshFilter>()).sharedMesh;
            Mesh newmesh = new Mesh();
            newmesh.vertices = mesh.vertices;
            newmesh.triangles = mesh.triangles;
            newmesh.uv = mesh.uv;
            newmesh.normals = mesh.normals;
            newmesh.colors = mesh.colors;
            newmesh.tangents = mesh.tangents;
            print(AssetDatabase.GetAllAssetPaths()[0]);
            AssetDatabase.CreateAsset(newmesh, AssetDatabase.GetAllAssetPaths()[0] + "/mesh_copy.asset");
        }
    }
}

答案 3 :(得分:0)

尝试这个one ...

您还可以尝试使用one ...

答案 4 :(得分:0)

最近,Unity通过“包管理器”添加了对ProBuilder包的访问权限。

更多信息在这里: https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/manual/index.html