CodeSmile AssetDatabase 1.9
Unity's AssetDatabase in enjoyable, consistent, concise, convenient, comprehensible, safe, documented form.
Loading...
Searching...
No Matches
Asset.Importer.cs
1// Copyright (C) 2021-2023 Steffen Itterheim
2// Refer to included LICENSE file for terms and conditions.
3
4using System;
5using System.Diagnostics.CodeAnalysis;
6using System.Reflection;
7using UnityEditor;
8using Object = UnityEngine.Object;
9
10namespace CodeSmileEditor
11{
12 public sealed partial class Asset
13 {
17 public static class Importer
18 {
31 public static Type GetActive([NotNull] Path path)
32 {
33#if UNITY_2022_2_OR_NEWER
34 return AssetDatabase.GetImporterType(path);
35#else
36 UnityEngine.Debug.LogWarning("GetImporterType not available in this Unity version - returning null");
37 return null;
38#endif
39 }
40
53 public static Type GetActive(GUID guid)
54 {
55#if UNITY_2022_2_OR_NEWER
56 return AssetDatabase.GetImporterType(guid);
57#else
58 UnityEngine.Debug.LogWarning("GetImporterType not available in this Unity version - returning null");
59 return null;
60#endif
61 }
62
75 public static Type GetActive([NotNull] Object asset) => GetActive(File.GetGuid(asset));
76
89 public static Type[] GetActive([NotNull] Path[] paths) => GetActive(Path.ToStrings(paths));
90
103 public static Type[] GetActive([NotNull] String[] paths)
104 {
105#if UNITY_2022_2_OR_NEWER
106 return AssetDatabase.GetImporterTypes(paths);
107#else
108 throw new NotSupportedException("GetImporterTypes not available in this Unity version");
109#endif
110 }
111
124 public static Type[] GetActive(ReadOnlySpan<GUID> guids)
125 {
126#if UNITY_2022_2_OR_NEWER
127 return AssetDatabase.GetImporterTypes(guids);
128#else
129 UnityEngine.Debug.LogWarning("GetImporterTypes not available in this Unity version - returning empty array");
130 return new Type[0];
131#endif
132 }
133
143 public static Type[] GetAvailable([NotNull] Path path)
144 {
145#if UNITY_2022_1_OR_NEWER
146 return AssetDatabase.GetAvailableImporters(path);
147#else
148 return AssetDatabase.GetAvailableImporterTypes(path);
149#endif
150 }
151
161 public static Type[] GetAvailable([NotNull] Object asset) => GetAvailable(Path.Get(asset));
162
173 public static Type GetDefault([NotNull] Path path)
174 {
175#if UNITY_2022_1_OR_NEWER
176 return AssetDatabase.GetDefaultImporter(path);
177#else
178 throw new NotSupportedException("GetDefaultImporter is not available in this Unity version");
179#endif
180 }
181
192 public static Type GetDefault([NotNull] Object asset) => GetDefault(Path.Get(asset));
193
205 public static Type GetOverride([NotNull] Path path) => AssetDatabase.GetImporterOverride(path);
206
218 public static Type GetOverride([NotNull] Object asset) => GetOverride(Path.Get(asset));
219
232 public static void SetOverride<T>([NotNull] Path path)
233#if UNITY_2022_1_OR_NEWER
234 where T : AssetImporter
235#else
236 where T : UnityEditor.AssetImporters.ScriptedImporter
237#endif
238 {
239 AssetDatabase.SetImporterOverride<T>(path);
240 }
241
254 public static void ClearOverride([NotNull] Path path) => AssetDatabase.ClearImporterOverride(path);
255
270 public static Boolean IsOverridden([NotNull] Path path) => GetDefault(path) != GetOverride(path);
271
284 [ExcludeFromCodeCoverage] // cannot be tested
285 public static void ApplySettings([NotNull] Path path) => AssetDatabase.WriteImportSettingsIfDirty(path);
286
287 internal static void SetImporterOverride([NotNull] Type value, [NotNull] String assetPath)
288 {
289 var methodName = "SetImporterOverride";
290 var bindingFlags = BindingFlags.Public | BindingFlags.Static;
291 var methodInfo = typeof(AssetDatabase).GetMethod(methodName, bindingFlags).MakeGenericMethod(value);
292 methodInfo.Invoke(null, new System.Object[] { assetPath });
293 }
294 }
295 }
296}
static GUID GetGuid([NotNull] Object asset)
Returns the GUID of an object. Returns an empty GUID if the object is null or not an asset.
Groups file related operations.
Definition Asset.File.cs:33
static Type GetDefault([NotNull] Path path)
Returns an asset's default importer type.
static void ApplySettings([NotNull] Path path)
Writes any unsaved changes of the given asset's importer to disk.
static Type GetActive([NotNull] Path path)
Gets the active AssetImporter type used for the given asset.
static Type GetActive([NotNull] Object asset)
Gets the active AssetImporter type used for the given asset.
static Type GetActive(GUID guid)
Gets the active AssetImporter type used for the given asset.
static Type[] GetActive(ReadOnlySpan< GUID > guids)
Gets the active AssetImporter types used for the given assets.
static Type GetDefault([NotNull] Object asset)
Returns an asset's default importer type.
static Type GetOverride([NotNull] Object asset)
Returns an asset's overridden importer type.
static Type[] GetAvailable([NotNull] Object asset)
Gets the available AssetImporter types for assets of this kind.
static Boolean IsOverridden([NotNull] Path path)
Returns true if the AssetImporter type for this asset has been overridden.
static Type[] GetActive([NotNull] String[] paths)
Gets the active AssetImporter types used for the given assets.
static void ClearOverride([NotNull] Path path)
Clears an AssetImporter override for the specified asset.
static Type GetOverride([NotNull] Path path)
Returns an asset's overridden importer type.
static Type[] GetActive([NotNull] Path[] paths)
Gets the active AssetImporter types used for the given assets.
static void SetOverride< T >([NotNull] Path path)
Sets the custom AssetImporter to use for the specified asset.
static Type[] GetAvailable([NotNull] Path path)
Gets the available AssetImporter types for assets of this kind.
Groups all AssetImporter related functionality.
static String[] ToStrings([NotNull] IEnumerable< Path > paths)
Converts an IEnumerable collection of Path instances to a string array.
static Path Get([NotNull] Object asset)
Gets the relative path of an asset.
Represents a relative path to an asset file or folder, typically under 'Assets' or 'Packages'.
Definition Asset.Path.cs:25