Skip to content

Instantly share code, notes, and snippets.

@Narazaka
Last active January 9, 2024 09:18
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 Narazaka/23c640c3466e478949f7c04417492f9f to your computer and use it in GitHub Desktop.
Save Narazaka/23c640c3466e478949f7c04417492f9f to your computer and use it in GitHub Desktop.
NDMFMantisLODCustomEditor.cs
MIT
https://booth.pm/ja/items/5409262
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace MantisLODEditor.ndmf
{
[CustomEditor(typeof(NDMFMantisLODEditor))]
public class NDMFMantisLODCustomEditor : Editor
{
private bool m_isPreview;
private Dictionary<Component, Mesh> m_originalMeshes;
private bool m_applied;
SerializedProperty m_protectBoundary;
SerializedProperty m_protectDetail;
SerializedProperty m_protectSymmetry;
SerializedProperty m_protectNormal;
SerializedProperty m_protectShape;
SerializedProperty m_useDetailMap;
SerializedProperty m_detailBoost;
SerializedProperty m_quality;
private void OnEnable()
{
m_protectBoundary = serializedObject.FindProperty("protect_boundary");
m_protectDetail = serializedObject.FindProperty("protect_detail");
m_protectSymmetry = serializedObject.FindProperty("protect_symmetry");
m_protectNormal = serializedObject.FindProperty("protect_normal");
m_protectShape = serializedObject.FindProperty("protect_shape");
m_useDetailMap = serializedObject.FindProperty("use_detail_map");
m_detailBoost = serializedObject.FindProperty("detail_boost");
m_quality = serializedObject.FindProperty("quality");
}
public override void OnInspectorGUI()
{
serializedObject.UpdateIfRequiredOrScript();
EditorGUILayout.PropertyField(m_protectBoundary);
EditorGUILayout.PropertyField(m_protectDetail);
EditorGUILayout.PropertyField(m_protectSymmetry);
EditorGUILayout.PropertyField(m_protectNormal);
EditorGUILayout.PropertyField(m_protectShape);
EditorGUILayout.PropertyField(m_useDetailMap);
EditorGUILayout.PropertyField(m_detailBoost);
EditorGUILayout.PropertyField(m_quality);
if (serializedObject.hasModifiedProperties)
{
m_applied = false;
}
serializedObject.ApplyModifiedProperties();
var mantis = target as NDMFMantisLODEditor;
m_originalMeshes = m_originalMeshes ?? mantis.GetMesh();
if (GUILayout.Button(m_isPreview ? "Stop Preview" : "Preview"))
{
m_isPreview = !m_isPreview;
m_applied = false;
if (!m_isPreview)
{
Revert();
}
}
if (m_isPreview)
{
if (mantis != null && !m_applied)
{
var result = mantis.Apply(m_originalMeshes);
EditorGUILayout.LabelField($"Triangles : {result.Item2}/{result.Item1}");
m_applied = true;
}
}
}
private void OnDisable()
{
Revert();
}
private void Revert()
{
foreach (var pair in m_originalMeshes)
{
switch (pair.Key)
{
case MeshFilter meshFilter:
meshFilter.sharedMesh = pair.Value;
break;
case SkinnedMeshRenderer skinnedMeshRenderer:
skinnedMeshRenderer.sharedMesh = pair.Value;
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment