Tanoda
TriLibSettingsProvider.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Reflection;
6using UnityEditor;
7using UnityEngine;
8
9namespace TriLibCore.Editor
10{
11 public class TriLibSettingsProvider : SettingsProvider
12 {
13 private class Styles
14 {
15 public static readonly GUIStyle Group = new GUIStyle { padding = new RectOffset(10, 10, 5, 5) };
16 }
17
18 private const string ReadersFileTemplate = "//Auto-generated: Do not modify this file!\n\nusing System.Collections;\nusing System.Collections.Generic;\n{0}\nnamespace TriLibCore\n{{\n public class Readers\n {{\n public static IList<string> Extensions\n {{\n get\n {{\n var extensions = new List<string>();{1}\n return extensions;\n }}\n }}\n public static ReaderBase FindReaderForExtension(string extension)\n {{\n\t\t\t{2}\n return null;\n }}\n }}\n}}";
19
20 private readonly List<ImporterOption> _importerOptions = new List<ImporterOption>();
21 private readonly string _readersFilePath;
22
23 public TriLibSettingsProvider(string path, SettingsScope scopes = SettingsScope.User, IEnumerable<string> keywords = null) : base(path, scopes, keywords)
24 {
25 var triLibReadersAssets = AssetDatabase.FindAssets("TriLibReaders");
26 if (triLibReadersAssets.Length > 0)
27 {
28 _readersFilePath = AssetDatabase.GUIDToAssetPath(triLibReadersAssets[0]);
29 }
30 else
31 {
32 throw new Exception("Could not find TriLibReaders.cs file. Please re-import TriLib package.");
33 }
34 _importerOptions.Clear();
35 var pluginImporters = PluginImporter.GetAllImporters();
36 foreach (var pluginImporter in pluginImporters)
37 {
38 if (!pluginImporter.isNativePlugin && pluginImporter.assetPath.Contains("TriLibCore."))
39 {
40 var assembly = Assembly.LoadFile(pluginImporter.assetPath);
41 foreach (var type in assembly.ExportedTypes)
42 {
43 if (type.BaseType == typeof(ReaderBase))
44 {
45 _importerOptions.Add(new ImporterOption(type.Name, type.Namespace, pluginImporter));
46 }
47 }
48 }
49 }
50 }
51
52 public override void OnGUI(string searchContext)
53 {
54 EditorGUILayout.Space();
55 var contentWidth = GUILayoutUtility.GetLastRect().width * 0.5f;
56 EditorGUIUtility.labelWidth = contentWidth;
57 EditorGUIUtility.fieldWidth = contentWidth;
58 GUILayout.BeginVertical(Styles.Group);
59 GUILayout.Label("Enabled Readers", EditorStyles.boldLabel);
60 GUILayout.Label("You can disable file formats you don't use here");
61 EditorGUILayout.Space();
62 var changed = false;
63 foreach (var importerOption in _importerOptions)
64 {
65 var value = importerOption.PluginImporter.GetCompatibleWithAnyPlatform();
66 var newValue = EditorGUILayout.Toggle(importerOption, value);
67 if (newValue != value)
68 {
69 importerOption.PluginImporter.SetCompatibleWithAnyPlatform(newValue);
70 changed = true;
71 }
72 }
73 if (changed)
74 {
75 string usings = null;
76 string extensions = null;
77 string findReader = null;
78 foreach (var importerOption in _importerOptions)
79 {
80 if (importerOption.PluginImporter.GetCompatibleWithAnyPlatform())
81 {
82 extensions += $"\n\t\t\t\textensions.AddRange({importerOption.text}.GetExtensions());";
83 usings += $"using {importerOption.Namespace};\n";
84 findReader += $"\n\t\t\tif (((IList) {importerOption.text}.GetExtensions()).Contains(extension))\n\t\t\t{{\n\t\t\t\treturn new {importerOption.text}();\n\t\t\t}}";
85 }
86 }
87 var text = string.Format(ReadersFileTemplate, usings, extensions, findReader);
88 var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(_readersFilePath);
89 File.WriteAllText(_readersFilePath, text);
90 EditorUtility.SetDirty(textAsset);
91 AssetDatabase.SaveAssets();
92 AssetDatabase.Refresh();
93 }
94 EditorGUILayout.Space();
95 GUILayout.Label("Material Mappers", EditorStyles.boldLabel);
96 GUILayout.Label("Select the Material Mappers according your project rendering pipeline");
97 EditorGUILayout.Space();
98 foreach (var materialMapperName in MaterialMapper.RegisteredMappers)
99 {
100 var value = TriLibSettings.GetBool(materialMapperName);
101 var newValue = EditorGUILayout.Toggle(materialMapperName, value);
102 if (newValue != value)
103 {
104 TriLibSettings.SetBool(materialMapperName, newValue);
105 }
106 }
107 CheckMappers.Initialize();
108 EditorGUILayout.Space();
109 GUILayout.Label("Misc Options", EditorStyles.boldLabel);
110 GUILayout.Label("Advanced Options");
111 EditorGUILayout.Space();
112 ShowConditionalToggle("Enable GLTF Draco Decompression (Experimental)", "TRILIB_DRACO");
113 ShowConditionalToggle("Force synchronous loading", "TRILIB_FORCE_SYNC");
114 ShowConditionalToggle("Change Thread names (Debug purposes only)", "TRILIB_USE_THREAD_NAMES");
115 EditorGUILayout.Space();
116 GUILayout.BeginHorizontal();
117 GUILayout.FlexibleSpace();
118 if (GUILayout.Button("Version Notes"))
119 {
121 }
122 if (GUILayout.Button("API Reference"))
123 {
124 Application.OpenURL("https://ricardoreis.net/trilib/trilib2/docs/");
125 }
126 if (GUILayout.Button("Support"))
127 {
128 Application.OpenURL("mailto:contato@ricardoreis.net");
129 }
130 GUILayout.EndHorizontal();
131 GUILayout.EndVertical();
132 base.OnGUI(searchContext);
133 }
134
135 private void ShowConditionalToggle(string label, string symbol)
136 {
137 var currentValue = TriLibDefineSymbolsHelper.IsSymbolDefined(symbol);
138 var newValue = EditorGUILayout.Toggle(label, currentValue);
139 if (newValue != currentValue)
140 {
141 TriLibDefineSymbolsHelper.UpdateSymbol(symbol, newValue);
142 }
143 }
144
145 [SettingsProvider]
146 public static SettingsProvider Register()
147 {
148 var provider = new TriLibSettingsProvider("Project/TriLib", SettingsScope.Project)
149 {
150 keywords = GetSearchKeywordsFromGUIContentProperties<Styles>()
151 };
152 return provider;
153 }
154 }
155}
TriLibSettingsProvider(string path, SettingsScope scopes=SettingsScope.User, IEnumerable< string > keywords=null)
override void OnGUI(string searchContext)
Represents the TriLib project settings provider. You can override this behavior to store the settings...
static void SetBool(string key, bool value)
static bool GetBool(string key)