Skip to content

Instantly share code, notes, and snippets.

@MewSoul
Created January 30, 2018 10:32
Show Gist options
  • Save MewSoul/53d3b7b8a3429e6eccb01b53420f387a to your computer and use it in GitHub Desktop.
Save MewSoul/53d3b7b8a3429e6eccb01b53420f387a to your computer and use it in GitHub Desktop.
Extension to easily add/remove custom defines to specific build targets
using UnityEditor;
using UnityEngine;
namespace Utils
{
public class CustomDefineEditor : EditorWindow
{
private string[] defines =
{
"debug"
};
private bool[] values;
private BuildTargetGroup selectedTargetGroup;
private void Awake()
{
values = new bool[defines.Length];
selectedTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
LoadCustomDefines();
}
[MenuItem("Window/CustomDefines")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(CustomDefineEditor), false, "CustomDefines");
}
private void OnGUI()
{
GUILayout.Label("Target", EditorStyles.boldLabel);
BuildTargetGroup selection = (BuildTargetGroup)EditorGUILayout.EnumPopup("Selected target", selectedTargetGroup);
if (selection != selectedTargetGroup)
{
selectedTargetGroup = selection;
LoadCustomDefines();
}
GUILayout.Label("Defines", EditorStyles.boldLabel);
for (int i = 0; i < defines.Length; i++)
{
string define = defines[i];
values[i] = EditorGUILayout.Toggle(define, values[i]);
}
if (GUILayout.Button("Save"))
{
SaveCustomDefines();
Debug.Log("Custom defines saved!");
}
}
private void SaveCustomDefines()
{
string definesToSave = "";
for (int i = 0; i < values.Length; i++)
{
if (values[i])
{
definesToSave += defines[i] + ";";
}
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(selectedTargetGroup, definesToSave);
}
private void LoadCustomDefines()
{
string customDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(selectedTargetGroup);
for (int i = 0; i < defines.Length; i++)
{
values[i] = customDefines.Contains(defines[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment