Property Attribute를 사용하여 Tag의string을 읽을 수 있도록 편집기 확장

5738 단어 UnityC#
지난번 기사를 돌아보니 태그에 적용되는 것으로 나타나 태그 버전의 Property Attribute를 제작했다.
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;
}


기본'tag'외에 추가'tag'도 표시됩니다.

좋은 웹페이지 즐겨찾기