|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using UnityEngine; |
| 10 | +using UnityEngine.Scripting; |
| 11 | +using Object = UnityEngine.Object; |
| 12 | + |
| 13 | +namespace UnityEditor.Experimental.AssetImporters |
| 14 | +{ |
| 15 | + // Class concept: helper class that holds a generated asset object, during the import phase of scripted importer. |
| 16 | + // |
| 17 | + // Motivation: A source asset file can produce multiple assets (a main one and multple sub assets). Needed a way to genericaly hold these sub assets of the source Asset. |
| 18 | + class ImportedObject |
| 19 | + { |
| 20 | + // Is this the main part of the asset being imported? |
| 21 | + public bool mainAsset { get; set; } |
| 22 | + |
| 23 | + public Object asset { get; set; } |
| 24 | + |
| 25 | + // Unique identifier, within imported asset context, of asset |
| 26 | + public string identifier { get; set; } |
| 27 | + |
| 28 | + public Texture2D thumbnail { get; set; } |
| 29 | + } |
| 30 | + |
| 31 | + public class AssetImportContext |
| 32 | + { |
| 33 | + public string assetPath { get; internal set; } |
| 34 | + public BuildTarget selectedBuildTarget { get; internal set; } |
| 35 | + |
| 36 | + List<ImportedObject> m_SubAssets = new List<ImportedObject>(); |
| 37 | + internal List<ImportedObject> subAssets |
| 38 | + { |
| 39 | + get { return m_SubAssets; } |
| 40 | + } |
| 41 | + |
| 42 | + internal AssetImportContext() |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + // Adds an asset object to the resulting asset list and tags it as the main asset. |
| 47 | + // A minimum and a maximum of one asset must be tagged as the main asset. |
| 48 | + // param identifier: Unique identifier, within the source asset, that remains the same accross re-import events of the given source asset file. |
| 49 | + public void SetMainAsset(string identifier, Object asset) |
| 50 | + { |
| 51 | + AddAsset(true, identifier, asset, null); |
| 52 | + } |
| 53 | + |
| 54 | + // Adds an asset object to the resulting asset list and tags it as the main asset. |
| 55 | + // A minimum and a maximum of one asset must be tagged as the main asset. |
| 56 | + // param identifier: Unique identifier, within the source asset, that remains the same accross re-import events of the given source asset file. |
| 57 | + public void SetMainAsset(string identifier, Object asset, Texture2D thumbnail) |
| 58 | + { |
| 59 | + AddAsset(true, identifier, asset, thumbnail); |
| 60 | + } |
| 61 | + |
| 62 | + // Adds a sub asset object to the resulting asset list, that is Not the main asset. |
| 63 | + // param identifier: Unique identifier, within the source asset, that remains the same accross re-import events of the given source asset file. |
| 64 | + public void AddSubAsset(string identifier, Object asset) |
| 65 | + { |
| 66 | + AddAsset(false, identifier, asset, null); |
| 67 | + } |
| 68 | + |
| 69 | + // Adds a sub asset object to the resulting asset list, that is Not the main asset. |
| 70 | + // param identifier: Unique identifier, within the source asset, that remains the same accross re-import events of the given source asset file. |
| 71 | + public void AddSubAsset(string identifier, Object asset, Texture2D thumbnail) |
| 72 | + { |
| 73 | + AddAsset(false, identifier, asset, thumbnail); |
| 74 | + } |
| 75 | + |
| 76 | + void AddAsset(bool main, string identifier, Object asset, Texture2D thumbnail) |
| 77 | + { |
| 78 | + if (asset == null) |
| 79 | + { |
| 80 | + throw new ArgumentNullException("asset", "Cannot add a null asset : " + (identifier ?? "<null>")); |
| 81 | + } |
| 82 | + |
| 83 | + var mainAsset = m_SubAssets.FirstOrDefault(x => x.mainAsset); |
| 84 | + if (main && mainAsset != null) |
| 85 | + { |
| 86 | + throw new Exception(String.Format(@"A Main asset has already been added and only one is allowed: ""{0}"" conflicting on ""{1}"" and ""{2}""", assetPath, mainAsset.identifier, identifier)); |
| 87 | + } |
| 88 | + |
| 89 | + var desc = new ImportedObject() |
| 90 | + { |
| 91 | + mainAsset = main, |
| 92 | + identifier = identifier, |
| 93 | + asset = asset, |
| 94 | + thumbnail = thumbnail |
| 95 | + }; |
| 96 | + if (main) |
| 97 | + m_SubAssets.Insert(0, desc); |
| 98 | + else |
| 99 | + m_SubAssets.Add(desc); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Class Concept: Root, abstract class for all Asset importers implemented in C#. |
| 104 | + public abstract class ScriptedImporter : AssetImporter |
| 105 | + { |
| 106 | + // Struct Concept: simple struct to carry import request arguments from native code. |
| 107 | + // Must mirror native counterpart exactly! |
| 108 | + [StructLayout(LayoutKind.Sequential)] |
| 109 | + struct ImportRequest |
| 110 | + { |
| 111 | + public readonly string m_AssetSourcePath; |
| 112 | + public readonly BuildTarget m_SelectedBuildTarget; |
| 113 | + } |
| 114 | + |
| 115 | + // Struct Concept: simple struct to carry resulting objects of import back to native code. |
| 116 | + // Must mirror native counterpart exactly! |
| 117 | + [StructLayout(LayoutKind.Sequential)] |
| 118 | + struct ImportResult |
| 119 | + { |
| 120 | + public Object[] m_Assets; |
| 121 | + public string[] m_AssetNames; |
| 122 | + public Texture2D[] m_Thumbnails; |
| 123 | + } |
| 124 | + |
| 125 | + // Called by native code to invoke the import handling code of the specialized scripted importer class. |
| 126 | + // Marshals the data between the two models (native / managed) |
| 127 | + [RequiredByNativeCode] |
| 128 | + ImportResult GenerateAssetData(ImportRequest request) |
| 129 | + { |
| 130 | + var ctx = new AssetImportContext() |
| 131 | + { |
| 132 | + assetPath = request.m_AssetSourcePath, |
| 133 | + selectedBuildTarget = request.m_SelectedBuildTarget |
| 134 | + }; |
| 135 | + |
| 136 | + OnImportAsset(ctx); |
| 137 | + |
| 138 | + if (!ctx.subAssets.Any((x) => x.mainAsset)) |
| 139 | + throw new Exception("Import failed/rejected as none of the sub assets was set as the 'main asset':" + ctx.assetPath); |
| 140 | + |
| 141 | + var result = new ImportResult |
| 142 | + { |
| 143 | + m_Assets = ctx.subAssets.Select(x => x.asset).ToArray(), |
| 144 | + m_AssetNames = ctx.subAssets.Select(x => x.identifier).ToArray(), |
| 145 | + m_Thumbnails = ctx.subAssets.Select(x => x.thumbnail).ToArray() |
| 146 | + }; |
| 147 | + |
| 148 | + return result; |
| 149 | + } |
| 150 | + |
| 151 | + public abstract void OnImportAsset(AssetImportContext ctx); |
| 152 | + |
| 153 | + [RequiredByNativeCode] |
| 154 | + internal static void RegisterScriptedImporters() |
| 155 | + { |
| 156 | + var importers = AttributeHelper.FindEditorClassesWithAttribute(typeof(ScriptedImporterAttribute)); |
| 157 | + foreach (var importer in importers) |
| 158 | + { |
| 159 | + var importerType = importer as Type; |
| 160 | + var attribute = Attribute.GetCustomAttribute(importerType, typeof(ScriptedImporterAttribute)) as ScriptedImporterAttribute; |
| 161 | + var handledExts = GetHandledExtensionsByImporter(attribute); |
| 162 | + |
| 163 | + // Prevent duplicates between importers! When duplicates found: all are rejected |
| 164 | + foreach (var imp in importers) |
| 165 | + { |
| 166 | + if (imp == importer) |
| 167 | + continue; |
| 168 | + |
| 169 | + var attribute2 = Attribute.GetCustomAttribute(imp as Type, typeof(ScriptedImporterAttribute)) as ScriptedImporterAttribute; |
| 170 | + var handledExts2 = GetHandledExtensionsByImporter(attribute2); |
| 171 | + |
| 172 | + // Remove intersection? |
| 173 | + foreach (var x1 in handledExts2) |
| 174 | + { |
| 175 | + if (handledExts.ContainsKey(x1.Key)) |
| 176 | + { |
| 177 | + // Log error message and remove from handledExts |
| 178 | + Debug.LogError(String.Format("Scripted importers {0} and {1} are targeting the {2} extension, rejecting both.", importerType.FullName, (imp as Type).FullName, x1.Key)); |
| 179 | + handledExts.Remove(x1.Key); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // Register the importer |
| 185 | + foreach (var ext in handledExts) |
| 186 | + AssetImporter.RegisterImporter(importerType, attribute.version, attribute.importQueuePriority, ext.Key); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + static SortedDictionary<string, bool> GetHandledExtensionsByImporter(ScriptedImporterAttribute attribute) |
| 191 | + { |
| 192 | + var handledExts = new SortedDictionary<string, bool>(); |
| 193 | + if (attribute.fileExtensions != null) |
| 194 | + { |
| 195 | + foreach (var ext in attribute.fileExtensions) |
| 196 | + { |
| 197 | + var cleanExt = ext; |
| 198 | + if (cleanExt.StartsWith(".")) |
| 199 | + cleanExt = cleanExt.Substring(1); |
| 200 | + |
| 201 | + handledExts.Add(cleanExt, true); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return handledExts; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // Class Concept: Class attribute that describes Scriptable importers and their static characteristics. |
| 210 | + [AttributeUsage(AttributeTargets.Class, Inherited = false)] |
| 211 | + public class ScriptedImporterAttribute : Attribute |
| 212 | + { |
| 213 | + public int version { get; private set; } |
| 214 | + |
| 215 | + // Gives control over when the asset is imported with regards to assets of other types. |
| 216 | + // Positive values delay the processing of source asset files while Negative values place them earlier in the import process. |
| 217 | + public int importQueuePriority { get; private set; } |
| 218 | + |
| 219 | + public string[] fileExtensions { get; private set; } |
| 220 | + |
| 221 | + public ScriptedImporterAttribute(int version, string[] exts) |
| 222 | + { |
| 223 | + Init(version, exts, 0); |
| 224 | + } |
| 225 | + |
| 226 | + public ScriptedImporterAttribute(int version, string ext) |
| 227 | + { |
| 228 | + Init(version, new[] { ext }, 0); |
| 229 | + } |
| 230 | + |
| 231 | + public ScriptedImporterAttribute(int version, string[] exts, int importQueueOffset) |
| 232 | + { |
| 233 | + Init(version, exts, importQueueOffset); |
| 234 | + } |
| 235 | + |
| 236 | + public ScriptedImporterAttribute(int version, string ext, int importQueueOffset) |
| 237 | + { |
| 238 | + Init(version, new[] { ext }, importQueueOffset); |
| 239 | + } |
| 240 | + |
| 241 | + private void Init(int version, string[] exts, int importQueueOffset) |
| 242 | + { |
| 243 | + if (exts == null || exts.Any(string.IsNullOrEmpty)) |
| 244 | + throw new ArgumentException("Must provide valid, none null, file extension strings."); |
| 245 | + |
| 246 | + this.version = version; |
| 247 | + this.importQueuePriority = importQueueOffset; |
| 248 | + fileExtensions = exts; |
| 249 | + } |
| 250 | + } |
| 251 | +} |
0 commit comments