Skip to content

Instantly share code, notes, and snippets.

@AkihiroImada
Created November 18, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkihiroImada/dc881d4bf858a71094bc2f639946ed3e to your computer and use it in GitHub Desktop.
Save AkihiroImada/dc881d4bf858a71094bc2f639946ed3e to your computer and use it in GitHub Desktop.
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