|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using UnityEditorInternal; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEditor; |
| 6 | + |
| 7 | +namespace FbxExporters.EditorTools { |
| 8 | + |
| 9 | + [CustomEditor(typeof(ExportSettings))] |
| 10 | + public class ExportSettingsEditor : UnityEditor.Editor { |
| 11 | + public override void OnInspectorGUI() { |
| 12 | + ExportSettings temp = (ExportSettings)target; |
| 13 | + |
| 14 | + temp.weldVertices = EditorGUILayout.Toggle ("Weld Vertices:", temp.weldVertices); |
| 15 | + |
| 16 | + if (GUI.changed) { |
| 17 | + EditorUtility.SetDirty (temp); |
| 18 | + temp.Dirty (); |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + [FilePath("ProjectSettings/FbxExportSettings.asset",FilePathAttribute.Location.ProjectFolder)] |
| 24 | + public class ExportSettings : FbxExporters.EditorTools.ScriptableSingleton<ExportSettings> |
| 25 | + { |
| 26 | + public bool weldVertices = true; |
| 27 | + |
| 28 | + [MenuItem("Edit/Project Settings/Fbx Export", priority = 300)] |
| 29 | + static void ShowManager() |
| 30 | + { |
| 31 | + instance.name = "Fbx Export Settings"; |
| 32 | + Selection.activeObject = instance; |
| 33 | + } |
| 34 | + |
| 35 | + public void Dirty() |
| 36 | + { |
| 37 | + instance.Save (true); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public class ScriptableSingleton<T> : ScriptableObject where T : ScriptableObject |
| 42 | + { |
| 43 | + private static T s_Instance; |
| 44 | + public static T instance |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + if (ScriptableSingleton<T>.s_Instance == null) |
| 49 | + { |
| 50 | + ScriptableSingleton<T>.CreateAndLoad(); |
| 51 | + } |
| 52 | + return ScriptableSingleton<T>.s_Instance; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected ScriptableSingleton() |
| 57 | + { |
| 58 | + if (ScriptableSingleton<T>.s_Instance != null) |
| 59 | + { |
| 60 | + Debug.LogError("ScriptableSingleton already exists. Did you query the singleton in a constructor?"); |
| 61 | + } |
| 62 | + } |
| 63 | + private static void CreateAndLoad() |
| 64 | + { |
| 65 | + string filePath = ScriptableSingleton<T>.GetFilePath(); |
| 66 | + if (!string.IsNullOrEmpty(filePath)) |
| 67 | + { |
| 68 | + var loaded = InternalEditorUtility.LoadSerializedFileAndForget(filePath); |
| 69 | + if (loaded.Length > 0) { |
| 70 | + ScriptableSingleton<T>.s_Instance = loaded [0] as T; |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + if (ScriptableSingleton<T>.s_Instance == null) |
| 75 | + { |
| 76 | + T t = ScriptableObject.CreateInstance<T>(); |
| 77 | + ScriptableSingleton<T>.s_Instance = t; |
| 78 | + } |
| 79 | + } |
| 80 | + protected virtual void Save(bool saveAsText) |
| 81 | + { |
| 82 | + if (ScriptableSingleton<T>.s_Instance == null) |
| 83 | + { |
| 84 | + Debug.Log("Cannot save ScriptableSingleton: no instance!"); |
| 85 | + return; |
| 86 | + } |
| 87 | + string filePath = ScriptableSingleton<T>.GetFilePath(); |
| 88 | + if (!string.IsNullOrEmpty(filePath)) |
| 89 | + { |
| 90 | + string directoryName = Path.GetDirectoryName(filePath); |
| 91 | + if (!Directory.Exists(directoryName)) |
| 92 | + { |
| 93 | + Directory.CreateDirectory(directoryName); |
| 94 | + } |
| 95 | + InternalEditorUtility.SaveToSerializedFileAndForget(new T[] |
| 96 | + { |
| 97 | + ScriptableSingleton<T>.s_Instance |
| 98 | + }, filePath, saveAsText); |
| 99 | + } |
| 100 | + } |
| 101 | + private static string GetFilePath() |
| 102 | + { |
| 103 | + Type typeFromHandle = typeof(T); |
| 104 | + object[] customAttributes = typeFromHandle.GetCustomAttributes(true); |
| 105 | + object[] array = customAttributes; |
| 106 | + for (int i = 0; i < array.Length; i++) |
| 107 | + { |
| 108 | + object obj = array[i]; |
| 109 | + if (obj is FilePathAttribute) |
| 110 | + { |
| 111 | + FilePathAttribute filePathAttribute = obj as FilePathAttribute; |
| 112 | + return filePathAttribute.filepath; |
| 113 | + } |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + [AttributeUsage(AttributeTargets.Class)] |
| 121 | + public class FilePathAttribute : Attribute |
| 122 | + { |
| 123 | + public enum Location |
| 124 | + { |
| 125 | + PreferencesFolder, |
| 126 | + ProjectFolder |
| 127 | + } |
| 128 | + public string filepath |
| 129 | + { |
| 130 | + get; |
| 131 | + set; |
| 132 | + } |
| 133 | + public FilePathAttribute(string relativePath, FilePathAttribute.Location location) |
| 134 | + { |
| 135 | + if (string.IsNullOrEmpty(relativePath)) |
| 136 | + { |
| 137 | + Debug.LogError("Invalid relative path! (its null or empty)"); |
| 138 | + return; |
| 139 | + } |
| 140 | + if (relativePath[0] == '/') |
| 141 | + { |
| 142 | + relativePath = relativePath.Substring(1); |
| 143 | + } |
| 144 | + if (location == FilePathAttribute.Location.PreferencesFolder) |
| 145 | + { |
| 146 | + this.filepath = InternalEditorUtility.unityPreferencesFolder + "/" + relativePath; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + this.filepath = relativePath; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments