|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditor; |
| 5 | +using FbxExporters.EditorTools; |
| 6 | +#if UNITY_2018_1_OR_NEWER |
| 7 | +using UnityEditor.Presets; |
| 8 | +#endif |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace FbxExporters |
| 12 | +{ |
| 13 | + namespace Editor |
| 14 | + { |
| 15 | + public class ConvertToPrefabEditorWindow : ExportOptionsEditorWindow |
| 16 | + { |
| 17 | + protected override GUIContent WindowTitle { get { return new GUIContent ("Convert Options"); }} |
| 18 | + protected override float MinWindowHeight { get { return 350; } } // determined by trial and error |
| 19 | + protected override string ExportButtonName { get { return "Convert"; } } |
| 20 | + private string m_prefabFileName = ""; |
| 21 | + |
| 22 | + private float m_prefabExtLabelWidth; |
| 23 | + |
| 24 | + protected override bool DisableNameSelection { |
| 25 | + get { |
| 26 | + return (ToExport != null && ToExport.Length > 1); |
| 27 | + } |
| 28 | + } |
| 29 | + protected override bool DisableTransferAnim { |
| 30 | + get { |
| 31 | + return ToExport == null || ToExport.Length > 1; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static void Init (IEnumerable<GameObject> toConvert) |
| 36 | + { |
| 37 | + ConvertToPrefabEditorWindow window = CreateWindow<ConvertToPrefabEditorWindow> (); |
| 38 | + window.InitializeWindow (); |
| 39 | + window.SetGameObjectsToConvert (toConvert); |
| 40 | + window.Show (); |
| 41 | + } |
| 42 | + |
| 43 | + protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){ |
| 44 | + ToExport = toConvert.OrderBy (go => go.name).ToArray (); |
| 45 | + |
| 46 | + TransferAnimationSource = null; |
| 47 | + TransferAnimationDest = null; |
| 48 | + |
| 49 | +<<<<<<< HEAD |
| 50 | + if (m_toConvert.Length == 1) { |
| 51 | + var go = m_toConvert [0]; |
| 52 | + // check if the GameObject is a model instance, use as default filename and path if it is |
| 53 | + if(ConvertToModel.IsModelInstance(go)) { |
| 54 | + var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject; |
| 55 | + var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset); |
| 56 | + // remove Assets/ from beginning of path |
| 57 | + mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length); |
| 58 | + |
| 59 | + m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath); |
| 60 | + ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath)); |
| 61 | + } else { |
| 62 | + m_prefabFileName = go.name; |
| 63 | + } |
| 64 | + } else if (m_toConvert.Length > 1) { |
| 65 | +======= |
| 66 | + if (ToExport.Length == 1) { |
| 67 | + m_prefabFileName = ToExport [0].name; |
| 68 | + |
| 69 | + // if only one object selected, set transfer source/dest to this object |
| 70 | + var go = ModelExporter.GetGameObject (ToExport [0]); |
| 71 | + if (go) { |
| 72 | + TransferAnimationSource = go.transform; |
| 73 | + TransferAnimationDest = go.transform; |
| 74 | + } |
| 75 | + } else if (ToExport.Length > 1) { |
| 76 | +>>>>>>> master |
| 77 | + m_prefabFileName = "(automatic)"; |
| 78 | + } |
| 79 | + |
| 80 | + this.SetFilename (m_prefabFileName); |
| 81 | + } |
| 82 | + |
| 83 | + protected override void OnEnable () |
| 84 | + { |
| 85 | + base.OnEnable (); |
| 86 | + if (!m_innerEditor) { |
| 87 | + m_innerEditor = UnityEditor.Editor.CreateEditor (ExportSettings.instance.convertToPrefabSettings); |
| 88 | + } |
| 89 | + m_prefabExtLabelWidth = m_fbxExtLabelStyle.CalcSize (new GUIContent (".prefab")).x; |
| 90 | + } |
| 91 | + |
| 92 | + protected override void Export () |
| 93 | + { |
| 94 | + var fbxDirPath = ExportSettings.GetFbxAbsoluteSavePath (); |
| 95 | + var fbxPath = System.IO.Path.Combine (fbxDirPath, m_exportFileName + ".fbx"); |
| 96 | + |
| 97 | + var prefabDirPath = ExportSettings.GetPrefabAbsoluteSavePath (); |
| 98 | + var prefabPath = System.IO.Path.Combine (prefabDirPath, m_prefabFileName + ".prefab"); |
| 99 | + |
| 100 | +<<<<<<< HEAD |
| 101 | + if (m_toConvert == null) { |
| 102 | +======= |
| 103 | + // check if file already exists, give a warning if it does |
| 104 | + if (!OverwriteExistingFile (fbxPath) || !OverwriteExistingFile (prefabPath)) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + if (ToExport == null) { |
| 109 | +>>>>>>> master |
| 110 | + Debug.LogError ("FbxExporter: missing object for conversion"); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | +<<<<<<< HEAD |
| 115 | + if (m_toConvert.Length == 1) { |
| 116 | + var go = m_toConvert [0]; |
| 117 | + |
| 118 | + if (!OverwriteExistingFile (prefabPath)) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // Only create the prefab (no FBX export) if we have selected the root of a model prefab instance. |
| 123 | + if(ConvertToModel.IsModelInstance(go)) { |
| 124 | + // don't re-export fbx |
| 125 | + // create prefab out of model instance in scene, link to existing fbx |
| 126 | + var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject; |
| 127 | + var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset); |
| 128 | + var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath; |
| 129 | + var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath); |
| 130 | + |
| 131 | + if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) { |
| 132 | + ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath); |
| 133 | + return; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // check if file already exists, give a warning if it does |
| 138 | + if (!OverwriteExistingFile (fbxPath)) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | +======= |
| 143 | + if (ToExport.Length == 1) { |
| 144 | + var go = ModelExporter.GetGameObject (ToExport [0]); |
| 145 | +>>>>>>> master |
| 146 | + ConvertToModel.Convert ( |
| 147 | + go, fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info |
| 148 | + ); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + foreach (var obj in ToExport) { |
| 153 | + var go = ModelExporter.GetGameObject (obj); |
| 154 | + ConvertToModel.Convert ( |
| 155 | + go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + protected override ExportOptionsSettingsSerializeBase SettingsObject |
| 161 | + { |
| 162 | + get { return ExportSettings.instance.convertToPrefabSettings.info; } |
| 163 | + } |
| 164 | + #if UNITY_2018_1_OR_NEWER |
| 165 | + protected override void ShowPresetReceiver () |
| 166 | + { |
| 167 | + ShowPresetReceiver (ExportSettings.instance.convertToPrefabSettings); |
| 168 | + } |
| 169 | + #endif |
| 170 | + protected override void CreateCustomUI () |
| 171 | + { |
| 172 | + GUILayout.BeginHorizontal (); |
| 173 | + EditorGUILayout.LabelField(new GUIContent( |
| 174 | + "Prefab Name", |
| 175 | + "Filename to save prefab to."),GUILayout.Width(LabelWidth-TextFieldAlignOffset)); |
| 176 | + |
| 177 | + EditorGUI.BeginDisabledGroup (DisableNameSelection); |
| 178 | + // Show the export name with an uneditable ".prefab" at the end |
| 179 | + //------------------------------------- |
| 180 | + EditorGUILayout.BeginVertical (); |
| 181 | + EditorGUILayout.BeginHorizontal(EditorStyles.textField, GUILayout.Height(EditorGUIUtility.singleLineHeight)); |
| 182 | + EditorGUI.indentLevel--; |
| 183 | + // continually resize to contents |
| 184 | + var textFieldSize = m_nameTextFieldStyle.CalcSize (new GUIContent(m_prefabFileName)); |
| 185 | + m_prefabFileName = EditorGUILayout.TextField (m_prefabFileName, m_nameTextFieldStyle, GUILayout.Width(textFieldSize.x + 5), GUILayout.MinWidth(5)); |
| 186 | + m_prefabFileName = ModelExporter.ConvertToValidFilename (m_prefabFileName); |
| 187 | + |
| 188 | + EditorGUILayout.LabelField ("<color=#808080ff>.prefab</color>", m_fbxExtLabelStyle, GUILayout.Width(m_prefabExtLabelWidth)); |
| 189 | + EditorGUI.indentLevel++; |
| 190 | + |
| 191 | + EditorGUILayout.EndHorizontal(); |
| 192 | + EditorGUILayout.EndVertical (); |
| 193 | + //----------------------------------- |
| 194 | + EditorGUI.EndDisabledGroup (); |
| 195 | + GUILayout.EndHorizontal (); |
| 196 | + |
| 197 | + GUILayout.BeginHorizontal(); |
| 198 | + EditorGUILayout.LabelField(new GUIContent( |
| 199 | + "Prefab Path", |
| 200 | + "Relative path for saving Linked Prefabs."),GUILayout.Width(LabelWidth - FieldOffset)); |
| 201 | + |
| 202 | + var pathLabels = ExportSettings.GetRelativePrefabSavePaths(); |
| 203 | + |
| 204 | + ExportSettings.instance.selectedPrefabPath = EditorGUILayout.Popup (ExportSettings.instance.selectedPrefabPath, pathLabels, GUILayout.MinWidth(SelectableLabelMinWidth)); |
| 205 | + |
| 206 | + if (GUILayout.Button(new GUIContent("...", "Browse to a new location to save prefab to"), EditorStyles.miniButton, GUILayout.Width(BrowseButtonWidth))) |
| 207 | + { |
| 208 | + string initialPath = Application.dataPath; |
| 209 | + |
| 210 | + string fullPath = EditorUtility.OpenFolderPanel( |
| 211 | + "Select Linked Prefab Save Path", initialPath, null |
| 212 | + ); |
| 213 | + |
| 214 | + // Unless the user canceled, make sure they chose something in the Assets folder. |
| 215 | + if (!string.IsNullOrEmpty(fullPath)) |
| 216 | + { |
| 217 | + var relativePath = ExportSettings.ConvertToAssetRelativePath(fullPath); |
| 218 | + if (string.IsNullOrEmpty(relativePath)) |
| 219 | + { |
| 220 | + Debug.LogWarning("Please select a location in the Assets folder"); |
| 221 | + } |
| 222 | + else |
| 223 | + { |
| 224 | + ExportSettings.AddPrefabSavePath(relativePath); |
| 225 | + |
| 226 | + // Make sure focus is removed from the selectable label |
| 227 | + // otherwise it won't update |
| 228 | + GUIUtility.hotControl = 0; |
| 229 | + GUIUtility.keyboardControl = 0; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + GUILayout.EndHorizontal(); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments