Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modify MeshTopology with one button.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
public class MeshTopologyModifier : MonoBehaviour
{
[SerializeField] SkinnedMeshRenderer[] m_skinned;
[SerializeField] MeshFilter[] m_filters;
private void Reset()
{
SetAll();
}
[ContextMenu("Go")]
void Go()
{
for (int i = 0; i < m_skinned.Length; i++)
{
var mesh = CopyMesh(m_skinned[i].sharedMesh);
Modify(mesh);
m_skinned[i].sharedMesh = mesh;
}
for (int i = 0; i < m_filters.Length; i++)
{
var mesh = CopyMesh(m_filters[i].sharedMesh);
Modify(mesh);
m_filters[i].sharedMesh = mesh;
}
}
[ContextMenu("Set All")]
void SetAll()
{
m_skinned = GetComponentsInChildren<SkinnedMeshRenderer>();
m_filters = GetComponentsInChildren<MeshFilter>();
}
void Modify(Mesh mesh)
{
int vert = mesh.vertices.Length;
int[] indecies = new int[vert];
for (int i = 0; i < vert; i++)
{
indecies[i] = i;
}
for (int i = 0; i < mesh.subMeshCount; i++)
{
mesh.SetIndices(indecies, MeshTopology.Points, i);
}
}
static Mesh CopyMesh(Mesh mesh)
{
Mesh newmesh = new Mesh();
newmesh.vertices = mesh.vertices;
if (mesh.triangles.Length != 0) newmesh.triangles = mesh.triangles;
newmesh.uv = mesh.uv;
newmesh.normals = mesh.normals;
newmesh.colors = mesh.colors;
newmesh.tangents = mesh.tangents;
if (mesh.boneWeights != null) newmesh.boneWeights = mesh.boneWeights;
if (mesh.bindposes != null) newmesh.bindposes = mesh.bindposes;
AssetDatabase.CreateAsset(newmesh, string.Format("{0}_{1}_{2}", AssetDatabase.GetAssetPath(mesh), mesh.name, "copy.asset"));
return newmesh;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.