77using UnityEngine ;
88using UnityEngine . SceneManagement ;
99using UnityEngine . Serialization ;
10+ using Object = UnityEngine . Object ;
1011
1112namespace UnityGLTF
1213{
@@ -31,69 +32,81 @@ public static string RetrieveTexturePath(UnityEngine.Texture texture)
3132 return path ;
3233 }
3334
34- private static bool TryGetExportNameAndRootTransformsFromSelection ( out string sceneName , out Transform [ ] rootTransforms )
35+ private static bool TryGetExportNameAndRootTransformsFromSelection ( out string sceneName , out Transform [ ] rootTransforms , out Object [ ] rootResources )
3536 {
3637 if ( Selection . transforms . Length > 1 )
3738 {
3839 sceneName = SceneManager . GetActiveScene ( ) . name ;
3940 rootTransforms = Selection . transforms ;
41+ rootResources = null ;
4042 return true ;
4143 }
4244 if ( Selection . transforms . Length == 1 )
4345 {
4446 sceneName = Selection . activeGameObject . name ;
4547 rootTransforms = Selection . transforms ;
48+ rootResources = null ;
4649 return true ;
4750 }
4851 if ( Selection . objects . Any ( ) && Selection . objects . All ( x => x is GameObject ) )
4952 {
5053 sceneName = Selection . objects . First ( ) . name ;
5154 rootTransforms = Selection . objects . Select ( x => ( x as GameObject ) . transform ) . ToArray ( ) ;
55+ rootResources = null ;
56+ return true ;
57+ }
58+
59+ if ( Selection . objects . Any ( ) && Selection . objects . All ( x => x is Material ) )
60+ {
61+ sceneName = "Material Library" ;
62+ rootTransforms = null ;
63+ rootResources = Selection . objects ;
5264 return true ;
5365 }
5466
5567 sceneName = null ;
5668 rootTransforms = null ;
69+ rootResources = null ;
5770 return false ;
5871 }
5972
6073 [ MenuItem ( MenuPrefix + ExportGltf , true ) ]
6174 [ MenuItem ( MenuPrefixGameObject + ExportGltf , true ) ]
6275 private static bool ExportSelectedValidate ( )
6376 {
64- return TryGetExportNameAndRootTransformsFromSelection ( out _ , out _ ) ;
77+ return TryGetExportNameAndRootTransformsFromSelection ( out _ , out _ , out _ ) ;
6578 }
6679
6780 [ MenuItem ( MenuPrefix + ExportGltf ) ]
6881 [ MenuItem ( MenuPrefixGameObject + ExportGltf , false , 33 ) ]
6982 private static void ExportSelected ( )
7083 {
71- if ( ! TryGetExportNameAndRootTransformsFromSelection ( out var sceneName , out var rootTransforms ) )
84+ if ( ! TryGetExportNameAndRootTransformsFromSelection ( out var sceneName , out var rootTransforms , out var rootResources ) )
7285 {
7386 Debug . LogError ( "Can't export: selection is empty" ) ;
7487 return ;
7588 }
7689
77- Export ( rootTransforms , false , sceneName ) ;
90+ Export ( rootTransforms , rootResources , false , sceneName ) ;
7891 }
7992
8093 [ MenuItem ( MenuPrefix + ExportGlb , true ) ]
8194 [ MenuItem ( MenuPrefixGameObject + ExportGlb , true ) ]
8295 private static bool ExportGLBSelectedValidate ( )
8396 {
84- return TryGetExportNameAndRootTransformsFromSelection ( out _ , out _ ) ;
97+ return TryGetExportNameAndRootTransformsFromSelection ( out _ , out _ , out _ ) ;
8598 }
8699
87100 [ MenuItem ( MenuPrefix + ExportGlb ) ]
88101 [ MenuItem ( MenuPrefixGameObject + ExportGlb , false , 34 ) ]
89102 private static void ExportGLBSelected ( )
90103 {
91- if ( ! TryGetExportNameAndRootTransformsFromSelection ( out var sceneName , out var rootTransforms ) )
104+ if ( ! TryGetExportNameAndRootTransformsFromSelection ( out var sceneName , out var rootTransforms , out var rootResources ) )
92105 {
93106 Debug . LogError ( "Can't export: selection is empty" ) ;
94107 return ;
95108 }
96- Export ( rootTransforms , true , sceneName ) ;
109+ Export ( rootTransforms , rootResources , true , sceneName ) ;
97110 }
98111
99112 [ MenuItem ( MenuPrefix + "Export active scene as glTF" ) ]
@@ -103,7 +116,7 @@ private static void ExportScene()
103116 var gameObjects = scene . GetRootGameObjects ( ) ;
104117 var transforms = Array . ConvertAll ( gameObjects , gameObject => gameObject . transform ) ;
105118
106- Export ( transforms , false , scene . name ) ;
119+ Export ( transforms , null , false , scene . name ) ;
107120 }
108121
109122 [ MenuItem ( MenuPrefix + "Export active scene as GLB" ) ]
@@ -113,15 +126,31 @@ private static void ExportSceneGLB()
113126 var gameObjects = scene . GetRootGameObjects ( ) ;
114127 var transforms = Array . ConvertAll ( gameObjects , gameObject => gameObject . transform ) ;
115128
116- Export ( transforms , true , scene . name ) ;
129+ Export ( transforms , null , true , scene . name ) ;
117130 }
118131
119- private static void Export ( Transform [ ] transforms , bool binary , string sceneName )
132+ private static void Export ( Transform [ ] transforms , Object [ ] resources , bool binary , string sceneName )
120133 {
121134 var settings = GLTFSettings . GetOrCreateSettings ( ) ;
122135 var exportOptions = new ExportContext ( settings ) { TexturePathRetriever = RetrieveTexturePath } ;
123136 var exporter = new GLTFSceneExporter ( transforms , exportOptions ) ;
124137
138+ if ( resources != null )
139+ {
140+ exportOptions . AfterSceneExport += ( sceneExporter , _ ) =>
141+ {
142+ foreach ( var resource in resources )
143+ {
144+ if ( resource is Material material )
145+ sceneExporter . ExportMaterial ( material ) ;
146+ if ( resource is Texture2D texture )
147+ sceneExporter . ExportTexture ( texture , "unknown" ) ;
148+ if ( resource is Mesh mesh )
149+ sceneExporter . ExportMesh ( mesh ) ;
150+ }
151+ } ;
152+ }
153+
125154 var invokedByShortcut = Event . current ? . type == EventType . KeyDown ;
126155 var path = settings . SaveFolderPath ;
127156 if ( ! invokedByShortcut || ! Directory . Exists ( path ) )
@@ -132,7 +161,8 @@ private static void Export(Transform[] transforms, bool binary, string sceneName
132161 var ext = binary ? ".glb" : ".gltf" ;
133162 var resultFile = GLTFSceneExporter . GetFileName ( path , sceneName , ext ) ;
134163 settings . SaveFolderPath = path ;
135- if ( binary )
164+
165+ if ( binary )
136166 exporter . SaveGLB ( path , sceneName ) ;
137167 else
138168 exporter . SaveGLTFandBin ( path , sceneName ) ;
0 commit comments