├── Editor.meta ├── Editor ├── DefineInspectorWindow.cs └── DefineInspectorWindow.cs.meta ├── LICENSE ├── LICENSE.meta ├── README.md └── README.md.meta /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 76363895db68bd34abd5b580dcd80c1a 3 | folderAsset: yes 4 | timeCreated: 1512039157 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Editor/DefineInspectorWindow.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System.Linq; 4 | using UnityEditor; 5 | using UnityEngine; 6 | 7 | public class DefineInspectorWindow : EditorWindow 8 | { 9 | private const string _filename = "DefineInspectorSymbols.txt"; 10 | 11 | private static List _defineSymbols = new List(); 12 | private static string _pathToFile; 13 | private static bool _hasReadSymbols; 14 | private string _newDefine = ""; 15 | 16 | [MenuItem("Window/DefineInspector")] 17 | public static void ShowWindow() { 18 | GetWindow(typeof(DefineInspectorWindow)); 19 | CheckToReadPairsFromFile(); 20 | } 21 | 22 | [UnityEditor.Callbacks.DidReloadScripts] 23 | private static void OnScriptsReloaded() { 24 | CheckToReadPairsFromFile(); 25 | } 26 | 27 | private static void CheckToReadPairsFromFile() { 28 | if (!_hasReadSymbols) { 29 | _pathToFile = Path.Combine(Application.dataPath, _filename); 30 | _defineSymbols = ParseFileToPairs(_pathToFile); 31 | SortDefineSymbols(); 32 | _hasReadSymbols = true; 33 | } 34 | } 35 | 36 | private static List ParseFileToPairs(string filepath) { 37 | if (!File.Exists(filepath)) { 38 | File.Create(filepath); 39 | } 40 | string text = File.ReadAllText(filepath); 41 | string[] lines = text.Split(';'); 42 | var pairs = new List(); 43 | foreach (string line in lines) { 44 | var pair = DefinePair.ParseLine(line); 45 | if (pair != null) { 46 | pairs.Add(pair); 47 | } 48 | } 49 | return pairs; 50 | } 51 | 52 | private static void SortDefineSymbols() { 53 | _defineSymbols = _defineSymbols.OrderBy(o=>o.defineSymbol).ToList(); 54 | } 55 | 56 | private void OnGUI() { 57 | CheckToReadPairsFromFile(); 58 | 59 | GUILayout.Label("Defines", EditorStyles.boldLabel); 60 | int i = 0; 61 | var definesToDelete = new List(); 62 | foreach (DefinePair pair in _defineSymbols) { 63 | GUILayout.BeginHorizontal(); 64 | GUILayout.Label(pair.defineSymbol, GUILayout.Width(200)); 65 | bool newOn = EditorGUILayout.Toggle("", pair.on, GUILayout.MinWidth(30)); 66 | if (newOn != pair.on) { 67 | pair.on = newOn; 68 | UpdatePairInFile(pair); 69 | } 70 | 71 | if (GUILayout.Button("Delete", GUILayout.Width(100))) { 72 | definesToDelete.Add(i); 73 | } 74 | GUILayout.EndHorizontal(); 75 | i++; 76 | } 77 | 78 | foreach (int deleteIndex in definesToDelete) { 79 | DeletePairFromFile(_defineSymbols[deleteIndex]); 80 | _defineSymbols.RemoveAt(deleteIndex); 81 | } 82 | 83 | GUILayout.BeginHorizontal(); 84 | _newDefine = GUILayout.TextField(_newDefine); 85 | if (GUILayout.Button("Add New", GUILayout.Width(100)) && !string.IsNullOrEmpty(_newDefine)) { 86 | var newPair = new DefinePair { 87 | defineSymbol = _newDefine, 88 | on = false, 89 | }; 90 | _defineSymbols.Add(newPair); 91 | AddPairToFile(newPair); 92 | SortDefineSymbols(); 93 | _newDefine = ""; 94 | } 95 | GUILayout.EndHorizontal(); 96 | 97 | if (GUILayout.Button("Compile")) { 98 | SetScriptDefines(); 99 | } 100 | } 101 | 102 | private static void AddPairToFile(DefinePair pair) { 103 | string pairStr = pair.ToFileString(); 104 | string appendStr = pairStr + ";"; 105 | File.AppendAllText(_pathToFile, appendStr); 106 | } 107 | 108 | private void UpdatePairInFile(DefinePair pair) { 109 | DeletePairFromFile(pair); 110 | AddPairToFile(pair); 111 | } 112 | 113 | private static void DeletePairFromFile(DefinePair pair) { 114 | string needle = pair.defineSymbol; 115 | string text = File.ReadAllText(_pathToFile); 116 | int index = text.IndexOf(needle); 117 | int delimiterIndex = text.IndexOf(";", index + 1); 118 | if (delimiterIndex < 0) { 119 | text = text.Remove(index); 120 | } else { 121 | text = text.Remove(index, delimiterIndex - index + 1); 122 | } 123 | File.WriteAllText(_pathToFile, text); 124 | } 125 | 126 | private void SetScriptDefines() { 127 | var targetBuildGroup = EditorUserBuildSettings.selectedBuildTargetGroup; 128 | string defines = CreateDefinesString(); 129 | PlayerSettings.SetScriptingDefineSymbolsForGroup(targetBuildGroup, defines); 130 | } 131 | 132 | private string CreateDefinesString() { 133 | string str = ""; 134 | foreach (DefinePair pair in _defineSymbols) { 135 | if (pair.on) 136 | str += pair.defineSymbol + ";"; 137 | } 138 | return str; 139 | } 140 | 141 | public class DefinePair 142 | { 143 | public string defineSymbol; 144 | public bool on; 145 | 146 | public static DefinePair ParseLine(string line) { 147 | string[] split = line.Split(','); 148 | if (split.Length != 2) { 149 | return null; 150 | } 151 | var pair = new DefinePair { 152 | defineSymbol = split[0], 153 | on = split[1] == "1", 154 | }; 155 | return pair; 156 | } 157 | 158 | public string ToFileString() { 159 | return defineSymbol + "," + (on ? "1" : "0"); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /Editor/DefineInspectorWindow.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cdac8c6ad8ff3da4f9b4fdfca17be94f 3 | timeCreated: 1512039170 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Hayden Lee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /LICENSE.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4982aed041633d74298f482fbc88f97a 3 | timeCreated: 1525168232 4 | licenseType: Pro 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Unity Define Inspector 4 | 5 | A Unity inspector panel to quickly add and remove a set of custom defined [Scripting Define Symbols](https://docs.unity3d.com/Manual/PlatformDependentCompilation.html). 6 | 7 | 8 | ### Instructions 9 | 1. Navigate to the `Assets/` folder in your Unity project 10 | 2. `git clone https://github.com/haydenjameslee/unity-define-inspector.git` 11 | 3. In Unity, open `Window -> DefineInspector` 12 | 4. Use [this gif](https://i.imgur.com/4mCXwtw.gif) as a demo on how to use 13 | 14 | ### Info 15 | 16 | Author: Hayden Lee 17 | 18 | Source: https://github.com/haydenjameslee/unity-define-inspector 19 | 20 | Version: 1.1.1 21 | 22 | License: MIT 23 | 24 | Preview: https://imgur.com/a/SjY0imi 25 | 26 | Tested on: Unity 5.6.1f1 27 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d4fb18a3053b20046a73931cfcbd6be3 3 | timeCreated: 1525167953 4 | licenseType: Pro 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | --------------------------------------------------------------------------------