using System.Collections.Generic; using System.Linq; using UnityEditor; namespace ImstkEditor { /// /// Adds the given define symbols to PlayerSettings define symbols. /// Just add your own define symbols to the Symbols property at the below. /// see https://forum.unity.com/threads/scripting-define-symbols-access-in-code.174390/ /// [InitializeOnLoad] public class DefineSymbols : Editor { /// /// Symbols that will be added to the editor /// public static readonly string[] StaticSymbols = new string[] { }; public static bool runonce = false; /// /// Add define symbols as soon as Unity gets done compiling. /// static DefineSymbols() { string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup); List allDefines = definesString.Split(';').ToList(); List originalDefines = new List(allDefines); allDefines.AddRange(StaticSymbols.Except(allDefines)); allDefines.AddRange(GetDeviceSymbols().Except(allDefines)); if (Enumerable.SequenceEqual(allDefines, originalDefines)) return; PlayerSettings.SetScriptingDefineSymbolsForGroup( EditorUserBuildSettings.selectedBuildTargetGroup, string.Join(";", allDefines.ToArray())); } /// /// Check through the known device names and see if it exists in the factory, /// if yes, set the appropriate symbol so that the device class will be compiled /// private static List GetDeviceSymbols() { var factory = new Imstk.DeviceManagerFactory(); var names = new string[] { "OpenHapticDeviceManager", "IMSTK_USE_OPENHAPTICS", "HaplyDeviceManager", "IMSTK_USE_HAPLY", "VRPNDeviceManager", "IMSTK_USE_VRPN" }; var symbols = new List(); for (int i = 0; i < names.Length; i += 2) { if (Imstk.DeviceManagerFactory.contains(names[i])) { symbols.Add(names[i + 1]); } } return symbols; } } }