22
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Inspectorの配列のElementに固定の名前つける

Last updated at Posted at 2017-01-18

Inspectorの配列のElementのところをわかりやすくしたい

ことの発端はOculus OVRLipSyncを使って製作を行っているのですが、15種類の口形素(sil, PP, FF, TH, DD, kk, CH, SS, nn, RR, aa, E, ih, oh, ou)を配列のElementに入れていかないといけないが途中でわけがわからず、よく間違えるのでなんとかならないかなぁと
74280c28d21d371e7a191ae0ca145461.png
※この15個のElementです

EditorフォルダにPropertyDrawerの拡張を書く

Editorフォルダの中に以下のソースを入れる

NamedArrayDrawer.cs
using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(NamedArrayAttribute))]
public class NamedArrayDrawer : PropertyDrawer
{
    public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    {
        try
        {
            int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
            EditorGUI.PropertyField(rect, property, new GUIContent(((NamedArrayAttribute)attribute).names[pos]));

        }
        catch
        {
            EditorGUI.PropertyField(rect, property, label);
        }
    }
}

※Unity 5.4だとこの書き方
5.3だと【EditorGUI.PropertyField】→【EditorGUI.ObjecField】な気がします(未検証)

NamedArrayAttributeクラスの作成

適当なところにNamedArrayAttributeクラスを作成

NamedArrayAttribute.cs
using UnityEngine;

public class NamedArrayAttribute : PropertyAttribute
{
    public readonly string[] names;
    public NamedArrayAttribute(string[] names) { this.names = names; }
}

実際に使う

OVRLipSyncContextMorphTarget.cs内で該当する配列の上に拡張したAttributeを置く

OVRLipSyncContextMorphTarget.cs
    // Set the blendshape index to go to (-1 means there is not one assigned)
    [NamedArrayAttribute(new string[] { "sil", "PP", "FF", "TH", "DD", "kk", "CH", "SS", "nn", "RR", "aa", "E", "ih", "oh", "ou" })]  // ←追加
    public int[] VisemeToBlendTargets = new int[(int)OVRLipSync.ovrLipSyncViseme.Count];

できたもの

1af33868c190de938e7e9e350e749d2e.png

うん、これで間違えなくなりそう!

22
13
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
22
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?