How to Serialize Interfaces in Unity (Drag and Drop Support)

Поделиться
HTML-код
  • Опубликовано: 17 янв 2025

Комментарии • 105

  • @git-amend
    @git-amend  2 месяца назад +18

    Happy Sunday, code warriors! 🎉 Drop a comment and smash that 👍💥

  • @Wbjpen
    @Wbjpen 2 месяца назад +36

    [X] Extremely useful tool
    [X] Great tutorial for custom editors
    [X] Bafflement that this feature isn't built into Unity

    • @git-amend
      @git-amend  2 месяца назад +5

      Thank you, I agree 💯 this should already be a feature of the engine!

  • @sisus_co
    @sisus_co 2 месяца назад +20

    Having full-blown interface support in Unity removes a huge source of constant friction, and opens up a new world of possibilities - I can't recommend this enough!

    • @git-amend
      @git-amend  2 месяца назад +3

      Couldn't agree more 💯

  • @01codebit
    @01codebit 2 месяца назад +3

    These advanced C# techniques are simply awesome! Great video as usual from you. Thanks for your work.

  • @spaderdabomb
    @spaderdabomb 2 месяца назад

    One of the best pieces of Unity content I've seen in a long time. Amazing stuff!

    • @git-amend
      @git-amend  2 месяца назад

      Wow, thanks! Glad to hear that!

  • @padrox9789
    @padrox9789 2 месяца назад +1

    It's brilliant when you see how fast you explain really complex stuff in general. I really love your videos because I feel like I'm learning so much about various generic topics.
    Thanks a lot for your work, you'll have my support all the way long.

    • @git-amend
      @git-amend  2 месяца назад

      Great to hear! Thank you!

  • @512Squared
    @512Squared 2 месяца назад

    You inspired the usual 'wow' from me. And having spent the last four months or so writing several custom property drawers and attributes, i was happy to see i followed most of this.
    Cheers again for sharing your valuable expertise.

    • @git-amend
      @git-amend  2 месяца назад

      That is awesome to hear! Thanks for the comment!

  • @chizuru1999
    @chizuru1999 2 месяца назад +1

    Your channel was much needed to me as a beginner game dev. I used to watch tarodev but he stopped uploading. You are godsend 😊

    • @git-amend
      @git-amend  2 месяца назад +1

      Happy to hear that!

  • @dragonsnackproductions
    @dragonsnackproductions 2 месяца назад

    I love these advanced takes ! Such talent and awesome presentation for years now! :) Thank you Sir!

  • @techdave99
    @techdave99 2 месяца назад +7

    Getting a better understanding of why things like the Odin inspector/serializer does. The video is also a good reference to setting up property drawers.

    • @git-amend
      @git-amend  2 месяца назад

      Good, glad to hear that! I made a few refinements to the drawer code in the repo if you want to review that too.

  • @sadbuttrue666
    @sadbuttrue666 2 месяца назад

    this is sick! We thought about dragging interfaces in on Friday, but we couldn't find a way, so thank you very much!

  • @lucianobisconti9078
    @lucianobisconti9078 2 месяца назад

    It's amazing how you explain, you're the beast, I love your code

    • @git-amend
      @git-amend  2 месяца назад

      I appreciate that!

  • @afriendlyfox
    @afriendlyfox 2 месяца назад

    The chad robot in the thumbnail is git-amend giving us the solution to the eternal pain the interface serialization is
    Thank you so much, I really didn't want to tie certain things to abstract classes just to have the convenience of drag-and-drop, and now I don't have to

  • @myk3l9675
    @myk3l9675 2 месяца назад +6

    I'm earlier than the guy who's going to say that you always post a video on exactly what problem he was having!

    • @git-amend
      @git-amend  2 месяца назад +1

      Hahah, very nice... and FIRST!

    • @Fitz0fury
      @Fitz0fury 2 месяца назад +1

      If I'm that guy I'm not sure you'll see many more of those. Not a lot of stuff to add to the project anymore. One or two to revisit when I upgrade AI.
      I did get a really strong run of like 10-12 videos that I used something from... which was honestly game changing for me. Just wanted to make sure to engage, but I often don't have any improvment note ..so I'm just thanking him.

    • @myk3l9675
      @myk3l9675 2 месяца назад +1

      ​@@Fitz0fury oh. I was just teasing, not hating!

    • @Fitz0fury
      @Fitz0fury 2 месяца назад +1

      @myk3l9675 I was not offended. I set myself up for it to be honest. I'd have made fun of me for it long ago if i were you. More than once I was thought "well im committed to making a comment. the guy has helped me out loads...to bad i have literally nothing valuable to say".
      Edit: also for context git-amend did make some of the videos due to issues I was having in my project..or atleast they provided inspiration for videos. So I was infact the direct benefactor.

  • @setroid8235
    @setroid8235 2 месяца назад +1

    I remember commenting about this topic recently. This is a really helpful solution. I wish this functionality was built in to Unity. (I also wish that Odin came standard too)

    • @git-amend
      @git-amend  2 месяца назад +1

      I couldn't agree more!

  • @suntonio7187
    @suntonio7187 2 месяца назад

    As per usual : great tuto ! Bravo!
    I always find myself considering changes to my projects right after i see your videos.
    Especially : kudos on your design patterns implementations, they are great pratical examples!
    PS : i know it would be very time-consuming, but i feel you could make indepth version of your videos that last twice the time with examples, code snippets, because you talk fast and some subject are quite advanced imho ^^

    • @git-amend
      @git-amend  2 месяца назад

      Thanks for the comment. I'll keep your feedback in mind, though time is always limited.

  • @EliotCarney-Seim
    @EliotCarney-Seim 2 месяца назад

    Love your vídeos man, thank you for making them. You're seriously underrated.
    .
    .
    .
    But man do me a for and prepend your private member fields with an underscore!!!

    • @git-amend
      @git-amend  2 месяца назад

      Thanks for the comment, but I will not be using underscores as prefixes.

    • @EliotCarney-Seim
      @EliotCarney-Seim 2 месяца назад

      ​@@git-amendBoooooo! Whose naming convention do you follow if not Microsoft's?

  • @antijulius
    @antijulius 2 месяца назад +5

    "Wouldn't it be nice if you could assign objects by interface?" YES IT WOULD. If I had a nickel every time I thought this to myself I'd have like... fifty bucks.

  • @Gangerowski
    @Gangerowski 2 месяца назад

    20 min and you solved the biggest pain in Unity? I am amazed! and a bit scared :D

    • @git-amend
      @git-amend  2 месяца назад +1

      Haha well it took most of Friday night to be honest... condensed down into 20 mins!

  • @정동우-n2x
    @정동우-n2x 2 месяца назад

    RequireableInterface That's a really good keyword! always Thanks!

    • @정동우-n2x
      @정동우-n2x 2 месяца назад

      May I ask you a minor question? I recently heard from OOP that they prefer composition to inheritance.
      Right now, it seems like you're using the interface in combination with the composition, but I'm going to ask you a question
      What are the benefits compared to DI injection?
      I'm sorry for the rudimentary question.

    • @git-amend
      @git-amend  2 месяца назад +1

      Here, the advantage is that we can drag and drop things that are in the scene or things that are Assets in the project like prefabs or scriptable objects. DI is more for things that do not derive from UnityEngine.Object, such as pure C# services or factory classes that can supply your dependencies at runtime.
      As far as composition vs inheritance, I think it depends on the situation. You can watch the video on the homepage of the channel if you haven't yet. Generally speaking, it is better to use composition than inheritance, but not always. You have to use your best judgement.

    • @정동우-n2x
      @정동우-n2x 2 месяца назад +2

      @@git-amend I watched the video and posted a comment. Teacher.
      Thank you for always answering the novice questions without getting annoyed.
      I'm asking you this question because the basis of my code is you and teacher Smack.
      That's why I'm trying to learn as much from you as possible, I prefer DI injection, but I asked you out of curiosity because your code prefers serialization.
      I really want it to be your Unity official tutorial.
      Thank you always.

  • @jonathanthorpe4228
    @jonathanthorpe4228 2 месяца назад

    Thank you so much for this! So useful...

    • @git-amend
      @git-amend  2 месяца назад

      You're so welcome!

  • @RoxGame
    @RoxGame 2 месяца назад +1

    Two days ago, I was searching for that - it's as if you can read minds!

    • @git-amend
      @git-amend  2 месяца назад +1

      Nice, glad to hear that!

  • @tst2648
    @tst2648 2 месяца назад +1

    How do you feel at this point about also doing the UI Toolkit version in the property drawer in these type of videos, or would it add too much time/complexity? Great stuff as always, thanks!

    • @git-amend
      @git-amend  2 месяца назад

      Thank you! Unfortunately, making something custom in UI Toolkit would essentially be a whole topic on it's own. And in this case it's not really necessary, we can piggyback on the existing GUI methods.

  • @nixonscherbarth1974
    @nixonscherbarth1974 2 месяца назад +2

    This is a gamechanger, waaaaaaaaaaaaaaaaaaaaaauw!

    • @git-amend
      @git-amend  2 месяца назад

      Glad to hear that! Hope it helps you out!

  • @twistedliverstudio
    @twistedliverstudio 2 месяца назад +2

    Solid as always and invaluable information.

    • @git-amend
      @git-amend  2 месяца назад +1

      Much appreciated!

  • @rutchjohnson
    @rutchjohnson 2 месяца назад

    Happy Sunday!

  • @Fen1kz
    @Fen1kz 4 дня назад

    I believe there's a mistake in the video.
    At 11:45, line 19, you have `EditorGUI.ObjectField(position, label, underlyingProperty.objectReferenceValue, typeof(UnityEngine.Object), true)`
    meanwhile in the github the 4th parameter is different - it should be args.ObjectType
    I was trying to follow with you and actually type code you type in, in attempt to catch up with you and understand at least something.

    • @git-amend
      @git-amend  3 дня назад +1

      Improvements and bugfixes happen sometimes after the video is released, so the video and the repository may not always match. You might find it more useful to reference the first commit, and then compare what has changed and why in subsequent commits.

  • @HasanZekiGöğebakan-f6t
    @HasanZekiGöğebakan-f6t 2 месяца назад

    A great video again 👍
    What about for classes that are not Unity Object or Mono.
    Should we add SerializationCallbacks for each class and handle them ourselves?

    • @git-amend
      @git-amend  2 месяца назад

      I think that depends on what you are trying to do. In many cases, Unity has the [SerializeReference] attribute which was meant for anything that doesn't derive from UnityEngine.Object. However, it also lacks support for the inspector when it comes to interfaces. There are a few workarounds, such as making a base class that you can inherit from. Or build (or modify) a solution like the one in the video. I was thinking about making a serializable callbacks video too, maybe I'll do that one sooner than later.

  • @1DemiRaven
    @1DemiRaven 2 месяца назад

    You did a video on stats and modifiers a while back but I'm curios about how you would manage the full amount of player stats while keeping things like health, attack, defense, etc. Modular for use on other things like enemy agents. I'd consider something like a manager class and scriptable objects.
    maybe having a dedicate player manager and separate enemy/Npc manager? Is this in the right direction?
    also since its relevant, what interfaces would you implement in the process. Im still learning this stuff 🧐

    • @git-amend
      @git-amend  2 месяца назад

      These are complex questions that can't be answered in a YT comment. I recommend joining Discord and asking your question under the related video topic.

  • @Neoisrael007
    @Neoisrael007 2 месяца назад

    Another great video, How is this not supported as a built in feature in unity ? Amazing ! Thanks !

    • @git-amend
      @git-amend  2 месяца назад

      Yes, I wish it were native, it would be so useful!

  • @tonynussbaum
    @tonynussbaum 2 месяца назад

    Seriously, everyone should just buy Odin. It's amazing.

    • @git-amend
      @git-amend  2 месяца назад

      Yes, I agree... black friday coming soon, so no excuses!

  • @bnaZan6550
    @bnaZan6550 2 месяца назад +3

    Unity does support serializing interfaces using [SerializeReference]. Since [SerializeField] serializes by values, and interfaces don't have a values.
    Unless I'm getting this wrong?

    • @git-amend
      @git-amend  2 месяца назад +1

      You will find that there is no support for this, and the field will not be visible in the inspector.
      [SerializeReference] public IDamageable item;
      Technically you could work around that with a custom inspector or drawer, or by using a base class that implements the interface. But there are other limitations as well. See: docs.unity3d.com/ScriptReference/SerializeReference.html

    • @sisus_co
      @sisus_co 2 месяца назад

      [SerializeReference] only supports serializing plain old class objects, not UnityEngine.Object references, and it also has no built-in Inspector support :(

    • @user-oc8jp2bk2y
      @user-oc8jp2bk2y 2 месяца назад

      ​@@git-amend what limitations tho? I always serialize interfaces as a base class (Like MonoBehaviour itself iirc) which then casted to an interface in the script. Only issue could arise is if I ignore OnValidate sanity check giving me an error if I placed wrong class there. I mean, this is not the proper clean solution, but it seems to work well. Yeah, basically I did 5:23 just what you started from.

  • @owencoopersfx
    @owencoopersfx 2 месяца назад

    Thank you!

  • @hop3studio511
    @hop3studio511 2 месяца назад

    Very useful tutorial as always, but then something bothering me, i see you are using unity 6 for this tutorial, yet still using the old imgui for editor stuff, is there any specific reason for that?, maybe it's easier or just your preferences..
    Thank you in advance

    • @git-amend
      @git-amend  2 месяца назад +2

      I hope that doesn't bother you too much. I try to only introduce one complex topic per video. UI Toolkit is still new to most people, so that will have to be a separate video.

    • @hop3studio511
      @hop3studio511 2 месяца назад

      @git-amend thanks, indeed this topic already complex for most of us to handle, don't stop making it sir, anyway another thing about ui toolkit, if there any chance in future, can you cover about listview, multicolumn list view, tree view etc, with support for use editor binding via binding path... Currently it's just so weird for me, and also i see many question about that in unity discussion with no clear "true way"

  • @davidheeren3751
    @davidheeren3751 2 месяца назад

    Do you use Midjourney for your thumbnails?

    • @git-amend
      @git-amend  2 месяца назад

      Sometimes, and yes today this is from Midjourney with a heavy dose of Photoshop.

  • @karole5646
    @karole5646 2 месяца назад

    No idea why this is not built-in...tysm

    • @git-amend
      @git-amend  2 месяца назад

      You're welcome, maybe Unity 7 will support this...

  • @redragon1229
    @redragon1229 2 месяца назад

    I've heard Unity 6 now has its own serializer

  • @JumpCatStudio102
    @JumpCatStudio102 2 месяца назад +1

    isnt unity already supported this by [SerializeReference] ?

    • @git-amend
      @git-amend  2 месяца назад +1

      The [SerializeReference] attribute is meant to work with regular class objects, meaning UnityEngine.Object types aren’t supported, and it lacks Inspector support by default.

  • @hoying_ttabi
    @hoying_ttabi 2 месяца назад

    I'm sorry, it's different from the video, but I'm asking because there's a problem. When moving on to the next scene in Unity, how do I call up and use cavas and player objects that contain various UI used in the previous scene in the next scene? And can you tell me how to maintain the reference values that I connected on the editor in unity?

    • @git-amend
      @git-amend  2 месяца назад

      Often people will use an extra scene to hold references that need to persist between scenes. You should watch a video like this one: ruclips.net/video/JFP-cCFID7o/видео.html

  • @islefootball
    @islefootball 2 месяца назад

    Whats your system specs you are using?

  • @damonfedorick
    @damonfedorick 2 месяца назад

    Nice

    • @damonfedorick
      @damonfedorick 2 месяца назад

      Should i scrap this for property drawer Extensions?

    • @git-amend
      @git-amend  2 месяца назад +1

      Thanks

  • @luizthiagosouza5377
    @luizthiagosouza5377 2 месяца назад

    Omg ty bro

  • @Nice.Creeper
    @Nice.Creeper Месяц назад

    while using this i deskovered a problem you mite want to hear about.
    when you have multipal of the same script on diffrent components fx(enemy.Script with a attack.Interface).
    and you then select them all at the same time. the interface on the first component will override the one on the otheres. this isen,t the worst and you can work around this by only selecing one objeckt at a time. but i thote you mite want to know if you plan on updating the scripts

  • @pierreb4959
    @pierreb4959 2 месяца назад

    interesting but I don't understand much, not even what it's basically used for...😭 thanks anyway 😂

    • @git-amend
      @git-amend  2 месяца назад +1

      Maybe we'll have a video on using interfaces soon, I feel like you are not alone.

    • @pierreb4959
      @pierreb4959 2 месяца назад +1

      @@git-amend yes i'm just looking at what is an interface on unity website :)

    • @suntonio7187
      @suntonio7187 2 месяца назад +1

      Une interface est un contrat que passe une classe, qui garanti l'implémentation d'un (ou plusieurs) membre (dans l'exemple : la méthode Damage(int amount)).
      Cela permet que plusieurs objets différents (une classe "héro",une classe "ennemi" ou une classe "porte" par exemple, toutes les classes auxquelles tu veut pouvoir infliger des dégâts dans ton projet) qui implémentent cette interface auront forcément une méthode publique Damage(int amount).
      Une interface étant un type cela permet aussi de traiter 'hero' et 'ennemi' comme étant des IDamageable (et ainsi mettre des objets de nature différente dans une même liste par exemple).
      C'est très simplifié, mais j'espère que cela clarifie la question ;)
      PS : les interface sont des outils C#, ne limite pas tes recherches a Unity ^^
      PPS : vu que tu a l'air de t'appeler pierre, je me suis permis de répondre en français ;) (google trad corrigera mon erreur au cas ou, j'espère)

    • @pierreb4959
      @pierreb4959 2 месяца назад

      @@suntonio7187 oui , merci, mes recherches m'ont amené à comprendre exactement ce que tu as expliqué 😉

  • @VMariPadma
    @VMariPadma 2 месяца назад +1

    😉Hi G Can you make an video for me i have struggled in the middle path of unity concept. I know loops , conditions, Interface, abstract, but not like you serialise and making😵 anything custom please make an video for a guid what topic🤗 are here in unity programmer. I dont know what are the concpets are here i am learning like an islands 🏝 in ocen pleace givr an clarify the conpets or need to learn in unity programing like you. I need to become an coder like you please guidence. Iam making an smoke fire to any one see my and give your hands to clarify me.😅not able to buy an courses

  • @isleeg
    @isleeg 2 месяца назад

    If you have no labels!! Make sure you put InterfaceReferenceUtil.OnGUI(position, underlyingProperty, label, args); after EditorGUI.EndProperty(); in your InterfaceReferenceDrawer(.cs) script.

  • @RoxGame
    @RoxGame 2 месяца назад

    I'm wondering if we can assign a class that has an interface but doesn't inherit from MonoBehaviour or ScriptableObject.

    • @git-amend
      @git-amend  2 месяца назад +1

      This setup is just meant for things that inherit from UnityEngine.Object. For other objects (System.Object) technically that is what [SerializeReference] is meant to be used for... though it lacks support for the Inspector as well for interfaces. But you could create a Base class that implements an interface and then inherit from that. Some examples here: docs.unity3d.com/ScriptReference/SerializeReference.html

    • @RoxGame
      @RoxGame 2 месяца назад

      ​@@git-amend The base class seems promising, but after giving it a try, only its label appears while the drag-and-drop field is missing, which is odd.
      [SerializeReference] public Base Test;