playMaker

Author Topic: Extending ObjectPropertyDrawer  (Read 1278 times)

Kristianhj

  • Playmaker Newbie
  • *
  • Posts: 4
  • Heureka Games
    • View Profile
    • Asset Store Profile
Extending ObjectPropertyDrawer
« on: January 09, 2019, 05:28:05 AM »
I'm a publisher at the Asset Store, I i'm trying to integrate my History/Favorites tool with Playmaker, but running into some trouble.

What I want to do is to extend the playmaker editor a bit to allow user to select directly from their favorited assets in places like this:



I have been able to extent the propertydrawer for AudioClip like this:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(AudioClip))]
public class FSMCustomAudioClipPropertyDrawer : HutongGames.PlayMakerEditor.ObjectPropertyDrawer
{
    public override Object OnGUI(GUIContent label, Object obj, bool isSceneObject, params object[] attributes)
    {
        EditorGUI.DrawRect(new Rect(0, 0, 50, 50), Color.yellow);
        EditorGUILayout.BeginVertical();
        EditorGUILayout.LabelField("From custom PropertyDrawer");
        EditorGUILayout.EndVertical();
        return null;
    }
}



But it just doesn't work for e.g. GameObjects and Materials. I'm trying to do it like this:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(GameObject))]
public class FSMCustomPropertyDrawer : HutongGames.PlayMakerEditor.ObjectPropertyDrawer
{
    public override Object OnGUI(GUIContent label, Object obj, bool isSceneObject, params object[] attributes)
    {
        EditorGUI.DrawRect(new Rect(0, 0, 500, 500), Color.cyan);
        EditorGUILayout.LabelField("YEAAAH GameObject");
        return null;
    }
}

Any info on whether or not this would be possible? And perhaps even how to do it?
« Last Edit: January 09, 2019, 05:33:48 AM by Kristianhj »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15489
  • Official Playmaker Support
    • View Profile
Re: Extending ObjectPropertyDrawer
« Reply #1 on: January 18, 2019, 12:25:34 PM »
Hi,

 I am too sure what you are trying to achieve. Could you do that on a regular monobehaviour and send me the script, I'll then see how to do it inside a PlayMaker action.

 Bye,

 Jean

ps: also don't hesitate to bump threads or pm me directly

Kristianhj

  • Playmaker Newbie
  • *
  • Posts: 4
  • Heureka Games
    • View Profile
    • Asset Store Profile
Re: Extending ObjectPropertyDrawer
« Reply #2 on: January 22, 2019, 07:30:18 AM »
Hi Jean

Thanks for your reply.
I want to modify the FSM action editor by adding a button next to properties of type GameObject.

I want to insert a button here that will allow the user to select from a list of favorite assets instead of a list of all assets (Which is the current default behavior)



I can customize the editor for AudioClips like this:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(AudioClip))]
But I cant do it for GameObjects like this:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(GameObject))]
Customizing the editor for Materials also doesn't work:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(Material))]

I have tried to test modifying the FSM action editor for "Create Object" with the following code, but it doesn't work:
Code: [Select]
[HutongGames.PlayMakerEditor.ObjectPropertyDrawer(typeof(GameObject))]
public class FSMCustomPropertyDrawer : HutongGames.PlayMakerEditor.ObjectPropertyDrawer
{
    public override Object OnGUI(GUIContent label, Object obj, bool isSceneObject, params object[] attributes)
    {
        EditorGUI.DrawRect(new Rect(0, 0, 500, 500), Color.cyan);
        return null;
    }
}

I would expect a cyan box to appear, but that doesn't happen (Works fine for AudioClips)

Did that clear up what i'm trying to achieve?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15489
  • Official Playmaker Support
    • View Profile
Re: Extending ObjectPropertyDrawer
« Reply #3 on: January 23, 2019, 02:17:53 AM »
Hi,

 I see, because this is actually a FsmGameObject that is used in this action, not a regular GameObject.

