Tanoda
LeapGraphicPreferences.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEditor;
11using System;
12using System.IO;
13using System.Collections.Generic;
14using System.Text.RegularExpressions;
15
17
18 public class LeapGraphicPreferences : MonoBehaviour {
19 public const int GRAPHIC_COUNT_SOFT_CEILING = 144;
20 public const string LEAP_GRAPHIC_CGINC_PATH = "LeapMotion/Modules/GraphicRenderer/Resources/GraphicRenderer.cginc";
21 public const string LEAP_GRAPHIC_SHADER_FOLDER = "Assets/Plugins/LeapMotion/Legacy/GraphicRenderer/Shaders/";
22 private static Regex _graphicMaxRegex = new Regex(@"^#define\s+GRAPHIC_MAX\s+(\d+)\s*$");
23
24 public const string PROMPT_WHEN_GROUP_CHANGE_KEY = "LeapGraphicRenderer_ShouldPromptWhenGroupChange";
25
26 public const string PROMP_WHEN_ADD_CUSTOM_CHANNEL_LEY = "LeapGraphicRenderer_ShouldPrompWhenAddCustomChannel";
27
28 private static int _cachedGraphicMax = -1; //-1 signals dirty
29 public static int graphicMax {
30 get {
31 if (_cachedGraphicMax == -1) {
32 string errorMesage;
33 string path;
34 List<string> lines;
35 int lineIndex;
36
37 tryCalculateGraphicMax(out _cachedGraphicMax, out errorMesage, out path, out lines, out lineIndex);
38
39 if (errorMesage != null) {
40 _cachedGraphicMax = int.MaxValue;
41 }
42 }
43
44 return _cachedGraphicMax;
45 }
46 }
47
48 public static bool promptWhenGroupChange {
49 get {
50 return EditorPrefs.GetBool(PROMPT_WHEN_GROUP_CHANGE_KEY, true);
51 }
52 set {
53 EditorPrefs.SetBool(PROMPT_WHEN_GROUP_CHANGE_KEY, value);
54 }
55 }
56
57 public static bool promptWhenAddCustomChannel {
58 get {
59 return EditorPrefs.GetBool(PROMP_WHEN_ADD_CUSTOM_CHANNEL_LEY, true);
60 }
61 set {
62 EditorPrefs.SetBool(PROMP_WHEN_ADD_CUSTOM_CHANNEL_LEY, value);
63 }
64 }
65
66 [LeapPreferences("Graphic Renderer", 20)]
67 private static void preferencesGUI() {
68 drawGraphicMaxField();
69
70 GUIContent groupChangedContent = new GUIContent("Prompt When Group Changed", "Should the system prompt the user when they change the group of a graphic to a group with different features.");
71 bool newPromptValue = EditorGUILayout.Toggle(groupChangedContent, promptWhenGroupChange);
72 if (promptWhenGroupChange != newPromptValue) {
73 promptWhenGroupChange = newPromptValue;
74 }
75
76 GUIContent addChannelContent = new GUIContent("Prompt For Custom Channel", "Should the system warn the user about writing custom shaders when they try to add a Custom Channel feature?");
77 bool newPromptWhenAddCustomChannelValue = EditorGUILayout.Toggle(addChannelContent, promptWhenAddCustomChannel);
78 if (newPromptWhenAddCustomChannelValue != promptWhenAddCustomChannel) {
79 promptWhenAddCustomChannel = newPromptWhenAddCustomChannelValue;
80 }
81
82 EditorGUILayout.Space();
83 GUILayout.Label("Surface-shader variant options", EditorStyles.boldLabel);
84
85 EditorGUILayout.HelpBox("Using surface-shader variants can drastically increase import time! Only enable variants if you are using surface shaders with the graphic renderer.", MessageType.Info);
86
87 using (new EditorGUILayout.HorizontalScope()) {
88 if (GUILayout.Button("Enable all variants")) {
89 setVariantsEnabledForSurfaceShaders(enable: true);
90 }
91
92 if (GUILayout.Button("Disable all variants")) {
93 setVariantsEnabledForSurfaceShaders(enable: false);
94 }
95 }
96 }
97
98 private static void setVariantsEnabledForSurfaceShaders(bool enable) {
99 foreach (var path in Directory.GetFiles(LEAP_GRAPHIC_SHADER_FOLDER, "*.shader", SearchOption.AllDirectories)) {
100 var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
101 if (shader == null) continue;
102
103 if (VariantEnabler.IsSurfaceShader(shader)) {
104 VariantEnabler.SetShaderVariantsEnabled(shader, enable);
105 }
106 }
107 AssetDatabase.Refresh();
108 }
109
110 private static void drawGraphicMaxField() {
111 int graphicMax;
112 string errorMessage;
113 string path;
114 List<string> lines;
115 int lineIndex;
116
117 _cachedGraphicMax = -1;
118 if (!tryCalculateGraphicMax(out graphicMax, out errorMessage, out path, out lines, out lineIndex)) {
119 EditorGUILayout.HelpBox(errorMessage +
120 "\n\nRe-installing the Leap Gui package can help fix this problem.",
121 MessageType.Warning);
122 return;
123 }
124
125 int newGraphicMax = EditorGUILayout.DelayedIntField("Max Graphics Per-Group", graphicMax);
126 newGraphicMax = Mathf.Min(newGraphicMax, 1023); //maximum array length for Unity shaders is 1023
127
129 if (!EditorUtility.DisplayDialog("Large Graphic Count",
130 "Setting the graphic count larger than 144 can cause incorrect rendering " +
131 "or shader compilation failure on certain systems, are you sure you want " +
132 "to continue?", "Yes", "Cancel")) {
133 return;
134 }
135 }
136
137 if (newGraphicMax == graphicMax) {
138 return; //Work here is done! Nothing to change!
139 }
140
141 lines[lineIndex] = lines[lineIndex].Replace(graphicMax.ToString(), newGraphicMax.ToString());
142
143 //Write the new data to the file
144 File.WriteAllLines(path, lines.ToArray());
145
146 //Make sure to re-import all the shaders
147 AssetDatabase.ImportAsset(LEAP_GRAPHIC_SHADER_FOLDER, ImportAssetOptions.ImportRecursive);
148 }
149
150 private static bool tryCalculateGraphicMax(out int elementMax,
151 out string errorMessage,
152 out string path,
153 out List<string> lines,
154 out int lineIndex) {
155 elementMax = -1;
156 errorMessage = null;
157 lines = null;
158 lineIndex = -1;
159
160 path = Path.Combine(Application.dataPath, LEAP_GRAPHIC_CGINC_PATH);
161 if (!File.Exists(path)) {
162 errorMessage = "Could not locate the Leap cginclude file, was it renamed or deleted?";
163 return false;
164 }
165
166 lines = new List<string>();
167
168 StreamReader reader = null;
169 try {
170 reader = File.OpenText(path);
171
172 while (true) {
173 string line = reader.ReadLine();
174 if (line == null) {
175 break;
176 }
177 lines.Add(line);
178 }
179 } catch (Exception e) {
180 errorMessage = "Exception caught when trying to read file.";
181 Debug.LogError(e);
182 return false;
183 } finally {
184 if (reader != null) {
185 reader.Dispose();
186 }
187 }
188
189 Match successMatch = null;
190 for (int i = 0; i < lines.Count; i++) {
191 string line = lines[i];
192 var match = _graphicMaxRegex.Match(line);
193 if (match.Success) {
194 successMatch = match;
195 lineIndex = i;
196 break;
197 }
198 }
199
200 if (successMatch == null) {
201 errorMessage = "Could not parse the file correctly, it might have been modified!";
202 return false;
203 }
204
205 if (!int.TryParse(successMatch.Groups[1].Value, out elementMax)) {
206 errorMessage = "The maximum graphic value must always be an integer value!";
207 return false;
208 }
209
210 return true;
211 }
212 }
213}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
This attribute is used to add items to the Leap Motion preferences window. This allows each module to...