Tanoda
ExporterMenu.cs
Go to the documentation of this file.
1// ===============================================================================================
2// The MIT License (MIT) for UnityFBXExporter
3//
4// UnityFBXExporter was created for Building Crafter (http://u3d.as/ovC) a tool to rapidly
5// create high quality buildings right in Unity with no need to use 3D modeling programs.
6//
7// Copyright (c) 2016 | 8Bit Goose Games, Inc.
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13// of the Software, and to permit persons to whom the Software is furnished to do so,
14// subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in all
17// copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
20// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
21// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
24// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25//
26// ===============================================================================================
27
28using UnityEngine;
29using System.Collections;
30using UnityEditor;
31
33{
34 public class ExporterMenu : Editor
35 {
36 // Dropdown
37 [MenuItem("GameObject/FBX Exporter/Only GameObject", false, 40)]
38 public static void ExportDropdownGameObjectToFBX()
39 {
40 ExportCurrentGameObject(false, false);
41 }
42
43 [MenuItem("GameObject/FBX Exporter/With new Materials", false, 41)]
45 {
46 ExportCurrentGameObject(true, false);
47 }
48
49 [MenuItem("GameObject/FBX Exporter/With new Materials and Textures", false, 42)]
51 {
52 ExportCurrentGameObject(true, true);
53 }
54
55 // Assets
56 [MenuItem("Assets/FBX Exporter/Only GameObject", false, 30)]
57 public static void ExportGameObjectToFBX()
58 {
59 ExportCurrentGameObject(false, false);
60 }
61
62 [MenuItem("Assets/FBX Exporter/With new Materials", false, 31)]
64 {
65 ExportCurrentGameObject(true, false);
66 }
67
68 [MenuItem("Assets/FBX Exporter/With new Materials and Textures", false, 32)]
70 {
71 ExportCurrentGameObject(true, true);
72 }
73
74 private static void ExportCurrentGameObject(bool copyMaterials, bool copyTextures)
75 {
76 if(Selection.activeGameObject == null)
77 {
78 EditorUtility.DisplayDialog("No Object Selected", "Please select any GameObject to Export to FBX", "Okay");
79 return;
80 }
81
82 GameObject currentGameObject = Selection.activeObject as GameObject;
83
84 if(currentGameObject == null)
85 {
86 EditorUtility.DisplayDialog("Warning", "Item selected is not a GameObject", "Okay");
87 return;
88 }
89
90 ExportGameObject(currentGameObject, copyMaterials, copyTextures);
91 }
92
101 public static string ExportGameObject(GameObject gameObj, bool copyMaterials, bool copyTextures, string oldPath = null)
102 {
103 if(gameObj == null)
104 {
105 EditorUtility.DisplayDialog("Object is null", "Please select any GameObject to Export to FBX", "Okay");
106 return null;
107 }
108
109 string newPath = GetNewPath(gameObj, oldPath);
110
111 if(newPath != null && newPath.Length != 0)
112 {
113 bool isSuccess = FBXExporter.ExportGameObjToFBX(gameObj, newPath, copyMaterials, copyTextures);
114
115 if(isSuccess)
116 {
117 return newPath;
118 }
119 else
120 EditorUtility.DisplayDialog("Warning", "The extension probably wasn't an FBX file, could not export.", "Okay");
121 }
122 return null;
123 }
124
131 private static string GetNewPath(GameObject gameObject, string oldPath = null)
132 {
133 // NOTE: This must return a path with the starting "Assets/" or else textures won't copy right
134
135 string name = gameObject.name;
136
137 string newPath = null;
138 if(oldPath == null)
139 newPath = EditorUtility.SaveFilePanelInProject("Export FBX File", name + ".fbx", "fbx", "Export " + name + " GameObject to a FBX file");
140 else
141 {
142 if(oldPath.StartsWith("/Assets"))
143 {
144 oldPath = Application.dataPath.Remove(Application.dataPath.LastIndexOf("/Assets"), 7) + oldPath;
145 oldPath = oldPath.Remove(oldPath.LastIndexOf('/'), oldPath.Length - oldPath.LastIndexOf('/'));
146 }
147 newPath = EditorUtility.SaveFilePanel("Export FBX File", oldPath, name + ".fbx", "fbx");
148 }
149
150 int assetsIndex = newPath.IndexOf("Assets");
151
152 if(assetsIndex < 0)
153 return null;
154
155 if(assetsIndex > 0)
156 newPath = newPath.Remove(0, assetsIndex);
157
158 return newPath;
159 }
160 }
161}
static string ExportGameObject(GameObject gameObj, bool copyMaterials, bool copyTextures, string oldPath=null)
Exports ANY Game Object given to it. Will provide a dialog and return the path of the newly exported ...
static void ExportDropdownGameObjectAndMaterialsToFBX()
Definition: ExporterMenu.cs:44
static void ExportDropdownGameObjectToFBX()
Definition: ExporterMenu.cs:38
static void ExportGameObjectAndMaterialsTexturesToFBX()
Definition: ExporterMenu.cs:69
static void ExportGameObjectAndMaterialsToFBX()
Definition: ExporterMenu.cs:63
static void ExportDropdownGameObjectAndMaterialsTexturesToFBX()
Definition: ExporterMenu.cs:50
static void ExportGameObjectToFBX()
Definition: ExporterMenu.cs:57
static bool ExportGameObjToFBX(GameObject gameObj, string newPath, bool copyMaterials=false, bool copyTextures=false)
Definition: FBXExporter.cs:41