Simple Sprite Sheet Animation in Unity ECS

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ม.ค. 2025

ความคิดเห็น • 88

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  5 ปีที่แล้ว +20

    Using this method we can get 10-20k individually animated Units.
    Stay tuned for the following video covering Graphics.DrawMeshInstance where we will see just how far we can push it!

  • @cheesecakenl1980
    @cheesecakenl1980 5 ปีที่แล้ว +12

    Wow exactly what I was looking for. Really liking ECS design pattern so far. More coding and structure less click & drag to connect everything together.

  • @technicaldisaster1590
    @technicaldisaster1590 5 ปีที่แล้ว +2

    oh jesus
    all of this for a moving sprite?
    I'm in tears

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +4

      Not for just a sprite, it's for an extremely fast sprite.
      Overkill if all you want is one sprite, a necessity if you want 10k+ animated sprites.

  • @auraonline1324
    @auraonline1324 5 ปีที่แล้ว +2

    Omg this is genius and an incredible implementation. Seems flawless although i know you put in hardwork. i would've gotten stuck on the shader attempting this solo.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      Yeah it was tough to figure out how to use the shader with different values. Lots of trial and error and trying many examples. Really need to take some time to properly learn shaders.

    • @chikato7106
      @chikato7106 5 ปีที่แล้ว

      @@CodeMonkeyUnity the shader graph is very handy. this example is so specific though.

    • @charg1nmalaz0r51
      @charg1nmalaz0r51 2 ปีที่แล้ว

      @@CodeMonkeyUnity could you also just change the uvs on the mesh what is the benefit of the shader approach because shader code makes 0 sense to me lol.

  • @cradle_of_chaos
    @cradle_of_chaos 3 ปีที่แล้ว +1

    This is again so useful. Would you consider maybe making a video for using GPU Instancing without ECS? That would reach a much wider audience.

  • @mehmedcavas3069
    @mehmedcavas3069 5 ปีที่แล้ว +5

    Best ecs tutorials

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +4

      Yup Physics and a complete Simple game is definitely on my list, need to find the time to do it.

    • @adamodimattia
      @adamodimattia 5 ปีที่แล้ว +1

      Absolutely agree, with Code Monkey's tutorials I am finally able to do my own ecs and jobs. And he covers stuff so much in depth, little by little making code more and more advanced gradually and really explaining stuff so you can grasp the mechanisms and patterns. Master Teacher!

  • @asdd2a
    @asdd2a 5 ปีที่แล้ว

    You DID IT!

  • @briangelhorn407
    @briangelhorn407 4 ปีที่แล้ว +1

    How i can use animator in a entity and add a animator controller and a one script for modify floats and bools in the animator controller?

  • @vladimir1838
    @vladimir1838 5 ปีที่แล้ว

    one more thing to get even better performance - you can calculate frame UV in a shader on GPU to free up more CPU time

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      Using compute shaders is indeed another way to get even more performance, need to look into that.

    • @vladimir1838
      @vladimir1838 5 ปีที่แล้ว

      I'm talking about vertex shader, you can adjust vertex uv before sampling texture

  • @TeoAl
    @TeoAl ปีที่แล้ว

    With DOTS 1.0 and the subscene workflow in mind, is it possible to do spritesheet animations using the standard animator clips / animator controllers unity creates?

  • @foamtoaster9742
    @foamtoaster9742 2 ปีที่แล้ว

    Would it not be better to cache the main camera as opposed to using camera.main for every draw call on every frame

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 ปีที่แล้ว

      Camera.main is now automatically cached, now need to cache it yourself nowadays

  • @warmbreezerising
    @warmbreezerising 5 ปีที่แล้ว +1

    Great tutorial ! I'm eager to see the use of DrawMeshInstance

  • @dadlord689
    @dadlord689 5 ปีที่แล้ว +1

    It would be perfect to meet key-frame animation + skeleton with ECS, but technically sprite sheets animation is still pretty great solution. I saw profiling graph with some basic C# code and it seems that script took pretty lot of time on execution, no matter how simple it is. Plus allocations\dealocations and GC in general. getting rid of will bring some performance already. Next thing is video memory, witch might get overloaded with sprite sheets. But technically it might be important on mobile only and probably only on devices with less then 1gb of VRAM. Well, I am sure that Unity will make possible to use key-frame animation later.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      Yeah sprite sheets a great and very simple but they do have a lot of limitations. In the Marines Vs Zombies video I used a Mesh based system and just grabbed the correct Mesh for that animation frame. It works but is a lot slower then the system here.

    • @dadlord689
      @dadlord689 5 ปีที่แล้ว

      @@CodeMonkeyUnity Yeah, this one is cool )

  • @chesterapavliashvili1533
    @chesterapavliashvili1533 5 ปีที่แล้ว

    You're LEGEND :D thx

  • @raven5165
    @raven5165 5 ปีที่แล้ว

    Awesome😎

  • @Marvin7-4-96
    @Marvin7-4-96 5 ปีที่แล้ว +1

    Hi! I learned from the actual github page how to instantiate 3D prefabs. Do you know how I can animate them? Its prety hard to learn ECS for 3D Objects/Prefabs and animations because their arent any tutorials out there :/. If I have some triggers or bools for the unity animator, where do I need to place them in my ecs code? I want that maybe 300 entitys with 3D Prefabs (that I converted to entitiys) walk around with the animation component.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      Right now the Unity Animator does not support ECS so you need to build your own.
      I think the Animator is on their roadmap for next year.

  • @seanpalmejarlabasan6704
    @seanpalmejarlabasan6704 5 ปีที่แล้ว +2

    I'm 11 and I want to become a game creator when I'm in college I'll be taking a course for computer engineering and this channel is a stepping stone to that dream

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      Awesome! Best of luck in your studies!

    • @Robber7
      @Robber7 5 ปีที่แล้ว

      Skip college if you are actually planning to work as a game dev. Its better and A LOT cheaper to just make your own portfolio. That matters more than a stupid exam when it comes to IT. (most of the time)

    • @brunettimarco
      @brunettimarco 5 ปีที่แล้ว

      Sean Palmejar Check this out th-cam.com/video/cUvjDOWYNY8/w-d-xo.html

  • @vrtech473
    @vrtech473 5 ปีที่แล้ว

    What is the difference between using public static Instance directly from the GameHandler and
    set private static instance and GetInstance()?
    Because in Awake() we can do
    Instance = this; with the public variable.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +2

      If you make it public static Instance then any class anywhere in your project can modify it. You could have an enemy script setting that instance to null. You don't want that to be possible, you want only that class to be able to set its own instance.

    • @vrtech473
      @vrtech473 5 ปีที่แล้ว

      @@CodeMonkeyUnity Thank you so much!.

  • @vasya123456777
    @vasya123456777 5 ปีที่แล้ว

    Hello! many thanks for all ur tutorials! And a question: it is possible to use sorting layers for entity? Or for DrawMesh?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      You have to implement it yourself, there's nothing preset like the SpriteRenderer.sortingLayer.
      If you have all your objects using a single mesh each then you can just modify the Z to set the sorting.

    • @vasya123456777
      @vasya123456777 5 ปีที่แล้ว

      @@CodeMonkeyUnity tyvm!

  • @saiyambhutani1086
    @saiyambhutani1086 5 ปีที่แล้ว +1

    Hey code monkey I am new to your channel. I want to make my own game after learning unity for about 2 years. I lave the sprites of you use for your channel like that of cap, assasian creed and spider man those were so cool man...So my main question is do you made them or get drom any website Pls pls pls reply

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  5 ปีที่แล้ว +1

    🤖Join the Community Discord discord.gg/eHjUVrm

  • @tylocook
    @tylocook 5 ปีที่แล้ว +1

    I wish you had explained why you chose not to use the RenderMesh system, but I think I know why. You cannot use Material Property Blocks in the current implemention of the Hybrid Renderer. And the Hybrid Renderer only seems to be performant if your entities all share the same exact material. Since your sprites require different material info at different times, then the Hybrid Renderer would not be able to draw them all with good performance.
    Am I right in assuming this is why you wrote your own specific renderer in this case?

    • @muzick300
      @muzick300 5 ปีที่แล้ว

      This isn't and answer but do you know if you are able to have different materials for the same group of entities.
      and are you able to change those material properties using the job system

    • @tylocook
      @tylocook 5 ปีที่แล้ว +1

      @@muzick300 I am not super experienced with the ECS yet. However, in my experience, while using RenderMesh and giving each one a different material, it seems to break the fact that ECS groups identical Entities into chunks (which is where major performance savings come from). Someone can please correct me if I am wrong, but it's how I interpreted what the Entity debugger was showing me when I tried it.

    • @muzick300
      @muzick300 5 ปีที่แล้ว

      @@tylocook So all entities of lets say an entity arch type must have the same material. So I cannot make a group of 10,000 entities of arch type X with half being the color green and half the color blue

    • @tylocook
      @tylocook 5 ปีที่แล้ว +1

      @@muzick300 You can create 10,000 entities of the archtype, yes. The Archetype will technically have a RenderMesh component. However, in my experience, if you change the material inside of RenderMesh to be different, it will break the ECS's way of packing things together tightly into memory (something they call chunks).
      So, the more material variations you have on otherwide identical archetypes, the more chunks you'll have.
      Keep in mind, I may be completely wrong about it, it's just what I think I've
      observed in the Entity Debugger.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      Yes the RenderMesh system doesn't have a way to set your own Material Property Blocks which is essential for this system to work.
      Check out the follow up video on 100,000 units which is only possible because of the custom renderer th-cam.com/video/t1f8ZreCuuQ/w-d-xo.html

  • @GuiadoProgramador
    @GuiadoProgramador 5 ปีที่แล้ว

    Hi CodeMonkey, are you that create all sprites for your games?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      Most I do myself in Photoshop but I've also worked with artists for certain things like the Weapons and Armors in Hyper Knights.

  • @adamodimattia
    @adamodimattia 5 ปีที่แล้ว

    Is a custom shader really necessary for this? Couldn't texture data be stored elsewhere? For example directly in the renderer script?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      The shader is required to do the UV work on the GPU.
      If you handle the UV in your script it will keep the CPU busy and make the code a lot slower since you won't be able to use GPU instancing.

    • @adamodimattia
      @adamodimattia 5 ปีที่แล้ว

      Oh ok, you probably explained it in tutorial now that I think about it, but I didn't quite get it. It's awesome thing then! It can hugely increase performance in so many other cases. Looks like it's time to learn some shaders programming 😌

  • @justinasvingilis
    @justinasvingilis 5 ปีที่แล้ว +1

    yes

  • @uskywan4145
    @uskywan4145 ปีที่แล้ว

    Hi CodeMonkey, I can't find the compoonentSystem,so i use SystemBase to instead.When i run my code,there comes the exception : UnityException: Internal_DrawMesh can only be called from the main thread. How can i do to fix it?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  ปีที่แล้ว

      The Entities package has changed massively since this video was made, the hybrid renderer code probably changed quite a bit so you need to look into that to figure out the new way of rendering things

  • @LimitedInput
    @LimitedInput 2 ปีที่แล้ว

    Would this methodology work with ECS 1.0?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 ปีที่แล้ว

      A lot of the syntax is different nowadays but yes the core data-oriented logic would be the same

  • @abdullahalsulami8440
    @abdullahalsulami8440 5 ปีที่แล้ว

    Thanks
    Please how can I make character menu ??

  • @winxalex1
    @winxalex1 5 ปีที่แล้ว

    Could we use IAnimationJob instead of Shader stuff for sprite Animation

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      That's part of the Experimental Animation API, no idea how that will evolve.

  • @ez4novi
    @ez4novi 5 ปีที่แล้ว

    Hi I have a question, if I have a 2048x2048 pixel sprite sheet with different animation frame in it with a 128x128 pixel per frame, including idle shoot etc,
    How do I offset it properly when changing a frame cuz Its not offset properly I tried your settings with the uvWidth = 1 / frameCount, its not offset to the right width and height sorry Im just a newbie for shader.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      How is it not offsetting properly? In your case you should end up with frameUVWidth = 128f / 2048f; Calculate the height the same way

    • @ez4novi
      @ez4novi 5 ปีที่แล้ว

      @@CodeMonkeyUnity Thank you finally fixed it, its just that my sprite sheet is not packed correctly so I edited it again. My frame animation are offset in vertical position so i used this.
      // frame pixels / sprite sheet size
      float uvWidth = 128f / 2048f;
      float uvHeight = 128f / 1920f;
      float uvOffsetX = spriteSheetAnimationData.animationIndex * uvWidth;
      ///
      /// animation index is 0 to 15 idle and shoot anim 0 - 7, 8 to 15
      /// S, SW, SE, W, E, N, NW, NE
      /// 0, 1, 2, 3, 4, 5, 6, 7 // idle index
      /// 8, 9, 10, 11, 12, 13, 14, 15 // shoot index
      ///
      ///
      float uvOffsetY = uvHeight * spriteSheetAnimationData.currentFrame; //

  • @justthethingsitlike
    @justthethingsitlike 4 ปีที่แล้ว

    Unfortunately, I cannot access project files via your website. Is there any way you can grant me those project filers. I need this codebase for my project.

  • @fahmyeldeeb
    @fahmyeldeeb 4 ปีที่แล้ว

    How are they individually animated when you're passing the material block to the SAME material each frame, you reference the same material, I can't replicate this, they are always flickering because they are trying to use each others MaterialBlock on the SAME material

  • @szooxgames704
    @szooxgames704 5 ปีที่แล้ว +1

    Nice video ! Can you make another video abaut layers in 2D , i trying to make a system like in Surviv Io (player can go Underground and cannot see did somoeones is up to him and kill)

  • @Steve-gi2yj
    @Steve-gi2yj 5 ปีที่แล้ว

    On 8 minutes, you said to go to my material then go to shader, select custom and custom shader or custom/instanceshader, but i dont have that, how do i get that??

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      The shader that I use is included in the project files or you can just google around for the simplest shader with GPU instancing which is what I did.

  • @TheUnluckyhobo
    @TheUnluckyhobo 3 ปีที่แล้ว

    need help I recreated the shader says I'm missing a , on line 11. but your code doesn't have any , on line 11. also I have no real idea how to get the custom shader to appear in the dropdown of shaders. PLEASE SOMEONE HELP ME! XD

    • @TheUnluckyhobo
      @TheUnluckyhobo 3 ปีที่แล้ว

      this is the error Parse error: syntax error, unexpected TVAL_ID, expecting ','

  • @eloque1973
    @eloque1973 5 ปีที่แล้ว

    I've been using this for a while, to find out about ECS. I suppose you are aware that ECS is deprecating some of the code you use?
    .ToConcurrent()' is obsolete: 'NativeQueue.ToConcurrent() is deprecated, use NativeQueue.AsParallelWriter() instead. (RemovedAfter 2019-10-25)'

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว

      Yup that's the only change needed as far as I'm aware, its just a name change and everything else works fine.

  • @TheGamingWiccan
    @TheGamingWiccan 5 ปีที่แล้ว

    how would i be able to use this to animate my player character when moving it around the game world and performing actions such as running jumping and anything else you can do in a 2d platformer like wall sliding wall jumping grappling with a grapple hook double jumping triple jumping swinging on rope or vines flying and attacking with melee and ranged and maybe weapon switching? the reason i ask is i'm making a 2d sidescrolling metroidvania called "AngeryMan" about a pissed of stickman who must save the multiverse from certain doom...

  • @arsenbabaev1022
    @arsenbabaev1022 4 ปีที่แล้ว

    HOW to use fucking Y dimension of sprite sheet

  • @rajcocgamming9854
    @rajcocgamming9854 5 ปีที่แล้ว

    please make fornite weapon hud system please please i love you

  • @mohamedessam-6228
    @mohamedessam-6228 5 ปีที่แล้ว

    thanks for your great work but why you focus at ECS only ...
    we need more projects, new idea for how to make game 2d
    but after all you'r great
    Channel and good Teacher

    • @auraonline1324
      @auraonline1324 5 ปีที่แล้ว +3

      what. this is terrible advice. That's your job to make basic games and learn as you go. Code Monkey is a god send teaching us this hard stuff like ECS and dots. Plus i want to see dots and ecs in his next game and hes already put in the work!

    • @mohamedessam-6228
      @mohamedessam-6228 5 ปีที่แล้ว

      @@auraonline1324 ok .

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  5 ปีที่แล้ว +1

      I try to vary the topics that I cover, the other 2 videos this week were not related to ECS
      Health Bar Damage Taken Effects th-cam.com/video/cR8jP8OGbhM/w-d-xo.html
      Endless Runner Level Generator in Unity th-cam.com/video/NtY_R0g8L8E/w-d-xo.html

    • @mohamedessam-6228
      @mohamedessam-6228 5 ปีที่แล้ว

      @@CodeMonkeyUnity what ever you cover it all good and thank you for teaching us what you know

  • @Mrpaul20
    @Mrpaul20 5 ปีที่แล้ว

    Who are also come from BRACKEYS ad