3
3
using UnityEngine ;
4
4
using System . Linq ;
5
5
using UnityEngine . Formats . Fbx . Exporter ;
6
+ using System . Runtime . Serialization ;
6
7
7
8
namespace UnityEditor . Formats . Fbx . Exporter
8
9
{
10
+ public class ConvertToLinkedPrefabException : System . Exception
11
+ {
12
+ public ConvertToLinkedPrefabException ( )
13
+ {
14
+ }
15
+
16
+ public ConvertToLinkedPrefabException ( string message )
17
+ : base ( message )
18
+ {
19
+ }
20
+
21
+ public ConvertToLinkedPrefabException ( string message , System . Exception inner )
22
+ : base ( message , inner )
23
+ {
24
+ }
25
+
26
+ protected ConvertToLinkedPrefabException ( SerializationInfo info , StreamingContext context )
27
+ : base ( info , context )
28
+ {
29
+ }
30
+ }
31
+
9
32
public static class ConvertToModel
10
33
{
11
34
const string GameObjectMenuItemName = "GameObject/Convert To Linked Prefab Instance..." ;
@@ -149,6 +172,11 @@ public static GameObject Convert (
149
172
string prefabFullPath = null ,
150
173
ConvertToPrefabSettingsSerialize exportOptions = null )
151
174
{
175
+ if ( toConvert == null )
176
+ {
177
+ throw new System . ArgumentNullException ( "toConvert" ) ;
178
+ }
179
+
152
180
// If we selected the something that's already backed by an
153
181
// FBX, don't export.
154
182
var mainAsset = GetOrCreateFbxAsset ( toConvert , fbxDirectoryFullPath , fbxFullPath , exportOptions ) ;
@@ -214,11 +242,16 @@ public static bool WillExportFbx(GameObject toConvert) {
214
242
/// file. Overrides fbxDirectoryFullPath. Ignored if toConvert is an
215
243
/// fbx asset or an instance of an fbx.</param>
216
244
/// <returns>The root of a model prefab asset.</returns>
217
- public static GameObject GetOrCreateFbxAsset ( GameObject toConvert ,
245
+ internal static GameObject GetOrCreateFbxAsset ( GameObject toConvert ,
218
246
string fbxDirectoryFullPath = null ,
219
247
string fbxFullPath = null ,
220
248
ConvertToPrefabSettingsSerialize exportOptions = null )
221
249
{
250
+ if ( toConvert == null )
251
+ {
252
+ throw new System . ArgumentNullException ( "toConvert" ) ;
253
+ }
254
+
222
255
var mainAsset = GetFbxAssetOrNull ( toConvert ) ;
223
256
if ( mainAsset ) {
224
257
return mainAsset ;
@@ -253,15 +286,15 @@ public static GameObject GetOrCreateFbxAsset(GameObject toConvert,
253
286
exportOptions != null ? exportOptions : new ConvertToPrefabSettingsSerialize ( )
254
287
) ;
255
288
if ( fbxActualPath != fbxFullPath ) {
256
- throw new System . Exception ( "Failed to convert " + toConvert . name ) ;
289
+ throw new ConvertToLinkedPrefabException ( "Failed to convert " + toConvert . name ) ;
257
290
}
258
291
}
259
292
260
293
// Replace w Model asset. LoadMainAssetAtPath wants a path
261
294
// relative to the project, not relative to the assets folder.
262
295
var unityMainAsset = AssetDatabase . LoadMainAssetAtPath ( projectRelativePath ) as GameObject ;
263
296
if ( ! unityMainAsset ) {
264
- throw new System . Exception ( "Failed to convert " + toConvert . name ) ;
297
+ throw new ConvertToLinkedPrefabException ( "Failed to convert " + toConvert . name ) ;
265
298
}
266
299
267
300
// Copy the mesh/materials from the FBX
@@ -327,6 +360,11 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
327
360
string prefabDirectoryFullPath = null ,
328
361
string prefabFullPath = null )
329
362
{
363
+ if ( instance == null )
364
+ {
365
+ throw new System . ArgumentNullException ( "instance" ) ;
366
+ }
367
+
330
368
if ( PrefabUtility . GetPrefabType ( instance ) == PrefabType . PrefabInstance ) {
331
369
return PrefabUtility . ReplacePrefab ( instance , PrefabUtility . GetCorrespondingObjectFromSource ( instance ) ) ;
332
370
}
@@ -351,7 +389,7 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
351
389
352
390
var prefab = PrefabUtility . CreatePrefab ( prefabFileName , instance , ReplacePrefabOptions . ConnectToPrefab ) ;
353
391
if ( ! prefab ) {
354
- throw new System . Exception ( string . Format ( "Failed to create prefab asset in [{0}]" , prefabFileName ) ) ;
392
+ throw new ConvertToLinkedPrefabException ( string . Format ( "Failed to create prefab asset in [{0}]" , prefabFileName ) ) ;
355
393
}
356
394
return prefab ;
357
395
}
@@ -365,6 +403,11 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
365
403
/// <param name="unityMainAsset">Main asset in the FBX.</param>
366
404
public static void SetupFbxPrefab ( GameObject toSetUp , GameObject unityMainAsset )
367
405
{
406
+ if ( toSetUp == null )
407
+ {
408
+ throw new System . ArgumentNullException ( "toSetUp" ) ;
409
+ }
410
+
368
411
// Set up the FbxPrefab component so it will auto-update.
369
412
// Make sure to delete whatever FbxPrefab history we had.
370
413
var fbxPrefab = toSetUp . GetComponent < FbxPrefab > ( ) ;
@@ -385,7 +428,7 @@ public static void SetupFbxPrefab(GameObject toSetUp, GameObject unityMainAsset)
385
428
/// </summary>
386
429
/// <returns>The root of a model prefab asset, or null.</returns>
387
430
/// <param name="go">A gameobject either in the scene or in the assets folder.</param>
388
- public static GameObject GetFbxAssetOrNull ( GameObject go ) {
431
+ internal static GameObject GetFbxAssetOrNull ( GameObject go ) {
389
432
// Children of model prefab instances will also have "model prefab instance"
390
433
// as their prefab type, so it is important that it is the root that is selected.
391
434
//
@@ -497,7 +540,7 @@ public static void EnforceUniqueNames(IEnumerable<GameObject> exportSet)
497
540
/// </summary>
498
541
/// <param name="dest">GameObject to update.</param>
499
542
/// <param name="source">Source to update from.</param>
500
- public static void UpdateFromSourceRecursive ( GameObject dest , GameObject source )
543
+ internal static void UpdateFromSourceRecursive ( GameObject dest , GameObject source )
501
544
{
502
545
// recurse over orig, for each transform finding the corresponding transform in the FBX
503
546
// and copying the meshes and materials over from the FBX
@@ -524,7 +567,7 @@ public static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
524
567
/// <returns>Dictionary containing the name to source game object.</returns>
525
568
/// <param name="dest">Destination GameObject.</param>
526
569
/// <param name="source">Source GameObject.</param>
527
- public static Dictionary < string , GameObject > MapNameToSourceRecursive ( GameObject dest , GameObject source ) {
570
+ internal static Dictionary < string , GameObject > MapNameToSourceRecursive ( GameObject dest , GameObject source ) {
528
571
var nameToGO = new Dictionary < string , GameObject > ( ) ;
529
572
530
573
var q = new Queue < Transform > ( ) ;
@@ -569,7 +612,7 @@ public static Dictionary<string,GameObject> MapNameToSourceRecursive(GameObject
569
612
///
570
613
/// The 'from' hierarchy is not modified.
571
614
/// </summary>
572
- public static void CopyComponents ( GameObject to , GameObject from ) {
615
+ internal static void CopyComponents ( GameObject to , GameObject from ) {
573
616
var originalComponents = new List < Component > ( to . GetComponents < Component > ( ) ) ;
574
617
575
618
// UNI-27534: This fixes the issue where the mesh collider would not update to point to the mesh in the fbx after export
0 commit comments