LoginSignup
2
1

More than 3 years have passed since last update.

[#Unity] BlendShapeを列挙してインデックスを保存できるエディタ拡張

Posted at

誰もが実装するであろう、BlendShapeのインデックスを登録しておくエディタ拡張です。
2019-09-12_191745.png

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));
    }
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1