Skip to content

Commit

Permalink
Refactor some syntax.
Browse files Browse the repository at this point in the history
Disable updating when component is foldout.
Attempt to refresh when the play mode is changed.
  • Loading branch information
JLChnToZ committed Sep 20, 2021
1 parent 5156ee7 commit ec8db36
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 38 deletions.
6 changes: 3 additions & 3 deletions UInspectorPlus/ComponentMethodDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ComponentMethodDrawer: IReflectorDrawer, IDisposable {
public GenericMenu.MenuFunction OnClose;

public bool ShouldDrawHeader {
get { return drawHeader; }
get => drawHeader;
set {
drawHeader = value;
paramsFolded &= value;
Expand All @@ -47,15 +47,15 @@ public bool ShouldDrawHeader {
public object Value => result?.Value;

public bool AllowPrivateFields {
get { return privateFields; }
get => privateFields;
set {
privateFields = value;
InitComponentMethods(false);
}
}

public bool AllowObsolete {
get { return obsolete; }
get => obsolete;
set {
obsolete = value;
InitComponentMethods(false);
Expand Down
3 changes: 3 additions & 0 deletions UInspectorPlus/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ private static MethodInfo FindMethod(Type fromType, string methodName, Type dele
);
}

// Special checker to deal with "null" UnityEngine.Object (Internally null, but still exists in Mono heap)
internal static bool IsInvalid(object obj) => obj is UnityObject uObj ? uObj == null : obj == null;

public static IEnumerable<Type> LooseGetTypes(Assembly assembly) {
for(int retries = 0; retries < 2; retries++)
try {
Expand Down
2 changes: 1 addition & 1 deletion UInspectorPlus/HexEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace UInspectorPlus {
internal class HexEdit: InspectorDrawer {
[SerializeField] private Vector2 scrollPos;
public byte[] Data { get => target as byte[]; }
public byte[] Data => target as byte[];
public int columns = 16;
private GUIContent temp = new GUIContent();

Expand Down
4 changes: 1 addition & 3 deletions UInspectorPlus/InspectorChildWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ private void OnGUI() {
GUILayout.EndScrollView();
}

private void OnDestroy() {
drawer.Dispose();
}
private void OnDestroy() => drawer.Dispose();

private void OnInspectorUpdate() {
if(EditorGUIUtility.editingTextField)
Expand Down
18 changes: 8 additions & 10 deletions UInspectorPlus/InspectorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static InspectorDrawer GetDrawer(object target, Type targetType, bool sho
public InspectorDrawer(object target, Type targetType, bool shown, bool showProps, bool showPrivateFields, bool showObsolete, bool showMethods) {
this.target = target;
BindingFlags flag = BindingFlags.Static | BindingFlags.Public;
if(target != null)
if(!Helper.IsInvalid(target))
flag |= BindingFlags.Instance;
if(allowPrivate = showPrivateFields)
flag |= BindingFlags.NonPublic;
Expand Down Expand Up @@ -104,7 +104,7 @@ public InspectorDrawer(object target, Type targetType, bool shown, bool showProp
AddMethodMenu();
foreach(var d in drawer)
d.OnRequireRedraw += RequireRedraw;
if(target != null)
if(!Helper.IsInvalid(target))
this.shown = Helper.GetState(target, shown);
}

Expand All @@ -120,7 +120,7 @@ private void AddMethodMenu() {
public void Draw(bool drawHeader = true, bool readOnly = false) {
if(drawHeader) {
shown = EditorGUILayout.InspectorTitlebar(shown, target as UnityObject);
if(target != null)
if(!Helper.IsInvalid(target))
Helper.StoreState(target, shown);
if(!shown)
return;
Expand All @@ -142,9 +142,7 @@ public void Draw(bool drawHeader = true, bool readOnly = false) {

protected virtual void Draw(bool readOnly) {
foreach(var item in drawer) {
var methodDrawer = item as ComponentMethodDrawer;
var fieldDrawer = item as MethodPropertyDrawer;
if(methodDrawer != null) {
if(item is ComponentMethodDrawer methodDrawer) {
EditorGUI.indentLevel--;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(GUIContent.none, GUILayout.Width(EditorGUIUtility.singleLineHeight));
Expand All @@ -157,7 +155,7 @@ protected virtual void Draw(bool readOnly) {
} else if(item != null) {
if(item.Info != null && !string.IsNullOrEmpty(searchText) && item.Info.Name.IndexOf(searchText, StringComparison.CurrentCultureIgnoreCase) < 0)
continue;
if(fieldDrawer != null)
if(item is MethodPropertyDrawer fieldDrawer)
fieldDrawer.Draw(readOnly);
else
item.Draw();
Expand All @@ -184,8 +182,8 @@ private void DrawRequestRefs() {
if(removal != null) MethodPropertyDrawer.drawerRequestingReferences.Remove(removal);
}

public void UpdateValues(bool updateProps) {
if(target == null) return;
public virtual void UpdateValues(bool updateProps) {
if(Helper.IsInvalid(target)) return;
foreach(var drawerItem in drawer) {
var propDrawer = drawerItem as MethodPropertyDrawer;
if(propDrawer == null)
Expand All @@ -205,7 +203,7 @@ public virtual void Dispose() {
}

protected void RequireRedraw() {
if(target != null && OnRequireRedraw != null)
if(!Helper.IsInvalid(target) && OnRequireRedraw != null)
OnRequireRedraw();
}
}
Expand Down
41 changes: 26 additions & 15 deletions UInspectorPlus/InspectorPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ private void OnEnable() {
typeMatcher.OnRequestRedraw += Repaint;
}

private void OnDisable() {
typeMatcher.OnRequestRedraw -= Repaint;
}
private void OnDisable() => typeMatcher.OnRequestRedraw -= Repaint;

private void OnDestroy() {
typeMatcher.Dispose();
}
private void OnDestroy() => typeMatcher.Dispose();

private void Initialize() {
if(initialized) return;
Expand Down Expand Up @@ -187,13 +183,15 @@ private void UpdateSearchMode() {
EditorPrefs.SetInt("inspectorplus_searchmode", searchMode);
}

private void OnSelectionChange() {
private void OnSelectionChange() => CheckSelection();

private void CheckSelection(bool callFromUpdate = false) {
if(!locked)
instanceIds = Selection.instanceIDs;
var pendingRemoveDrawers = new List<InspectorDrawer[]>();
var pendingAddDrawers = new List<InspectorDrawer[]>();
foreach(var drawer in drawers)
if(drawer.Length <= 0 || drawer[0].target == null || !instanceIds.Contains(Helper.ObjIdOrHashCode(drawer[0].target))) {
if(drawer.Length <= 0 || Helper.IsInvalid(drawer[0].target) || !instanceIds.Contains(Helper.ObjIdOrHashCode(drawer[0].target))) {
pendingRemoveDrawers.Add(drawer);
foreach(var d in drawer) d.Dispose();
}
Expand All @@ -202,12 +200,13 @@ private void OnSelectionChange() {
if(drawers.FindIndex(drawer => Helper.ObjIdOrHashCode(drawer[0].target) == instanceID) < 0)
pendingAddDrawers.Add(CreateDrawers(instanceID));
drawers.AddRange(pendingAddDrawers);
UpdateValues();
if(!callFromUpdate)
UpdateValues();
}

private InspectorDrawer[] CreateDrawers(int instanceID) {
var target = EditorUtility.InstanceIDToObject(instanceID);
if(target == null)
if(Helper.IsInvalid(target))
return new InspectorDrawer[0];
var ret = new List<InspectorDrawer>();
try {
Expand Down Expand Up @@ -247,13 +246,25 @@ private void IterateDrawers<T>(Action<T> each) where T : IReflectorDrawer {
each(methodDrawer);
}

private void UpdateValues() {
UpdateValues(forceUpdateProps || EditorApplication.isPlaying);
}
private void UpdateValues() => UpdateValues(forceUpdateProps || EditorApplication.isPlaying);

private void UpdateValues(bool updateProps) {
foreach(var drawerGroup in drawers.SelectMany(drawer => drawer))
drawerGroup.UpdateValues(updateProps);
for(int i = 0; i < drawers.Count; i++) {
var drawerGroup = drawers[i];
if(drawerGroup.Length == 0 || Helper.IsInvalid(drawerGroup[0].target)) {
CheckSelection(true);
break;
}
int instanceID = Helper.ObjIdOrHashCode(drawerGroup[0].target);
for(int j = 0; j < drawerGroup.Length; j++) {
if(Helper.IsInvalid(drawerGroup[j].target)) {
drawers[i] = CreateDrawers(instanceID);
break;
}
if(drawerGroup[j].shown)
drawerGroup[j].UpdateValues(updateProps);
}
}
Repaint();
}

Expand Down
2 changes: 1 addition & 1 deletion UInspectorPlus/ListInspectorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override void Draw(bool readOnly) {
(target as IList)[i] = arrayContentDrawer[i].Value;
},
drawHeaderCallback = r => GUI.Label(r, target.ToString(), EditorStyles.miniBoldLabel),
onCanAddCallback = l => target != null && !(target as IList).IsFixedSize,
onCanAddCallback = l => !Helper.IsInvalid(target) && !(target as IList).IsFixedSize,
onAddCallback = l => {
ReorderableList.defaultBehaviours.DoAddButton(l);
ListAddItem();
Expand Down
6 changes: 3 additions & 3 deletions UInspectorPlus/MethodPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void InitType() {
public void Draw() => Draw(false);

public void Draw(bool readOnly, Rect? rect = null) {
if(target == null && Helper.IsInstanceMember(memberInfo))
if(Helper.IsInvalid(target) && Helper.IsInstanceMember(memberInfo))
return;
readOnly |= isInfoReadonly;
var referenceModeBtn = (!allowReferenceMode && (
Expand Down Expand Up @@ -358,7 +358,7 @@ public void Dispose() {
}

private void AddField(UnityObject target) {
if(target == null)
if(Helper.IsInvalid(target))
return;
BindingFlags flag = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
if(privateFields)
Expand Down Expand Up @@ -627,7 +627,7 @@ private void DrawDirectField(bool readOnly, Rect? rect) {
}

private void DrawUnknownField(bool readOnly, object target, Rect? position = null) {
if(target == null)
if(Helper.IsInvalid(target))
return;
bool clicked;
if(!position.HasValue)
Expand Down
4 changes: 2 additions & 2 deletions UInspectorPlus/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 您可以指定所有的值,或將組建編號或修訂編號設為預設值
//方法是使用 '*',如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.0.0")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyVersion("0.5.0.0")]
[assembly: AssemblyFileVersion("0.5.0.0")]

0 comments on commit ec8db36

Please sign in to comment.