forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityType.cs
More file actions
118 lines (99 loc) · 4.21 KB
/
UnityType.cs
File metadata and controls
118 lines (99 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace UnityEditor
{
// NOTE : Corresponds to the native TypeFlags
[Flags]
enum UnityTypeFlags
{
Abstract = 1 << 0,
Sealed = 1 << 1,
EditorOnly = 1 << 2
}
sealed partial class UnityType
{
public string name { get; private set; }
public string nativeNamespace { get; private set; }
public int persistentTypeID { get; private set; }
public UnityType baseClass { get; private set; }
public UnityTypeFlags flags { get; private set; }
public bool isAbstract { get { return (flags & UnityTypeFlags.Abstract) != 0; } }
public bool isSealed { get { return (flags & UnityTypeFlags.Sealed) != 0; } }
public bool isEditorOnly { get { return (flags & UnityTypeFlags.EditorOnly) != 0; } }
uint runtimeTypeIndex;
uint descendantCount;
public string qualifiedName
{
get { return hasNativeNamespace ? nativeNamespace + "::" + name : name; }
}
// NOTE : nativeNamespace == "" for types with no namespace so added this helper for convenience
// in case the caller wasn't sure whether to compare nativeNamespace with null or empty
public bool hasNativeNamespace
{
get { return nativeNamespace.Length > 0; }
}
public bool IsDerivedFrom(UnityType baseClass)
{
// NOTE : Type indices are ordered so all derived classes are immediately following the
// base class allowing us to test inheritance with only a range check
return (runtimeTypeIndex - baseClass.runtimeTypeIndex) < baseClass.descendantCount;
}
public static UnityType FindTypeByPersistentTypeID(int id)
{
UnityType result = null;
ms_idToTypeInfo.TryGetValue(id, out result);
return result;
}
public static UnityType FindTypeByName(string name)
{
UnityType result = null;
ms_nameToTypeInfo.TryGetValue(name, out result);
return result;
}
public static UnityType FindTypeByNameCaseInsensitive(string name)
{
return ms_types.FirstOrDefault(t => string.Equals(name, t.name, StringComparison.OrdinalIgnoreCase));
}
public static ReadOnlyCollection<UnityType> GetTypes()
{
return ms_typesReadOnly;
}
static UnityType()
{
var types = UnityType.Internal_GetAllTypes();
ms_types = new UnityType[types.Length];
ms_idToTypeInfo = new Dictionary<int, UnityType>();
ms_nameToTypeInfo = new Dictionary<string, UnityType>();
for (int i = 0; i < types.Length; ++i)
{
// Types are sorted so base < derived and null baseclass is passed from native as 0xffffffff
UnityType baseClass = null;
if (types[i].baseClassIndex < types.Length)
baseClass = ms_types[types[i].baseClassIndex];
var newType = new UnityType
{
runtimeTypeIndex = types[i].runtimeTypeIndex,
descendantCount = types[i].descendantCount,
name = types[i].className,
nativeNamespace = types[i].classNamespace,
persistentTypeID = types[i].persistentTypeID,
baseClass = baseClass,
flags = (UnityTypeFlags)types[i].flags
};
ms_types[i] = newType;
ms_typesReadOnly = new ReadOnlyCollection<UnityType>(ms_types);
ms_idToTypeInfo[newType.persistentTypeID] = newType;
ms_nameToTypeInfo[newType.name] = newType;
}
}
static UnityType[] ms_types;
static ReadOnlyCollection<UnityType> ms_typesReadOnly;
static Dictionary<int, UnityType> ms_idToTypeInfo;
static Dictionary<string, UnityType> ms_nameToTypeInfo;
}
}