LoginSignup
16
6

More than 5 years have passed since last update.

Unityで定数を定義する

Last updated at Posted at 2015-05-31

Const.csでnamespaceを宣言、public staticなクラスを定義して
Main.csでusing Constして参照しています。

Const.cs
namespace Const {

    public static class CO {
        public const int CONST_INT = 10;

        public static readonly float[] CONST_LIST = {
            1.0f, 2.0f, 3.0f
        };

    }

}
Main.cs
using UnityEngine;
using System.Collections;

using Const;

public class Main : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Debug.Log (Const.CO.CONST_INT);
        Debug.Log (Const.CO.CONST_LIST[0]);
    }

}

追記

上の書き方だと配列の内容の変更まで制限してくれないらしいので以下の書き方がよさげ。

Const.cs
namespace Const {

    public static class CO {
        public const int CONST_INT = 10;

        public static readonly ReadOnlyCollection<float> CONST_LIST = 
                    Array.AsReadOnly(new float[] {1.0f, 2.0f, 3.0f});

    }

}
16
6
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
16
6