Tanoda
TriLibScriptedImporter.cs
Go to the documentation of this file.
1#pragma warning disable CS0105
2using UnityEngine;
3using TriLibCore.Interfaces;
4using UnityEditor;
5#if UNITY_2020_2_OR_NEWER
6using UnityEditor.AssetImporters;
7#else
8using UnityEditor.Experimental.AssetImporters;
9#endif
10namespace TriLibCore.Editor
11{
12 [ScriptedImporter(1, new[] { /*"gltf", "glb",*/ "ply", "stl", "3mf" })]
13 public class TriLibScriptedImporter : ScriptedImporter
14 {
16 {
17 get
18 {
19 var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions(true);
20 if (userData != null && userData != "null")
21 {
22 EditorJsonUtility.FromJsonOverwrite(userData, assetLoaderOptions);
23 }
24 return assetLoaderOptions;
25 }
26 set => userData = EditorJsonUtility.ToJson(value);
27 }
28
29 public override void OnImportAsset(AssetImportContext assetImportContext)
30 {
31 var assetLoaderOptions = AssetLoaderOptions;
32 var assetLoaderContext = AssetLoader.LoadModelFromFileNoThread(assetImportContext.assetPath, OnError, null, assetLoaderOptions, assetImportContext);
33 if (assetLoaderContext.RootGameObject != null)
34 {
35 assetImportContext.AddObjectToAsset("Main", assetLoaderContext.RootGameObject);
36 assetImportContext.SetMainObject(assetLoaderContext.RootGameObject);
37 foreach (var allocation in assetLoaderContext.Allocations)
38 {
39 if (string.IsNullOrWhiteSpace(allocation.name))
40 {
41 allocation.name = allocation.GetType().Name;
42 }
43 assetImportContext.AddObjectToAsset(allocation.name, allocation);
44 }
45 }
46 }
47
48 private static void OnError(IContextualizedError contextualizedError)
49 {
50 var exception = contextualizedError.GetInnerException();
51 if (contextualizedError.GetContext() is IAssetLoaderContext assetLoaderContext)
52 {
53 var assetImportContext = (AssetImportContext)assetLoaderContext.Context.CustomData;
54 if (assetLoaderContext.Context.Options.ShowLoadingWarnings)
55 {
56 assetImportContext.LogImportError(exception.ToString());
57 return;
58 }
59 }
60 Debug.LogError(exception.ToString());
61 }
62 }
63}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Definition: TriLibLoader.cs:9
override void OnImportAsset(AssetImportContext assetImportContext)