5
2

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.

UnityのエディタからDefineシンボルを定義する

Posted at

C#のシンボル定義ってあんまり個人的には多用したくない部類の機能ではあるのですが、簡単なのでUnityだとまま使います。
で、Unityのエディタ側から定義をいじれると便利なのでそれの操作クラス作りました。

使い方は
Symbol.Addで追加
Symbol.Removeで削除
Symbol.Setで上書きです

面倒くさいので全部params string[]の引数になっているので複数指定できます。

ところで全く関係ないですけどそろそろUnityがC#6.0に対応するので
String.Join(";", currentSymbols.ToArray())

String.Join(";", currentSymbols)
こうできるようになりますね。

Symbol.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;

namespace Assets.Editor
{
    static class Symbol
    {
        private static IEnumerable<string> currentSymbols;

        private static readonly BuildTargetGroup[] buildTargetGroup = new[]
        {
            BuildTargetGroup.Android, 
            BuildTargetGroup.Standalone, 
            /*...必要なものを...*/
        };

        public static string CurrentSymbols
        {
            get { return String.Join(";", currentSymbols.ToArray()); }
        }

        static Symbol()
        {
            //インスペクタ上では;区切り
            currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone).Split(';');
        }

        private static void SaveSymbol()
        {
            var symbols = CurrentSymbols;
            foreach (var target in buildTargetGroup)
            {
                PlayerSettings.SetScriptingDefineSymbolsForGroup(target, symbols);
            }
            
        }



        public static void Add(params string[] symbols)
        {
            currentSymbols = currentSymbols.Concat(symbols).Distinct().ToArray();
            SaveSymbol();
        }

        public static void Remove(params string[] symbols)
        {
            currentSymbols = currentSymbols.Except(symbols).ToArray();

            SaveSymbol();
        }

        public static void Set(params string[] symbols)
        {
            currentSymbols = symbols;
            SaveSymbol();
        }

    }
}

以下のテストを通過します

SymbolTest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnityEditor;

namespace Assets.Editor.Test
{
    [TestFixture]
    class SymbolTest
    {
        string startSymbols;

        [SetUp]
        public void SetUp()
        {
            startSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
        }

        [TearDown]
        public void TearDown()
        {
            Symbol.Set(startSymbols.Split(';'));
        }

        [Test]
        public void SetTest()
        {
            Symbol.Set("A","B");
            AssertSymbol("A", "B");
            Symbol.Set();
            AssertSymbol();

        }

        [Test]
        public void AddTest()
        {
            Symbol.Set();
            Symbol.Add("A");
            AssertSymbol("A");
            Symbol.Add("B");
            AssertSymbol("A","B");
            Symbol.Add("C","D");
            AssertSymbol("A","B","C","D");

            Symbol.Add("A","B","E");
            AssertSymbol("A", "B", "C", "D","E");
        }

        [Test]
        public void RemoveTest()
        {
            Symbol.Set("A", "B", "C", "D");
            Symbol.Remove("C", "D");
            AssertSymbol("A", "B"); 
            Symbol.Remove("A");
            AssertSymbol("B");
            Symbol.Remove("D");
            AssertSymbol("B");
            Symbol.Add("A");
            AssertSymbol("B","A");
            Symbol.Remove("B","C","D");
            AssertSymbol("A");
            Symbol.Remove("A");
            AssertSymbol();
        }

        void AssertSymbol(params string[] symbols)
        {
            Assert.AreEqual(string.Join(";", symbols),
                PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone));
        }
    }
}

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?