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

C#: Get texture from sprite slice for animating mesh texture

Discussion in 'Scripting' started by MentalGames, Dec 30, 2019.

  1. MentalGames

    MentalGames

    Joined:
    May 2, 2016
    Posts:
    39
    Heya!

    I have a real hard time finding information about how to do this. I have a sprite sheet that is set to multiple and I want to get the slices information so I can use it as texture on a mesh. Could someone point me in the right direction? Can this be done by using the rectOffset somehow? Or should I just forget it and use single sprites as opposed to a sprite sheet?

    I tried something like this (pseudo-ish code):
    Code (CSharp):
    1. public Sprite[] spriteList; //all sprite slices referenced in inspector
    2.  
    3. void Start(){
    4. renderer.material.mainTexture = spriteList[0].texture; //only gets the whole texture of the sprite sheet, not just the slice
    5. }
    If you're wondering what my goal is: I want to animate the faces of my character models not as 3D animation, but by changing the texture. I guess I can easily do this with single sprites, but I suppose it would be better using sheets (or atlases?)

    If it is unclear what I'm trying to achieve, please don't hesitate to inquire.

    Any help would be highly appreciated!
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
  3. MentalGames

    MentalGames

    Joined:
    May 2, 2016
    Posts:
    39
    Thanks! Alas, it is not really an answer to my question, which essentially is: how can I load the textures defined by sprite slices I cut by using the Sprite Editor when I reference them through the inspector? At the moment I'm solving my issue by offsetting and scaling the whole sheet, but out of interest, I would still love to know how to get the splice texture information.
     
    tonytopper likes this.
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Iirc the way sprites work is by offsetting the uvs, but sharing the same texture. So the texture is never actually cut.

    If you want to cut the texture itself, look into Texture2D.GetPixels and Texture2D.SetPixels
    you can use the Rects from the sprite and determine what pixels you need to get that way, then create new textures and set their pixels with this slice.
     
    tonytopper and MentalGames like this.
  5. RamonKing

    RamonKing

    Joined:
    Dec 13, 2019
    Posts:
    10
    I agree with aaro4130.
     
  6. MentalGames

    MentalGames

    Joined:
    May 2, 2016
    Posts:
    39
    Thank you aaro4130! You confirmed my assumption and maybe I will look into how to do this ;D Happy new year everybody!
     
  7. vmana

    vmana

    Joined:
    Aug 30, 2021
    Posts:
    1
    This post might be old, but it seems to be the only post on this subject, so I wrote some code that gets the sliced sprites texture.
    Code (CSharp):
    1. Texture2D GetSlicedSpriteTexture(Sprite sprite)
    2.     {
    3.         Rect rect = sprite.rect;
    4.         Texture2D slicedTex = new Texture2D((int)rect.width, (int)rect.height);
    5.         slicedTex.filterMode = sprite.texture.filterMode;
    6.  
    7.         slicedTex.SetPixels(0, 0, (int)rect.width, (int)rect.height, sprite.texture.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height));
    8.         slicedTex.Apply();
    9.  
    10.         return slicedTex;
    11.     }
    You just need to have the sprites parent texture Read/Write enabled.