Skip to content

Commit f9f7224

Browse files
author
Unity Technologies
committed
Unity 2023.1.0a19 C# reference source code
1 parent 70832e6 commit f9f7224

File tree

205 files changed

+9168
-2689
lines changed

Some content is hidden

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

205 files changed

+9168
-2689
lines changed

Editor/Mono/CustomEditorAttributes.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal partial class CustomEditorAttributes
4040

4141
CustomEditorAttributes()
4242
{
43-
Rebuild();
43+
Initialize();
4444
}
4545

4646
Type GetCustomEditorType(Type type, bool multiEdit)
@@ -148,7 +148,12 @@ static bool IsAppropriateEditor(MonoEditorType editor, bool isChildClass, bool i
148148
return isFallbackPass == editor.isFallback;
149149
}
150150

151-
void Rebuild()
151+
internal static void Rebuild()
152+
{
153+
instance.Initialize();
154+
}
155+
156+
void Initialize()
152157
{
153158
m_Cache.Clear();
154159
var types = TypeCache.GetTypesWithAttribute<CustomEditor>();

Editor/Mono/EditorGUI.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,14 @@ internal static GenericMenu FillPropertyContextMenu(SerializedProperty property,
29922992

29932993
Object targetObject = property.serializedObject.targetObject;
29942994

2995+
PropertyValueOriginInfo propertyOrigin = PrefabUtility.GetPropertyValueOriginInfo(property);
2996+
2997+
if (propertyOrigin.asset != null)
2998+
{
2999+
pm.AddItem(new GUIContent("Go to " + propertyOrigin.contextMenuText + " in '" + propertyOrigin.asset.name + "'"), false,
3000+
() => GoToPrefab(AssetDatabase.GetAssetPath(propertyOrigin.asset), PrefabUtility.GetGameObject(targetObject)));
3001+
}
3002+
29953003
bool shouldDisplayPrefabContextMenuItems = property.prefabOverride || (linkedProperty?.prefabOverride ?? false);
29963004

29973005
// Only display the custom apply/revert menu for GameObjects/Components that are not part of a Prefab instance & variant.
@@ -3261,6 +3269,16 @@ internal static GenericMenu FillPropertyContextMenu(SerializedProperty property,
32613269
return pm;
32623270
}
32633271

3272+
internal static void GoToPrefab(string assetPath, GameObject openedFromInstance)
3273+
{
3274+
// When this function is called from a Property Context Menu on the Overrides pop-up window, we need to make ensure that the correct GameObject
3275+
// (i.e. the GameObject that the property belongs to) is selected when opening Prefab Mode by explicitly setting it as the active GameObject.
3276+
if (EditorGUIUtility.comparisonViewMode != EditorGUIUtility.ComparisonViewMode.None)
3277+
Selection.activeGameObject = openedFromInstance;
3278+
3279+
PrefabStageUtility.OpenPrefab(assetPath, openedFromInstance, PrefabStage.Mode.InIsolation);
3280+
}
3281+
32643282
internal static void DoPropertyContextMenu(SerializedProperty property, SerializedProperty linkedProperty = null, GenericMenu menu = null)
32653283
{
32663284
GenericMenu pm = FillPropertyContextMenu(property, linkedProperty, menu);

Editor/Mono/EditorUtility.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,27 @@ internal static void DisplayObjectContextMenu(Rect position, Object[] context, i
580580
);
581581
}
582582
}
583+
584+
bool isComponentAddedInNestedPrefab = false;
585+
586+
Object source = PrefabUtility.GetCorrespondingObjectFromSource(targetObject);
587+
while (source)
588+
{
589+
if (PrefabUtility.IsAddedComponentOverride(source))
590+
{
591+
isComponentAddedInNestedPrefab = true;
592+
break;
593+
}
594+
595+
source = PrefabUtility.GetCorrespondingObjectFromSource(source);
596+
}
597+
598+
if (isComponentAddedInNestedPrefab)
599+
{
600+
pm.AddItem(new GUIContent("Go to Added Component in '" + PrefabUtility.GetPrefabAssetRootGameObject(source).name + "'"), false,
601+
() => PrefabStageUtility.OpenPrefab(AssetDatabase.GetAssetPath(source), PrefabUtility.GetGameObject(targetObject), PrefabStage.Mode.InIsolation));
602+
}
603+
583604
}
584605
else if (context != null && context.Length == 1 && context[0] is Material)
585606
{

Editor/Mono/GUI/Tools/EditorToolAttributes.cs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,49 @@ namespace UnityEditor.EditorTools
88
{
99
public abstract class ToolAttribute : Attribute
1010
{
11+
public const int defaultPriority = 1000;
12+
1113
string m_DisplayName;
1214
Type m_TargetContext, m_TargetType;
15+
Type m_VariantGroup;
16+
int m_ToolPriority = defaultPriority;
17+
int m_VariantPriority = defaultPriority;
18+
19+
public string displayName
20+
{
21+
get => m_DisplayName;
22+
set => m_DisplayName = value;
23+
}
24+
25+
public Type targetType
26+
{
27+
get => m_TargetType;
28+
set => m_TargetType = value;
29+
}
30+
31+
public Type targetContext
32+
{
33+
get => m_TargetContext;
34+
set => m_TargetContext = value;
35+
}
36+
37+
public int toolPriority
38+
{
39+
get => m_ToolPriority;
40+
set => m_ToolPriority = value;
41+
}
1342

14-
public string displayName => m_DisplayName;
15-
public Type targetType => m_TargetType;
16-
public Type targetContext => m_TargetContext;
43+
public Type variantGroup
44+
{
45+
get => m_VariantGroup;
46+
set => m_VariantGroup = value;
47+
}
48+
49+
public int variantPriority
50+
{
51+
get => m_VariantPriority;
52+
set => m_VariantPriority = value;
53+
}
1754

1855
ToolAttribute() {}
1956

@@ -25,6 +62,8 @@ protected ToolAttribute(string displayName, Type targetType = null, Type editorT
2562
throw new ArgumentException($"{GetType().Name} ({displayName}): editorToolContext type argument must " +
2663
$"be of type EditorToolContext", "editorToolContext");
2764
m_TargetContext = editorToolContext;
65+
m_VariantGroup = variantGroup;
66+
m_ToolPriority = toolPriority;
2867
}
2968
}
3069

@@ -33,8 +72,21 @@ public sealed class EditorToolAttribute : ToolAttribute
3372
{
3473
public EditorToolAttribute(string displayName, Type componentToolTarget = null)
3574
: base(displayName, componentToolTarget) {}
75+
3676
public EditorToolAttribute(string displayName, Type componentToolTarget, Type editorToolContext)
3777
: base(displayName, componentToolTarget, editorToolContext) {}
78+
79+
public EditorToolAttribute(
80+
string displayName,
81+
Type componentToolTarget,
82+
Type editorToolContext,
83+
int toolPriority,
84+
Type variantGroup)
85+
: base(displayName, componentToolTarget, editorToolContext)
86+
{
87+
this.toolPriority = toolPriority;
88+
this.variantGroup = variantGroup;
89+
}
3890
}
3991

4092
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]

Editor/Mono/GUI/Tools/EditorToolCache.cs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,41 @@
1010

1111
namespace UnityEditor.EditorTools
1212
{
13+
// Placeholder type for global editors to register as keys
14+
struct NullTargetKey {}
15+
1316
[Serializable]
1417
struct EditorTypeAssociation : ISerializationCallbackReceiver
1518
{
1619
[SerializeField]
17-
string m_TargetContext, m_TargetBehaviour, m_EditorType;
20+
string m_TargetContext, m_TargetBehaviour, m_EditorType, m_VariantGroup;
1821

1922
// Context and behaviour types can be null, and should be treated as universally applicable.
2023
public Type targetContext { get; private set; }
2124
public Type targetBehaviour { get; private set; }
2225
public Type editor { get; private set; }
26+
public Type variantGroup { get; private set; }
27+
public int priority { get; private set; }
28+
public int variantPriority { get; private set; }
2329

24-
public EditorTypeAssociation(Type editor, Type targetBehaviour, Type targetContext)
30+
public EditorTypeAssociation(Type editor, Type attributeType)
2531
{
26-
this.targetContext = targetContext;
27-
this.targetBehaviour = targetBehaviour;
2832
this.editor = editor;
29-
m_EditorType = m_TargetBehaviour = m_TargetContext = null;
33+
var attrib = editor.GetCustomAttributes(attributeType, false).FirstOrDefault() as ToolAttribute;
34+
targetBehaviour = attrib?.targetType ?? typeof(NullTargetKey);
35+
targetContext = attrib?.targetContext;
36+
variantGroup = attrib?.variantGroup;
37+
priority = attrib?.toolPriority ?? ToolAttribute.defaultPriority;
38+
variantPriority = attrib?.variantPriority ?? ToolAttribute.defaultPriority;
39+
m_TargetContext = m_TargetBehaviour = m_EditorType = m_VariantGroup = null;
3040
}
3141

3242
public void OnBeforeSerialize()
3343
{
3444
m_TargetContext = targetContext?.AssemblyQualifiedName;
3545
m_TargetBehaviour = targetBehaviour?.AssemblyQualifiedName;
3646
m_EditorType = editor?.AssemblyQualifiedName;
47+
m_VariantGroup = variantGroup?.AssemblyQualifiedName;
3748
}
3849

3950
public void OnAfterDeserialize()
@@ -44,7 +55,8 @@ public void OnAfterDeserialize()
4455
targetBehaviour = Type.GetType(m_TargetBehaviour);
4556
if (!string.IsNullOrEmpty(m_EditorType))
4657
editor = Type.GetType(m_EditorType);
47-
m_TargetContext = m_TargetBehaviour = m_EditorType = null;
58+
if (!string.IsNullOrEmpty(m_VariantGroup))
59+
variantGroup = Type.GetType(m_VariantGroup);
4860
}
4961
}
5062

@@ -152,11 +164,12 @@ public UnityObject InstantiateEditor()
152164
// target types are treated as "global" editors.
153165
class EditorToolCache
154166
{
155-
// Placeholder type for global editors to register as keys
156-
struct NullTargetKey {}
157167

158168
Type m_AttributeType;
169+
// Cache of the available tools as defined by EditorToolAttribute
159170
EditorTypeAssociation[] s_AvailableEditorTypeAssociations = null;
171+
// Type association data for all loaded tools, regardless of whether they are registered with an EditorToolAttribute.
172+
Dictionary<Type, EditorTypeAssociation> m_ToolMetaData = new Dictionary<Type, EditorTypeAssociation>();
160173
Dictionary<Type, List<EditorTypeAssociation>> s_EditorTargetCache = new Dictionary<Type, List<EditorTypeAssociation>>();
161174

162175
// Static fields in generic classes result in multiple static field instances. In this case that's fine.
@@ -195,22 +208,21 @@ EditorTypeAssociation[] availableEditorTypeAssociations
195208
s_AvailableEditorTypeAssociations = new EditorTypeAssociation[len];
196209

197210
for (int i = 0; i < len; i++)
198-
{
199-
var customToolAttribute = editorTools[i].GetCustomAttributes(m_AttributeType, false)
200-
.FirstOrDefault() as ToolAttribute;
201-
202-
if (customToolAttribute == null)
203-
continue;
211+
s_AvailableEditorTypeAssociations[i] = new EditorTypeAssociation(editorTools[i], m_AttributeType);
204212

205-
s_AvailableEditorTypeAssociations[i] = new EditorTypeAssociation(
206-
editorTools[i], customToolAttribute.targetType ?? typeof(NullTargetKey), customToolAttribute.targetContext);
207-
}
208213
}
209214

210215
return s_AvailableEditorTypeAssociations;
211216
}
212217
}
213218

219+
public EditorTypeAssociation GetMetaData(Type toolType)
220+
{
221+
if (!m_ToolMetaData.TryGetValue(toolType, out var data))
222+
m_ToolMetaData.Add(toolType, data = new EditorTypeAssociation(toolType, m_AttributeType));
223+
return data;
224+
}
225+
214226
public Type GetTargetType(Type editorType)
215227
{
216228
for (int i = 0, c = availableEditorTypeAssociations.Length; i < c; i++)
@@ -305,5 +317,16 @@ public void InstantiateEditors(EditorToolContext ctx, List<ComponentEditor> edit
305317
foreach (var editor in editors)
306318
editor.InstantiateEditor();
307319
}
320+
321+
public List<EditorTypeAssociation> GetEditorsForVariant(EditorTypeAssociation type)
322+
{
323+
var tools = new List<EditorTypeAssociation>();
324+
foreach(var association in availableEditorTypeAssociations)
325+
if (association.variantGroup == type.variantGroup
326+
&& association.targetBehaviour == type.targetBehaviour
327+
&& association.targetContext == type.targetContext)
328+
tools.Add(association);
329+
return tools;
330+
}
308331
}
309332
}

0 commit comments

Comments
 (0)