コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】正規表現を使用してstring型の値を制限する「RegexAttribute」

  1. 「RegexAttribute.cs」をUnityプロジェクトに追加します
  2. Unityプロジェクトに「Editor」フォルダが存在しない場合は作成します
  3. 「RegexDrawer.cs」をUnityプロジェクトの「Editor」フォルダに追加します
  4. 下記のような構文で使用します
using UnityEngine;

public class MyBehaviourScript : MonoBehaviour
{
    [Regex(@"^(?:\d{1,3}\.){3}\d{1,3}$", "Invalid IP address!\nExample: '127.0.0.1'")]
    public string ServerAddress;
}

f:id:baba_s:20140302173758p:plain

f:id:baba_s:20140302173803p:plain

このRegexAttributeを使用すると、
Inspector上で設定できるstring型の値を正規表現を使用して制限することができます

http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/