gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:06:28 27 4
gpt4 key购买 nike

我想编辑一个立方体上的 1 个顶点,但我不知道该怎么做。我试过到处寻找此功能,但找不到解决方案。

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

enter image description here

最佳答案

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

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

它工作得很好。但是请确保在使用之前保存您的场景,它有点问题。

  • 修改完成后不要忘记离开编辑模式。
  • 你不需要在你正在修改的游戏对象上添加“editMesh”标签,否则当你离开编辑模式时它会被删除。
  • 最后,如果您从 unity 修改一个基元,修改将应用于该基元的每个实例! (如果你把一个立方体换成金字塔,每个立方体都会变成一个金字塔)

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

    编辑网格.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(将其放在名为“Editor”的目录中)(然后您应该可以在菜单栏中找到它)
    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");
    }
    }
    }

    关于c# - 如何在 Unity 中编辑网格/顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32733293/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com