Skip to content

Instantly share code, notes, and snippets.

@My-Key
Last active January 6, 2021 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save My-Key/5c7d495b3f5669fd2c89514acc458c08 to your computer and use it in GitHub Desktop.
Save My-Key/5c7d495b3f5669fd2c89514acc458c08 to your computer and use it in GitHub Desktop.
This drawer brings back to Odin drawing of blue lines next to overriden properties. Based on PropertyContextMenuDrawer
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;
[DrawerPriority(0,10000,0)]
public sealed class PrefabOverrideDrawer<T> : OdinValueDrawer<T>
{
private static readonly Color OVERRIDE_MARGIN_COLOR = new Color(0.003921569f, 0.6f, 0.9215686f, 0.75f);
protected override bool CanDrawValueProperty(InspectorProperty property)
{
#if ODIN_INSPECTOR_3
return !property.IsTreeRoot && property.SupportsPrefabModifications && property.State.Enabled &&
property.State.Visible && property.Tree.PrefabModificationHandler.HasPrefabs &&
GlobalConfig<GeneralDrawerConfig>.Instance.ShowPrefabModifiedValueBar;
#else
return base.CanDrawValueProperty(property) && property.SupportsPrefabModifications &&
property.Tree.PrefabModificationHandler.HasPrefabs;
#endif
}
/// <summary>
/// Draws the property.
/// </summary>
protected override void DrawPropertyLayout(GUIContent label)
{
bool draw = Event.current.type == EventType.Repaint;
bool overriden = false;
bool childOrCollectionOverriden = false;
if (draw)
{
var valueOverriden = Property.ValueEntry != null && Property.ValueEntry.ValueChangedFromPrefab;
var childValueOverriden = false;
var collectionChanged = false;
if (!valueOverriden)
{
collectionChanged = Property.ValueEntry != null && (Property.ValueEntry.ListLengthChangedFromPrefab ||
Property.ValueEntry.DictionaryChangedFromPrefab);
if (!collectionChanged)
childValueOverriden = ChildValueOverriden(Property);
}
overriden = childOrCollectionOverriden = childValueOverriden || collectionChanged;
#if !ODIN_INSPECTOR_3
overriden = overriden || valueOverriden;
#endif
if (overriden)
GUIHelper.BeginLayoutMeasuring();
}
if (draw && childOrCollectionOverriden)
GUIHelper.PushIsBoldLabel(true);
CallNextDrawer(label);
if (draw && childOrCollectionOverriden)
GUIHelper.PopIsBoldLabel();
if (!draw || !overriden)
return;
var rect = GUIHelper.EndLayoutMeasuring();
var partOfCollection = Property.Parent != null && Property.Parent.ChildResolver is ICollectionResolver;
if (partOfCollection)
rect = GUIHelper.GetCurrentLayoutRect();
GUIHelper.IndentRect(ref rect);
GUIHelper.PushGUIEnabled(true);
if (!partOfCollection && childOrCollectionOverriden)
rect.height = EditorGUIUtility.singleLineHeight;
rect.width = 2;
rect.x -= 2;
SirenixEditorGUI.DrawSolidRect(rect, OVERRIDE_MARGIN_COLOR);
GUIHelper.PopGUIEnabled();
}
private static bool ChildValueOverriden(InspectorProperty property)
{
var children = property.Children;
var count = children.Count;
for (var index = 0; index < count; index++)
{
var child = children[index];
var valueEntry = child.ValueEntry;
if (valueEntry != null && (valueEntry.ValueChangedFromPrefab ||
valueEntry.ListLengthChangedFromPrefab ||
valueEntry.DictionaryChangedFromPrefab))
{
return true;
}
if (ChildValueOverriden(child))
return true;
}
return false;
}
}
@My-Key
Copy link
Author

My-Key commented Sep 1, 2020

Update:

  • blue line is positioned according to property indent
  • properly work with attributes like Space and Title

Probably is a bit more expensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment