Skip to content

Instantly share code, notes, and snippets.

@HarukaKajita
Last active October 17, 2022 06:45
Show Gist options
  • Save HarukaKajita/e2e4d8c450b74b0c7c6ef95846b8643b to your computer and use it in GitHub Desktop.
Save HarukaKajita/e2e4d8c450b74b0c7c6ef95846b8643b to your computer and use it in GitHub Desktop.
UnityのMeshから点だけのMeshを作るwindow
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class PointMeshMaker : EditorWindow
{
private Mesh mesh;
private bool update = false;
[MenuItem("Utility/PointMeshMaker")]
public static void ShowWindow()
{
GetWindow<PointMeshMaker>("PointMeshMaker");
}
private void OnGUI()
{
mesh = EditorGUILayout.ObjectField(mesh, typeof(Mesh), true) as Mesh;
update = EditorGUILayout.Toggle ("Update", update);
if (GUILayout.Button("Check Topology Type"))
{
if(mesh == null) return;
var topology = mesh.GetTopology(0);
Debug.Log(topology);
}
if (GUILayout.Button("Make Point Mesh"))
{
if(mesh == null) return;
var path = AssetDatabase.GetAssetPath(mesh);
Debug.Log("Mesh Path : " + path);
var pathWay = path.Split(new[] {'/'});
var fileName = pathWay.Last().Split(new[]{'.'}).First() + "_PointMesh.asset";
var folderPath = String.Join("/", pathWay.Take(pathWay.Length - 1).ToArray());
var filePath = folderPath + "/" + fileName;
Debug.Log("Folder Path : " + folderPath);
Debug.Log("New File Path : " + filePath);
var newMesh = (Mesh)Instantiate(mesh);
var verts = newMesh.vertices;
var indices = new List<int>();
for (var i = 0; i < mesh.vertexCount; i++) {
indices.Add(i);
}
newMesh.SetIndices(indices, MeshTopology.Points, 0);
if (!update)
{
AssetDatabase.CreateAsset(newMesh, filePath);
AssetDatabase.Refresh();
Debug.Log("Saved");
}
else
{
var meshData = AssetDatabase.LoadAssetAtPath<Mesh>(filePath);
meshData.vertices = newMesh.vertices;
meshData.colors = newMesh.colors;
meshData.uv = newMesh.uv;
meshData.uv2 = newMesh.uv2;
meshData.uv3 = newMesh.uv3;
meshData.uv4 = newMesh.uv4;
meshData.SetIndices(indices, MeshTopology.Points, 0);
EditorUtility.SetDirty (meshData);
AssetDatabase.Refresh();
Debug.Log("Updated");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment