Skip to content

Commit 074afc5

Browse files
author
Artem Perepelytsia
committed
Merge branch 'ext-events-improvements'
2 parents e6f7a38 + e5a6439 commit 074afc5

File tree

6 files changed

+73
-28
lines changed

6 files changed

+73
-28
lines changed

Editor/Drawers/TypeFieldDrawer.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,22 @@ private void DrawFieldContent(int controlID)
116116
{
117117
int indexOfComma = _serializedTypeRef.TypeNameAndAssembly.IndexOf(',');
118118
string fullTypeName = indexOfComma == -1 ? string.Empty : _serializedTypeRef.TypeNameAndAssembly.Substring(0, indexOfComma);
119-
GUIContent fieldContent = GUIContentHelper.Temp(GetTypeToShow(fullTypeName));
119+
GUIContent fieldContent = GUIContentHelper.Temp(GetTypeToShow(fullTypeName, out bool typeExists));
120+
121+
var previousColor = GUI.backgroundColor;
122+
123+
if (!typeExists)
124+
GUI.backgroundColor = new Color(1f, 0f, 0f, .5f);
125+
120126
EditorStyles.popup.Draw(_position, fieldContent, controlID);
127+
128+
GUI.backgroundColor = previousColor;
121129
}
122130

123-
private string GetTypeToShow(string typeName)
131+
private string GetTypeToShow(string typeName, out bool typeExists)
124132
{
133+
typeExists = true;
134+
125135
if (ProjectSettings.UseBuiltInNames)
126136
{
127137
string builtInName = typeName.ReplaceWithBuiltInName();
@@ -136,7 +146,10 @@ private string GetTypeToShow(string typeName)
136146
return DropdownWindow.NoneElementName;
137147

138148
if (TypeCache.GetType(_serializedTypeRef.TypeNameAndAssembly) == null)
149+
{
150+
typeExists = false;
139151
return typeName + MissingSuffix;
152+
}
140153

141154
return typeName;
142155
}

Editor/Util/SerializedTypeReference.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ internal readonly struct SerializedTypeReference
1414
private readonly SerializedObject _parentObject;
1515
private readonly SerializedProperty _guidProperty;
1616
private readonly SerializedProperty _guidAssignmentFailedProperty;
17+
private readonly SerializedProperty _suppressLogs;
1718

1819
public SerializedTypeReference(SerializedProperty typeReferenceProperty)
1920
{
2021
_parentObject = typeReferenceProperty.serializedObject;
2122
TypeNameProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference._typeNameAndAssembly));
2223
_guidProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GUID));
2324
_guidAssignmentFailedProperty = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference.GuidAssignmentFailed));
25+
_suppressLogs = typeReferenceProperty.FindPropertyRelative(nameof(TypeReference._suppressLogs));
2426

2527
FindGuidIfAssignmentFailed();
2628
}
@@ -31,17 +33,39 @@ public string TypeNameAndAssembly
3133
set => SetTypeNameAndAssembly(value);
3234
}
3335

36+
public bool SuppressLogs
37+
{
38+
get => _suppressLogs.boolValue;
39+
set
40+
{
41+
_suppressLogs.boolValue = value;
42+
_parentObject.ApplyModifiedProperties();
43+
}
44+
}
45+
3446
public bool TypeNameHasMultipleDifferentValues => TypeNameProperty.hasMultipleDifferentValues;
3547

3648
private bool GuidAssignmentFailed
3749
{
3850
get => _guidAssignmentFailedProperty.boolValue;
3951
// Used in C# 8
40-
[UsedImplicitly] set => SetGUIDAssignmentFailed(value);
52+
[UsedImplicitly]
53+
set
54+
{
55+
_guidAssignmentFailedProperty.boolValue = value;
56+
_parentObject.ApplyModifiedProperties();
57+
}
4158
}
4259

4360
// Used in C# 8
44-
[UsedImplicitly] private string GUID { set => SetGUID(value); }
61+
[UsedImplicitly] private string GUID
62+
{
63+
set
64+
{
65+
_guidProperty.stringValue = value;
66+
_parentObject.ApplyModifiedProperties();
67+
}
68+
}
4569

4670
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global",
4771
Justification = "The method is used by TypeFieldDrawer in C# 7")]
@@ -59,18 +83,6 @@ public void SetType(Type type)
5983
_parentObject.ApplyModifiedProperties();
6084
}
6185

62-
private void SetGUIDAssignmentFailed(bool value)
63-
{
64-
_guidAssignmentFailedProperty.boolValue = value;
65-
_parentObject.ApplyModifiedProperties();
66-
}
67-
68-
private void SetGUID(string value)
69-
{
70-
_guidProperty.stringValue = value;
71-
_parentObject.ApplyModifiedProperties();
72-
}
73-
7486
private static string GetClassGuidFromTypeName(string typeName)
7587
{
7688
var type = Type.GetType(typeName);

Runtime/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
[assembly: InternalsVisibleTo("TypeReferences.Editor.Tests")]
55
[assembly: InternalsVisibleTo("GenericUnityObjects")]
66
[assembly: InternalsVisibleTo("GenericUnityObjects.Editor")]
7+
[assembly: InternalsVisibleTo("ExtEvents")]
78
[assembly: InternalsVisibleTo("ExtEvents.Editor")]

Runtime/TypeReference.Editor.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
namespace TypeReferences
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
46
using System.Linq;
57
using Debug = UnityEngine.Debug;
68

79
#if UNITY_EDITOR
810
using SolidUtilities.Editor;
911
using UnityEditor;
12+
using UnityEditor.SceneManagement;
13+
using UnityEngine;
14+
using UnityEngine.SceneManagement;
1015
#endif
1116

1217
// This part of the class contains only the methods that are meant to be executed in Editor and not in builds.
@@ -31,7 +36,7 @@ private void UnsubscribeFromDelayCall()
3136
private static string GetGUIDFromType(Type type)
3237
{
3338
#if UNITY_EDITOR
34-
return AssetSearcher.GetClassGUID(type);
39+
return AssetHelper.GetClassGUID(type);
3540
#else
3641
return string.Empty;
3742
#endif
@@ -71,18 +76,33 @@ private void TryUpdatingTypeUsingGUID()
7176
#endif
7277
}
7378

74-
private void ReportObjectsWithMissingValue()
79+
[Conditional("UNITY_EDITOR")]
80+
private static void ReportObjectsWithMissingValue(string typeName)
7581
{
7682
#if UNITY_EDITOR
77-
var foundObjects = AssetSearcher.FindObjectsWithValue(nameof(_typeNameAndAssembly), _typeNameAndAssembly);
78-
Debug.Log("The value is set in the following objects:");
83+
bool firstLineLogged = false;
7984

80-
foreach (FoundObject foundObject in foundObjects)
85+
var serializedObjects = ProjectDependencySearcher.GetSerializedObjectsFromOpenScenes(new ProjectDependencySearcher.FoundObjects());
86+
var typeReferenceProperties = SerializedPropertyHelper.FindPropertiesOfType(serializedObjects, "TypeReference");
87+
88+
foreach (var typeReferenceProperty in typeReferenceProperties)
8189
{
82-
var details = foundObject
83-
.Select(detail => $"{detail.Key}: {detail.Value}");
90+
if (typeReferenceProperty.FindPropertyRelative(nameof(_typeNameAndAssembly)).stringValue != typeName
91+
|| typeReferenceProperty.FindPropertyRelative(nameof(_suppressLogs)).boolValue) // also don't report the missing type if the logs were suppressed for this TypeReference instance.
92+
{
93+
continue;
94+
}
95+
96+
var targetObject = typeReferenceProperty.serializedObject.targetObject;
8497

85-
Debug.Log($"[{foundObject.Type}] {string.Join(", ", details)}");
98+
// Log the first message only if any objects with missing value were found.
99+
if (!firstLineLogged)
100+
{
101+
Debug.LogWarning($"'{typeName}' was referenced but such type was not found. The value was set in the following objects:");
102+
firstLineLogged = true;
103+
}
104+
105+
Debug.Log($"<a>{targetObject.name}{(targetObject is MonoBehaviour ? $".{targetObject.GetType().Name}" : string.Empty)}</a>");
86106
}
87107
#endif
88108
}

Runtime/TypeReference.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class TypeReference : ISerializationCallbackReceiver
2222
[SerializeField] internal string _typeNameAndAssembly;
2323
public string TypeNameAndAssembly => _typeNameAndAssembly;
2424

25-
[SerializeField] private bool _suppressLogs;
25+
[SerializeField] internal bool _suppressLogs;
2626

2727
private Type _type;
2828

@@ -182,8 +182,7 @@ private void LogTypeNotFound()
182182
if (_reportedMissingValues.Contains(_typeNameAndAssembly))
183183
return;
184184

185-
Debug.LogWarning($"'{_typeNameAndAssembly}' was referenced but such type was not found.");
186-
ReportObjectsWithMissingValue();
185+
ReportObjectsWithMissingValue(_typeNameAndAssembly);
187186
_reportedMissingValues.Add(_typeNameAndAssembly);
188187
}
189188

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "Type References",
55
"description": "A plugin that allows selecting a type from a drop-down menu in the inspector.",
66
"dependencies": {
7-
"com.solidalloy.util": "1.35.0",
7+
"com.solidalloy.util": "1.37.0",
88
"com.unity.settings-manager": "1.0.3",
99
"com.solidalloy.unity-dropdown": "1.1.1"
1010
},

0 commit comments

Comments
 (0)