Created
November 18, 2017 14:08
-
-
Save AkihiroImada/dc881d4bf858a71094bc2f639946ed3e to your computer and use it in GitHub Desktop.
Modify MeshTopology with one button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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