Author Topic: How to get a List<EventDelegate> to work nicely with SerializedProperties?  (Read 3605 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Hello,

I have a "MonoBehaviour" with a "List<EventDelegate>" called "onUse" - In my custom editor, I just want to get a 'valid' reference to "onUse" via serialized properties, and then cast the property back to a List<EventDelegate> so that I could draw it - But I'm failing though.

I'm getting the property successfully:
  1. sp_onUse = serializedObject.FindProperty("onUse");
  2.  

But I don't know what to do here:
  1. NGUIEditorTools.DrawEvents("On Use", use, ???);
  2.  

On the side: I see in your editor scripts, sometimes you use SerializedProperties and other times you manually register for undo and use target instead. I can see two reasons why you would use target: 1- you wrote the editors before SerailizedProperties came about 2- (same reason I don't use SPs) SPs are very annoying, unpredictable and really tough to work with, debug, etc.

What approach you suggest I go forward with? stick with target and manually register undo, multiple edit, etc? or Wrestle with SPs?

Thanks a lot in advance.
« Last Edit: February 12, 2014, 10:50:10 PM by vexe »

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #1 on: February 12, 2014, 10:51:27 PM »
Anybody?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #2 on: February 13, 2014, 12:06:03 AM »
  1. NGUIEditorTools.DrawEvents("On Use", serializedObject, "onUse");
Stick to SPs. They're the new way of doing things. They handle undo/redo and multi-object editing automatically, which is nice. Note that event drawing can't be multi-edited though.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #3 on: February 13, 2014, 12:20:36 AM »
you passed "onUse" as a string? is that a new overload for DrawEvents?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #4 on: February 13, 2014, 12:27:05 AM »
serializedObject.FindProperty works by the name of the property, so that's what must be passed. Just have a look at the DrawEvents function.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #5 on: February 13, 2014, 12:57:34 AM »
Sorry I still don't get what you mean:

  1. NGUIEditorTools.DrawEvents("On Use", serializedObject, "onUse");
  2.  

I just updated to the latest NGUI and still, there are only 2 DrawEvents:

  1. static public void DrawEvents (string text, Object undoObject, List<EventDelegate> list)
  2. static public void DrawEvents (string text, Object undoObject, List<EventDelegate> list, string noTarget, string notValid)
  3.  

None of which takes a serializedObject as a 2nd parameter, and a string as third.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #6 on: February 13, 2014, 01:07:14 AM »
Sorry, my bad -- List<EventDelegate> can't be a property:
  1. NGUIEditorTools.DrawEvents("On Use", target, (target as YourScriptType).onUse);
Be sure to replace "YourScriptType" with your actual script's name.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #7 on: February 13, 2014, 01:11:24 AM »
Thank you!

If you don't mind please:

1- Generally speaking, are you saying that there's no way to convert a serializedProperty back to a List<SomeClass>? if there is, how? - Problem with using objectReferenceValue is that it's a UnityEngine.Object, and List is a System.Object. Do I have to wrap my list with a MonoBehaviour or something?
2- In this instance you suggested to use target, when do I know that it's safe to use target, and when should I always and must use SPs? because you're using a combination of both in your scripts :/

Thanks a lot!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #8 on: February 13, 2014, 01:13:53 AM »
There is.

Instead of "target as YourScriptType" you can also use "serializedObject.FindProperty("onUse").objectReferenceValue as YourScriptType".

As you can see it's quite a bit longer.

'target' is what the editor script is observing -- the reference to the object. SerializedObject is the same thing, except it points to the serialized version of it.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #9 on: February 13, 2014, 01:16:05 AM »
Yes but in your editor scripts, sometimes you use target and manually register undo, sometimes just SPs and other times both! - Why don't you always use SPs and not manually register for undo, etc if they handle everything for you?

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #10 on: February 13, 2014, 03:04:03 AM »
Sorry if I'm asking a lot. But there's very little information about the subject and I'm trying to get the hang of editor scripting.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get a List<EventDelegate> to work nicely with SerializedProperties?
« Reply #11 on: February 13, 2014, 03:16:44 AM »
Some code is simply old and was written before SPs were added. Other places don't allow usage of SPs (like this List<> drawing).