编辑Mesh顶点坐标

作者:追风剑情 发布于:2019-5-28 13:55 分类:Unity3d

场景截图

11111.png

22222.png

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

[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
public class MeshVertexEditor : MonoBehaviour {

    public float radius = 0.05f;

    private MeshFilter meshFilter;
    private Vector3[] vertices;
    private Transform[] points;
    private GameObject edit;
    private Transform mTransform;

    private void Awake()
    {
        mTransform = transform;
        edit = new GameObject("vertices");
        edit.transform.parent = transform;
        edit.transform.localPosition = Vector3.zero;
        edit.transform.localRotation = Quaternion.identity;
        edit.transform.localScale = Vector3.one;

        meshFilter = GetComponent<MeshFilter>();
        //调用mesh会生成一个副本
        vertices = meshFilter.mesh.vertices;
        points = new Transform[vertices.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            GameObject go = new GameObject("v"+i);
            Transform v = go.transform;
            v.parent = edit.transform;
            v.localPosition = vertices[i];
            points[i] = v;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Transform tran;
        for (int i = 0; i < points.Length; i++)
        {
            tran = points[i];
            vertices[i] = tran.localPosition;
            Gizmos.DrawSphere(tran.position, radius);
        }
        meshFilter.sharedMesh.vertices = vertices;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号