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

Question How to import a sprite mask from a file

Discussion in '2D' started by tkdevman, Apr 22, 2022.

  1. tkdevman

    tkdevman

    Joined:
    Apr 22, 2022
    Posts:
    2
    Hello all this is my problem :

    I want to import a black-and-white texture from an image file (using LoadImage), convert it into a sprite (using Sprite.Create), and use it as a sprite mask (with the Sprite Mask component).

    I have done the above steps, however my problem is that the Sprite Mask uses its sprite's alpha channel, and I do not know how to import my texture into a format that will work with the sprite renderer.

    If I import a sprite into the asset menu, and select "From Gray Scale" as the alpha source in the texture import settings, this does what I want to do, however I do not know how to do this in code

    I would rather not write a function that converts the texture into the format I want, but have no problems with it if it must be done.


    Edit : the texture converter runs far faster than I though, so I will use that for now, but I still wonder if there is a better way
     
    Last edited: Apr 23, 2022
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    instead of black and white just make a png and delete the pixels that are supposed to be transparent in the mask

    load image imports the alpha of the image if you load a png
     
  3. tkdevman

    tkdevman

    Joined:
    Apr 22, 2022
    Posts:
    2
    Thank you for your reply, however the mask image needs to be grayscale