Search Unity

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

    Dismiss Notice

Instantiate addressable from custom inspector

Discussion in 'Addressables' started by ledbetterMRI, Mar 24, 2020.

  1. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    8
    I have a monobehavior base class from which all of my UI classes inherit. I'd like to write an editor that iterates over each AssetReference in the currently inspected child class and draw a button for each of them. If the user clicks this button, I want to instantiate the AssetReference in the scene view.

    Can someone on the addressables team help me bridge the SerializedObject API with Addressables? Note that the code provided falls apart when comparing the iterator.type to typeof(AssetReference). I feel like I'm definitely misusing the API here. Here's my current (non-functional) code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.AddressableAssets;
    4.  
    5. [CustomEditor(typeof(UIController), true)]
    6. public class UIControllerEditor : Editor
    7. {
    8.     public override void OnInspectorGUI()
    9.     {
    10.         //base.OnInspectorGUI();
    11.  
    12.         serializedObject.Update();
    13.         var iterator = serializedObject.GetIterator();
    14.         while (iterator.Next(true))
    15.         {
    16.             if (iterator.type == typeof(AssetReference).ToString())
    17.             {
    18.                 if (GUILayout.Button("Instantiate " + iterator.name))
    19.                 {
    20.                     Addressables.LoadAssetAsync<GameObject>(iterator.objectReferenceValue).Completed += loadAddressableComplete;
    21.                 }
    22.             }
    23.         }
    24.     }
    25.  
    26.     private void loadAddressableComplete(UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle<GameObject> loadedObject)
    27.     {
    28.         GameObject controller = (GameObject)serializedObject.targetObject;
    29.         Instantiate(loadedObject.Result, controller.transform.root);
    30.     }
    31. }
     
    Last edited: Mar 25, 2020
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    161
    Since you're in editor, you don't need to use the LoadAssetAsync API, you can simply use the AssetDatabase.


    Code (CSharp):
    1. AssetReferenceGameObject assetReference;
    2.  
    3. var guid = assetReference.AssetGUID;
    4. var path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
    5. var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path);
    6. var newObj = Instantiate(prefab);
    I'm not actually sure how you can get the
    AssetReference
    from the
    SerializedObject
    , though, since it does not derive from UnityEngine.Object. It's been a while since I've messed with custom inspectors.
     
    ledbetterMRI likes this.
  3. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    8
    Thanks for the heads up! I'll modify.

    Yea, the fetching an asset reference from the serialized object has me stumped currently. @unity_bill can you help me or point me to someone from Unity who can help bridge the gap between Unity's custom editor API and Addressable's AssetReference serialized field?
     
unityunity