if you create

Public GameObject MyGameObject;

then that's going to be properly handled by your propertyDrawer.

 so instead of trying to modify a FsmGameObject, create a class that in turn control that FsmObject.

you could create a custom property editor  like this:

Code: [Select]
       GUILayout.BeginHorizontal();
        customClass.target = (FsmOwnerDefault) EditField("Target", typeof(FsmOwnerDefault), customClass.target, attributes);
        GUILayout.Label("hello");
        GUILayout.EndHorizontal();

with a custom class like that:

Code: [Select]
public class CustomClass
{
    public FsmOwnerDefault target;
}


does that make sense?

Bye,

 Jean

Kristianhj

  • Playmaker Newbie
  • *
  • Posts: 4
  • Heureka Games
    • View Profile
    • Asset Store Profile
Re: Extending ObjectPropertyDrawer
« Reply #4 on: January 23, 2019, 03:46:57 AM »
Hi Jean

Thanks for taking your time to try to explain it, but only understood like 80% :)

I tried a different route (which worked)
Code: [Select]
using HutongGames.PlayMakerEditor;
using UnityEngine;
using UnityEditor;

[CustomActionEditor(typeof(HutongGames.PlayMaker.Actions.CreateObject))]
public class SampleActionUI : CustomActionEditor
{
    public override bool OnGUI()
    {
        //EditField("gameObject");
        GUILayout.Label("FavoritesPRO");
        if (GUILayout.Button("Populate from History"))
        {
            HutongGames.PlayMaker.Actions.CreateObject favProTarget = (HutongGames.PlayMaker.Actions.CreateObject)target;
            string lastID = HeurekaGames.FavoritesPro.FavoritesProProfileData.Instance.GetSmartHistory()[0];
            Object loadedObject = (AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(lastID)));
            favProTarget.gameObject.SafeAssign(loadedObject);
        }

        DrawDefaultInspector();

        return GUI.changed;
    }
}



The code above allows me to hook into my tool (Favorites PRO) and assign to the gameobject field in the FSMAction. That's also great and all, but that would require me to do a CustomActionEditor each and every FSMAction in playmaker.

What I really want to do is do a propertydrawer for FSMMaterial, FSMGameObject etc. Maybe your last post showed me to do that, but I didn't fully grasp it
« Last Edit: January 23, 2019, 04:00:41 AM by Kristianhj »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15489
  • Official Playmaker Support
    • View Profile
Re: Extending ObjectPropertyDrawer
« Reply #5 on: January 24, 2019, 03:45:17 AM »
Hi,

no, you can't do that, as I said, the only option is to wrap it in your own class, and yes, it's therefore tricky to apply something to all actions.

 The problem with this is the conflict between several tools willing to each change the FsmXXX variables.

However, maybe you should contact Alex Chouls directly ( or if you are part of the beta, open a thread on this), because I certainly understand where you are coming from, I too would like to do such thing, and not just in PlayMaker but overal inside Unity.

Bye,

 Jean
« Last Edit: January 24, 2019, 03:49:50 AM by jeanfabre »

Kristianhj

  • Playmaker Newbie
  • *
  • Posts: 4
  • Heureka Games
    • View Profile
    • Asset Store Profile
Re: Extending ObjectPropertyDrawer
« Reply #6 on: January 24, 2019, 08:25:57 AM »
Thanks Jean

Thanks for the info, I wont bother Alex about this though, as I don't really have time to wait for any solution that might be implemented.

I think my approach from here, If I chose to continue working on it, will be to autogenerate CustomActionEditors for any and all FSMActions, and then customize the editor like that.

Thanks for your help, appreciate it.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15489
  • Official Playmaker Support
    • View Profile
Re: Extending ObjectPropertyDrawer
« Reply #7 on: January 25, 2019, 01:53:52 AM »
Hi,

 ok :)

 Bye,

 Jean