Skip to content

Commit c8b8696

Browse files
author
Unity Technologies
committed
Unity 2017.1.0b2 C# reference source code
1 parent 2ecf4d3 commit c8b8696

File tree

177 files changed

+6681
-3316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+6681
-3316
lines changed

Editor/Mono/AssemblyHelper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ static public string[] FindAssembliesReferencedBy(string path, string[] foldersT
195195
return FindAssembliesReferencedBy(tmp, foldersToSearch, target);
196196
}
197197

198-
static bool IsTypeMonoBehaviourOrScriptableObject(AssemblyDefinition assembly, TypeReference type)
198+
private static bool IsTypeAUserExtendedScript(AssemblyDefinition assembly, TypeReference type)
199199
{
200200
if (type == null)
201201
return false;
@@ -219,10 +219,14 @@ static bool IsTypeMonoBehaviourOrScriptableObject(AssemblyDefinition assembly, T
219219
// includes generic arguments we can't use to get type from assembly.
220220
var typeName = type.IsGenericInstance ? (type.Namespace + "." + type.Name) : type.FullName;
221221
var engineType = builtinAssembly.GetType(typeName);
222+
223+
// TODO: this "list of classes" should get dynamically filled by the classes them self, thus removing dependency of this class on those classes.
222224
if (engineType == typeof(MonoBehaviour) || engineType.IsSubclassOf(typeof(MonoBehaviour)))
223225
return true;
224226
if (engineType == typeof(ScriptableObject) || engineType.IsSubclassOf(typeof(ScriptableObject)))
225227
return true;
228+
if (engineType == typeof(Experimental.AssetImporters.ScriptedImporter) || engineType.IsSubclassOf(typeof(Experimental.AssetImporters.ScriptedImporter)))
229+
return true;
226230
}
227231

228232
TypeDefinition typeDefinition = null;
@@ -236,12 +240,12 @@ static bool IsTypeMonoBehaviourOrScriptableObject(AssemblyDefinition assembly, T
236240
// failure should be handled better in other places.
237241
}
238242
if (typeDefinition != null)
239-
return IsTypeMonoBehaviourOrScriptableObject(assembly, typeDefinition.BaseType);
243+
return IsTypeAUserExtendedScript(assembly, typeDefinition.BaseType);
240244

241245
return false;
242246
}
243247

244-
static public void ExtractAllClassesThatInheritMonoBehaviourAndScriptableObject(string path, out string[] classNamesArray, out string[] classNameSpacesArray)
248+
public static void ExtractAllClassesThatAreUserExtendedScripts(string path, out string[] classNamesArray, out string[] classNameSpacesArray)
245249
{
246250
List<string> classNames = new List<string>();
247251
List<string> nameSpaces = new List<string>();
@@ -262,7 +266,7 @@ static public void ExtractAllClassesThatInheritMonoBehaviourAndScriptableObject(
262266

263267
try
264268
{
265-
if (IsTypeMonoBehaviourOrScriptableObject(assembly, baseType))
269+
if (IsTypeAUserExtendedScript(assembly, baseType))
266270
{
267271
classNames.Add(type.Name);
268272
nameSpaces.Add(type.Namespace);
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
}

Editor/Mono/AssetStore/AssetStorePreviewManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public Color color
6969
int m_CacheRemove = 0;
7070
int m_ConvertedThisTick = 0;
7171
CachedAssetStoreImage m_DummyItem = new CachedAssetStoreImage();
72-
private PreviewRenderUtility m_PreviewUtility;
7372

7473
static bool s_NeedsRepaint = false;
7574

Editor/Mono/AttributeHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ static MonoCreateAssetItem[] ExtractCreateAssetMenuItems(Assembly assembly)
153153
return result.ToArray();
154154
}
155155

156+
internal static ArrayList FindEditorClassesWithAttribute(Type attribType)
157+
{
158+
var attributedTypes = new ArrayList();
159+
foreach (var loadedType in EditorAssemblies.loadedTypes)
160+
{
161+
if (loadedType.GetCustomAttributes(attribType, false).Length != 0)
162+
attributedTypes.Add(loadedType);
163+
}
164+
return attributedTypes;
165+
}
166+
156167
static internal object InvokeMemberIfAvailable(object target, string methodName, object[] args)
157168
{
158169
MethodInfo method = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

Editor/Mono/BuildPipeline/BuildPlatform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal BuildPlatforms()
107107
buildPlatformsList.Add(new BuildPlatform("PS Vita", "BuildSettings.PSP2", BuildTargetGroup.PSP2, true));
108108
buildPlatformsList.Add(new BuildPlatform("PS4", "BuildSettings.PS4", BuildTargetGroup.PS4, true));
109109
buildPlatformsList.Add(new BuildPlatform("Wii U", "BuildSettings.WiiU", BuildTargetGroup.WiiU, false));
110-
buildPlatformsList.Add(new BuildPlatform("Windows Store", "BuildSettings.Metro", BuildTargetGroup.WSA, true));
110+
buildPlatformsList.Add(new BuildPlatform("Universal Windows Platform", "BuildSettings.Metro", BuildTargetGroup.WSA, true));
111111
buildPlatformsList.Add(new BuildPlatform("WebGL", "BuildSettings.WebGL", BuildTargetGroup.WebGL, true));
112112
buildPlatformsList.Add(new BuildPlatform("Samsung TV", "BuildSettings.SamsungTV", BuildTargetGroup.SamsungTV, true));
113113
buildPlatformsList.Add(new BuildPlatform("Nintendo 3DS", "BuildSettings.N3DS", BuildTargetGroup.N3DS, false));

0 commit comments

Comments
 (0)