Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Display a PropertyField for an AssetReference without using [Serialize]

Discussion in 'Addressables' started by Firewolf34, Mar 14, 2019.

  1. Firewolf34


    May 14, 2013
    Lately we got to take advantage of the improved Custom PropertyDrawer for AssetReferences.

    This is great for if you have an object, that you'll [Serialize] and it has an AssetReference as a field, as it automatically creates a nice selection box in the Inspector for an AssetReference with the ability to look through the Addressable Asset Database and select one.

    However, I need a way to force draw the PropertyDrawer, manually, in an Editor class. Without waiting for the Inspector to do it automatically.

    I am making a custom Editor for a JSON file I am working on. The JSON file has a bunch of AssetReferences stored in it, as serialized text.

    I need a way to display the little AssetReference Field in the Inspector that is provided by the Custom PropertyDrawer, manually, via a function call or something. Since I'm in an Editor whose context (and inherited serializedObject property) is for the TextAsset that is currently selected, I have no SerializedObject to use for my PropertyField.

    I cannot simply do:
    Code (CSharp):
    1. EditorGUILayout.PropertyField(new UnityEditor.SerializedObject(myAssetReference));
    or more explicitly:
    Code (CSharp):
    1. EditorGUILayout.ObjectField("AssetReference Field:", typeof(AssetReference), false);
    Because the EditorGUI classes only work with UnityEngine.Objects, which an AssetReference is not.

    How can I display a selection field for an AssetReference without having to Serialize a field for the PropertyDrawer to automatically display? I want to draw the PropertyDrawer on-demand, when I want it, without having a SerializedObject. I can't just call the PropertyDrawer OnGUI methods manually because they're protected and also require SerializedObjects. And I can't Serialize a field because the object I'm inspecting is a TextAsset, not a custom ScriptableObject. I need a way to render a Custom PropertyDrawer inside of a Unity Editor class (e.g. class ConfigEditor : Editor)

    I don't want to re-invent the PropertyDrawer popup menu and everything just for my Editor. Any information you can provide is much, much appreciated! :)
    Last edited: Mar 14, 2019
  2. M_R


    Apr 15, 2015
    property drawers are only applied when you draw a SerializedProperty
    try something like
    Code (CSharp):
    1. class Dummy : ScriptableObject {AssetReference value;}
    2. OnEnable(){
    3. var dummy = ScriptableObject.Create<Dummy>();
    4. var so = new SerializedObject(dummy;
    5. var sp = so.FindProperty("value");
    6. }
    7. OnGUI(){
    8. so.Update();
    9. EditorGUILayout.PropertyFIeld(sp);
    10. so.ApplyModifiedProperties();
    11. use dummy.value....
    12. }
    KDCloud and AmitSuri like this.
  3. jerotas


    Sep 4, 2011

    I couldn't get this to work in a custom inspector. It does display the popup for Addressable, but if seemingly registers no changes no matter what you click....anyone get it to work properly in a Custom Inspector?
  4. KDCloud


    Apr 23, 2018
    like this to save.

    Code (CSharp):
    1. class Dummy : ScriptableObject {AssetReference value;}
    3. OnEnable(){
    4.     var dummy = ScriptableObject.Create<Dummy>();
    5.     var so = new SerializedObject(dummy);
    6.     var sp = so.FindProperty("value");
    7.     dummy.value = valueToSave;
    8. }
    10. OnGUI(){
    11.     so.Update();
    12.     EditorGUILayout.PropertyField(sp);
    13.     so.ApplyModifiedProperties();
    14.     // use dummy.value....
    15.     valueToSave = dummy.value;
    16. }