1
1

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.

Tagのstringを読み込むPropertyAttributeによるエディタ拡張

Posted at

前回の記事を見返したらtagにも適用できることに気が付いてtag版のPropertyAttributeも作りました。

TagAttribute.cs
using UnityEngine;
# if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
# endif
class TagAttribute : PropertyAttribute{}
# if UNITY_EDITOR
[CustomPropertyDrawer(typeof(TagAttribute))]
public class TagEditor : PropertyDrawer
{
    //tagのList
    List<string> AllTags
    {
        get
        {
            return InternalEditorUtility.tags.ToList();
        }
    }
    //ドロップダウンメニューの作成
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var list = AllTags;
        var selectedIndex = list.FindIndex(item => item.Equals(property.stringValue));
        if (selectedIndex == -1)
        {
            selectedIndex = list.FindIndex(item => item.Equals(list[0]));
        }

        selectedIndex = EditorGUI.Popup(position, label.text, selectedIndex, list.ToArray());

        property.stringValue = list[selectedIndex];
    }
}
# endif

前回の記事のgetterを変えただけです。
実装時は以下のようにstring型のフィールドに付けます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    [SerializeField, TagAttribute]
    string Tags;
}

__tags.png

_tags.png

デフォルトのtagに加え追加したtagが表示されています。

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?