playMaker

Author Topic: [SOLVED] AssetReference Type in Custom Action  (Read 5261 times)

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
[SOLVED] AssetReference Type in Custom Action
« on: November 21, 2019, 10:12:40 AM »
I'm writing some custom actions where I want to use the new Addressables system however the AssetReference Type does not seem to work with Playmaker. Anyone have any ideas how I can get this to work?

How it looks when you have a public AssetReference in the inspector. Notice that you get a nice drop down:


How it looks when you have a public AssetReference in PlayMaker. Notice that you get nothing. Not even somewhere to drag a reference into:
« Last Edit: November 29, 2019, 12:16:16 PM by JellyHair »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: AssetReference Type in Custom Action
« Reply #1 on: November 25, 2019, 09:36:37 AM »
Hi,

uhm... might be not supported indeed by PLayMaker inspector.

-- have you tried custom playmaker inspector
-- have you tried: https://forum.unity.com/threads/custom-inspector-for-a-list-of-addressables.575086/



Bye,

 Jean

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: AssetReference Type in Custom Action
« Reply #2 on: November 29, 2019, 12:15:45 PM »
Hi Jean - thanks for the pointers. The second link was key to cracking it. Here's how I got the AssetReference picker with a custom action editor:



Code: [Select]
using UnityEngine;
using UnityEditor;
using HutongGames.PlayMakerEditor;
using HutongGames.PlayMaker.Actions;

[CustomActionEditor(typeof(LoadAddressable))]
public class LoadAddressableCustomActionEditor : CustomActionEditor
{
    SerializedObject serializedObject;
    SerializedProperty property;

    public override void OnEnable()
    {
        var action = target as LoadAddressable;
        serializedObject = new SerializedObject(action.asset);
        property = serializedObject.FindProperty("Asset");
    }

    public override bool OnGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(property, false);
        serializedObject.ApplyModifiedProperties();

        return GUI.changed;
    }

}

Code: [Select]
using UnityEngine;
using UnityEngine.AddressableAssets;

public class AssetScriptable : ScriptableObject
{
    public AssetReference Asset;
}

Code: [Select]
using UnityEngine;
using UnityEngine.AddressableAssets;

namespace HutongGames.PlayMaker.Actions
{
    public class LoadAddressable : FsmStateAction
    {

        public AssetScriptable asset = ScriptableObject.CreateInstance<AssetScriptable>();

        public override void OnEnter()
        {
            Addressables.LoadAssetAsync<GameObject>(asset.Asset);
            Finish();
        }
    }
}
« Last Edit: November 29, 2019, 12:27:41 PM by JellyHair »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [SOLVED] AssetReference Type in Custom Action
« Reply #3 on: December 02, 2019, 02:48:02 AM »
Hi,

 Excellent. I'll ping Alex though on this cause maybe there is something that can be done internally, to allow addressables to be references without having to resort to a scriptableObject, which makes prone to bad memory handling.

Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [SOLVED] AssetReference Type in Custom Action
« Reply #4 on: December 02, 2019, 02:55:45 AM »
Hi,

I am reading the doc again on this, and I am getting confused.

- should the point of addressables to load them by string? like we do for Resources?

- what is the use case to have an action load an addressables by its direct reference?

Bye,

 Jean

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: [SOLVED] AssetReference Type in Custom Action
« Reply #5 on: December 02, 2019, 10:04:43 AM »
My understanding is that the AssetReference class is meant to replace the functionality of being able to drag and drop a gameobject reference into and inspector field as illustrated in the documentation here: https://docs.unity3d.com/Packages/com.unity.addressables@0.4/manual/AddressableAssetsGettingStarted.html.

My particular use-case is:
A designer can initialise a bunch of assets on the first state of a graph (character, location, animations etc.) and then run through a graph of whatever actions they want on those assets. Being able to pick the asset specifically from the Unity addressable picker eliminates human error associated with inputting a list of strings.

JellyHair

  • Playmaker Newbie
  • *
  • Posts: 16
Re: [SOLVED] AssetReference Type in Custom Action
« Reply #6 on: December 02, 2019, 11:10:11 AM »
On testing further I've found that the method above doesn't do a good job of retaining any data when you try and use a template and throws an error when trying to edit the state the action is on. I may have to resort to maintaining a list of strings somewhere and referencing them instead.

Code: [Select]
ArgumentException: Object at index 0 is null
UnityEditor.SerializedObject..ctor (UnityEngine.Object obj) (at C:/buildslave/unity/build/Editor/Mono/SerializedObject.bindings.cs:23)
LoadAddressablesCustomActionEditor.OnEnable () (at Assets/Editor/LoadAddressableCustomActionEditor.cs:15)
HutongGames.PlayMakerEditor.CustomActionEditors.GetCustomEditor (HutongGames.PlayMaker.FsmStateAction action) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Editor/Classes/CustomActionEditors.cs:77)


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [SOLVED] AssetReference Type in Custom Action
« Reply #7 on: December 03, 2019, 01:28:01 AM »
Hi,

 yes, string references are really dangerous for consistency over time and potential bugs as things get renamed...

maybe you can make a custom propertydrawer that let's you drop an addressables, but you get the name and store just the name, so that the user doesn't have to type it by hand. that would work ( but would not prevent renaming issues)

Bye,

 Jean