UnityPreviewObjectField not highlighting asset when clicked

Issue #459 resolved
Charles Goatley created an issue

I'm using UnityPreviewObjectField in a custom drawer. It is rendering the texture and allowing drag and drop to work, but when I click on the preview image it does not highlight (ping) the asset in the Unity Project view, preventing you from finding the asset that it references. However, if I change the call to UnityObjectField it does highlight the asset when clicked.

The Item Drawers in the RPGEditor Window demo, which I adapated my code from, do also highlight the asset when clicked.

Unity version is 2017.4.0f1 64 bit, and Odin version is 2.0.8.0, but the version drop down doesn't give me that as an option!

Here is the code used for my custom drawer. The commented line at the bottom works as expected, the uncommented one exhibits the bug:

using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;

//
// All items are scriptable objects, but we don't want to draw them with the boring scriptable object icon.
// Here we create a custom drawer for all Item types, that renders a preview-field using the item icon followed
// by the name of the item.
//

public class GamePropDrawer<TGameProp> : OdinValueDrawer<TGameProp>
    where TGameProp : GamePropObject
{
    protected override void DrawPropertyLayout(GUIContent label)
    {
        var rect = EditorGUILayout.GetControlRect(label != null, 40);

        if (label != null)
        {
            rect.xMin = EditorGUI.PrefixLabel(rect.AlignCenterY(15), label).xMin;
        }
        else
        {
            rect = EditorGUI.IndentedRect(rect);
        }

        GamePropObject prop = this.ValueEntry.SmartValue;
        Texture texture = null;

        if (prop)
        {
            if (prop.Spawners.Length > 0)
            {
                ObjectSpawner spawner = prop.Spawners[0];
                if (spawner != null && spawner.PropPrefab != null)
                    texture = AssetPreview.GetAssetPreview(spawner.PropPrefab.gameObject);
            }
            else if (prop.Props.Length > 0 && prop.Props[0] != null)
                texture = AssetPreview.GetAssetPreview(prop.Props[0].gameObject);
            GUI.Label(rect.AddXMin(50).AlignMiddle(16), EditorGUI.showMixedValue ? "-" : prop.name);
        }

        this.ValueEntry.WeakSmartValue = SirenixEditorFields.UnityPreviewObjectField(rect.AlignLeft(60), prop, texture, this.ValueEntry.BaseValueType, allowSceneObjects : false);
        //this.ValueEntry.WeakSmartValue = SirenixEditorFields.UnityObjectField(rect.AlignLeft(100), prop, this.ValueEntry.BaseValueType, allowSceneObjects: false);
    }
}

Comments (3)

  1. mSkull001

    Thanks for reporting this issue.

    It seems to be caused by EditorGUIUtility.PingObject not being able to ping prefabs in the project window when you reference them by a component.

    This issue is now fixed and will likely be available in the 2.0.14+ release.

  2. Log in to comment