誰もが実装するであろう、BlendShapeのインデックスを登録しておくエディタ拡張です。
MonoBehaviour側
using UnityEngine;
using System.Linq;
public class SetBlendshape : MonoBehaviour
{
public SkinnedMeshRenderer skin = null;
public int[] selectedIndexes = Enumerable.Range(0, 7).ToArray();
private void Start()
{
}
private void Update()
{
}
}
エディタ側
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(SetBlendshape))]
public class SetBlendshapeEditor : Editor
{
SerializedProperty skinProperty;
SerializedProperty selectedIndexesProperty;
private static string[] settings = new string[]
{
"A", "B", "C", "D", "E", "F", "G"
};
private void OnEnable()
{
skinProperty = serializedObject.FindProperty("skin");
selectedIndexesProperty = serializedObject.FindProperty("selectedIndexes");
}
public override void OnInspectorGUI()
{
var blendShapeNames = GetMeshBlendshapeNames();
serializedObject.Update();
EditorGUILayout.PropertyField(skinProperty);
if (EditorGUILayout.PropertyField(selectedIndexesProperty))
{
EditorGUI.indentLevel++;
for(int i = 0; i < settings.Length; i++)
{
BlendNameProperty(selectedIndexesProperty.GetArrayElementAtIndex(i), settings[i], blendShapeNames);
}
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties();
}
private string[] GetMeshBlendshapeNames()
{
var target = (SetBlendshape)serializedObject.targetObject;
if (target == null || target.skin == null)
{
return null;
}
var mesh = target.skin.sharedMesh;
var blendshapeCount = mesh.blendShapeCount;
var blendNames = new string[blendshapeCount];
for (int i = 0; i < mesh.blendShapeCount; ++i)
{
blendNames[i] = mesh.GetBlendShapeName(i);
}
return blendNames;
}
private void BlendNameProperty(SerializedProperty prop, string name, string[] blendShapeNames = null)
{
if (blendShapeNames == null)
{
EditorGUILayout.PropertyField(prop, new GUIContent(name));
return;
}
var values = new int[blendShapeNames.Length];
var options = new GUIContent[blendShapeNames.Length];
for (int i = 0; i < blendShapeNames.Length; ++i)
{
values[i] = i;
options[i] = new GUIContent(blendShapeNames[i]);
}
EditorGUILayout.IntPopup(prop, options, values, new GUIContent(name));
}
}