Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[OpenSource] SerializableAction - serialization of any action, even anonymous!

Discussion in 'Scripting' started by Seneral, May 24, 2016.

  1. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Hi guys,
    I've developed a serializable action solution that handles most cases of actions, even anonymous:)
    So far it only supports parameterless, single-cast actions but it is easily extendable to support other needs.
    Anyway, here is my implementation:
    Github Gist

    Each of the scripts is a wrapper for their respective type they should serialize.
    You can use the other classes if you want to add to the functionality. I commented everything to make it a bit more understandable, and expanding on the features should be quite easy.

    Features:
    - Supports all serializable objects of both UnityEngine.Object and System.Object
    - For unserializable objects, their serializable fields get serialized, all other get defaulted (one layer only)
    - Support for static methods
    - Supports generic methods or methods in generic classes
    - Supports even anonymous actions! You can fully use the context and reference just about all local variables!

    If you find any kind of bug or constellation that does not serialize correctly, please tell me so I can fix it

    SerializableAction is the backbone of UndoPro and conversely enables Undo in Node Editor Framework; here's some more detailed explanation of what UndoPro can do and what not in regards to anonymous functions - the same applies to SerializableAction!
    If I ever were to update SerializableAction, I would most likely modify UndoPro and forget about this gist, if that happens please tell me and I'll update it.

    Seneral
     
    Last edited: Mar 2, 2020
    CunningFox146 and SugoiDev like this.
  2. Sponsrob

    Sponsrob

    Joined:
    Oct 27, 2016
    Posts:
    2
    Hello, is this still up to date with the latest version of Unity?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You could just use UnityEvent, couldn't you?
     
  4. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Technically I'm not sure, since I have not maintained the gist link. It is being used in UndoPro though, which in turn is being used in my Node Editor Framework, so yes, not only is it currently working, it likely will receive updates for the forseeable future in case something breaks. I'll update the gist soon.

    Not 100% sure anymore since it was a while ago (few years to be exact) since I developed it, but one of the following was a big problem for me (or maybe all). Rest assured I could NOT have used UnityEvent in UndoPro, for example.
    1. Doesn't work in editor tools
    2. Doesn't work with anonymous functions capturing the scope (see this, for example, which is backed by a SerializableAction)

    Also sorry for late reply, noticed I'm not (anymore) receiving email updates about this thread...