9.
独自のPropertyDrawerを作成して使用する
プロパティ名を変更できる
PropertyDrawerを作成してみましょう
using UnityEngine;
public class Character : MonoBehaviour
{
[MyProperty("名前")] public string Name;
}
10.
using UnityEngine;
public class MyPropertyAttribute : PropertyAttribute
{
public string Label;
public MyPropertyAttribute(string label)
{
Label = label;
}
}
1.PropertyAttributeを継承したクラス作成
11.
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyPropertyAttribute))]
public class MyPropertyDrawer : PropertyDrawer
{
}
2.PropertyDrawerを継承したクラスを作成
12.
public override void OnGUI(
Rect position,
SerializedProperty property,
GUIContent label)
{
var myPropertyAttribute =
attribute as MyPropertyAttribute;
property.stringValue = EditorGUI.TextField(
position,
myPropertyAttribute.Label,
property.stringValue);
}
3. OnGUI関数をオーバーライドして実装
13.
using UnityEngine;
public class Character : MonoBehaviour
{
[MyProperty("名前")] public string Name;
}
4.作成したPropertyDrawerを使用する
22.
using UnityEngine;
public class Character : MonoBehaviour
{
public enum JobType
{
[EnumLabel("王国兵士")] SOLDIER,
[EnumLabel("魔法使い")] SORCERER,
}
[EnumLabel("ジョブ")] public JobType Job;
}
EnumLabelAttributeを使用する
23.
using UnityEngine;
public class Character : MonoBehaviour
{
public string Job;
public int Money;
}
入力できる値をポップアップで制限したい
25.
using UnityEngine;
public class Character : MonoBehaviour
{
[Popup("王国兵士", "魔法使い")]
public string Job;
[Popup(1, 5, 10, 50, 100, 500)]
public int Money;
}
PopupAttributeを使用する
26.
using UnityEngine;
public class LoadSceneButton : MonoBehaviour
{
public string SceneName;
}
ポップアップでシーン名を設定したい
31.
using UnityEngine;
public class Config : MonoBehaviour
{
// IPアドレス
[Regex(@"^(?:¥d{1,3}¥.){3}¥d{1,3}$", "無効なIPアドレスです!例:'127.0.0.1'")]
public string ServerAddress;
}
RegexAttributeを使用する
44.
using UnityEditor;
public static class MyClassCreator
{
// Unityエディタのメニューに
// ソースコード自動生成用のコマンドを追加します
[MenuItem("Tools/Create My Class")]
private static void Create()
{
}
}
1.MenuItemが適用された静的関数を作成
45.
// ソースコードを表す文字列を作成します
var builder = new System.Text.StringBuilder();
builder.AppendLine("public class MyClass");
builder.AppendLine("{");
builder.AppendLine("}");
2.StringBuilderで文字列を作成
64.
レイヤー名を定数で管理するクラスの生成
/// <summary>
/// レイヤー名を定数で管理するクラス
/// </summary>
public static class LayerName
{
public const int Default = 0;
public const int TransparentFX = 1;
public const int IgnoreRaycast = 2;
public const int Water = 4;
public const int Character = 8;
public const int DefaultMask = 1;
public const int TransparentFXMask = 2;
public const int IgnoreRaycastMask = 4;
public const int WaterMask = 16;
public const int CharacterMask = 256;
}