Skip to content

Commit 2ed217b

Browse files
committed
added custom editor settings
Settings are a singleton, and stored in the same folder as the other editor settings. Currently only contains a setting of whether or not to weld vertices when exporting
1 parent 7bf172c commit 2ed217b

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.IO;
3+
using UnityEditorInternal;
4+
using UnityEngine;
5+
using UnityEditor;
6+
7+
namespace FbxSdk.EditorTools {
8+
9+
[CustomEditor(typeof(FbxExportSettings))]
10+
public class FbxExportSettingsEditor : Editor {
11+
public override void OnInspectorGUI() {
12+
FbxExportSettings temp = (FbxExportSettings)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 FbxExportSettings : FbxSdk.EditorTools.ScriptableSingleton<FbxExportSettings>
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+
//t.hideFlags = HideFlags.HideAndDontSave;
78+
ScriptableSingleton<T>.s_Instance = t;
79+
}
80+
}
81+
protected virtual void Save(bool saveAsText)
82+
{
83+
if (ScriptableSingleton<T>.s_Instance == null)
84+
{
85+
Debug.Log("Cannot save ScriptableSingleton: no instance!");
86+
return;
87+
}
88+
string filePath = ScriptableSingleton<T>.GetFilePath();
89+
if (!string.IsNullOrEmpty(filePath))
90+
{
91+
string directoryName = Path.GetDirectoryName(filePath);
92+
if (!Directory.Exists(directoryName))
93+
{
94+
Directory.CreateDirectory(directoryName);
95+
}
96+
InternalEditorUtility.SaveToSerializedFileAndForget(new T[]
97+
{
98+
ScriptableSingleton<T>.s_Instance
99+
}, filePath, saveAsText);
100+
}
101+
}
102+
private static string GetFilePath()
103+
{
104+
Type typeFromHandle = typeof(T);
105+
object[] customAttributes = typeFromHandle.GetCustomAttributes(true);
106+
object[] array = customAttributes;
107+
for (int i = 0; i < array.Length; i++)
108+
{
109+
object obj = array[i];
110+
if (obj is FilePathAttribute)
111+
{
112+
FilePathAttribute filePathAttribute = obj as FilePathAttribute;
113+
return filePathAttribute.filepath;
114+
}
115+
}
116+
return null;
117+
}
118+
}
119+
120+
121+
[AttributeUsage(AttributeTargets.Class)]
122+
public class FilePathAttribute : Attribute
123+
{
124+
public enum Location
125+
{
126+
PreferencesFolder,
127+
ProjectFolder
128+
}
129+
public string filepath
130+
{
131+
get;
132+
set;
133+
}
134+
public FilePathAttribute(string relativePath, FilePathAttribute.Location location)
135+
{
136+
if (string.IsNullOrEmpty(relativePath))
137+
{
138+
Debug.LogError("Invalid relative path! (its null or empty)");
139+
return;
140+
}
141+
if (relativePath[0] == '/')
142+
{
143+
relativePath = relativePath.Substring(1);
144+
}
145+
if (location == FilePathAttribute.Location.PreferencesFolder)
146+
{
147+
this.filepath = InternalEditorUtility.unityPreferencesFolder + "/" + relativePath;
148+
}
149+
else
150+
{
151+
this.filepath = relativePath;
152+
}
153+
}
154+
}
155+
156+
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
404404
}
405405

406406
ExportTransform ( unityGo.transform, fbxNode);
407-
ExportMesh (GetMeshInfo( unityGo ), fbxNode, fbxScene);
407+
408+
bool weldVertices = FbxSdk.EditorTools.FbxExportSettings.instance.weldVertices;
409+
ExportMesh (GetMeshInfo( unityGo ), fbxNode, fbxScene, weldVertices);
408410

409411
if (Verbose)
410412
Debug.Log (string.Format ("exporting {0}", fbxNode.GetName ()));

0 commit comments

Comments
 (0)