- Home /
Sprite into texture alpha stretch problem
I'm making a Doom-styled game and I'm having trouble with the enemy sprites.
I'm using a panel to display the sprites and then I made this code to turn the sprite into a texture:
//ignore the array of sprites, pretend there's only one sprite
public Sprite[] spr;
// Update is called once per frame
void Start()
{
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
// set the pixel values
texture = spr[0].texture;
// Apply all SetPixel calls
texture.Apply();
// connect texture to material of GameObject this script is attached to
panel.GetComponent<Renderer>().material.mainTexture = texture;
}
Problem: My solution so far has been to go into the settings of this new texture and changing from Opaque to Cutout. But how do I do that in the script?
Answer by Namey5 · Aug 19, 2021 at 01:32 PM
I'm not sure what you mean by changing the texture from opaque to cutout - these settings are a part of the shader and so would be adjusted via the material (assuming you're using the standard shader);
Material mat = panel.GetComponent<Renderer>().material;
mat.SetInt ("_Mode", 1); // Rendering modes are [0-opaque, 1-cutout, 2-transparent]
Although I'm not sure why you can't just create a material in Editor with the right settings and use that. As a side note, most of your texture code is unnecessary;
// Creating a new texture here does nothing as it is about to be overridden
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
// Override the texture with the sprite's one
texture = spr[0].texture;
// This also does nothing as the sprite's texture hasn't been changed
texture.Apply();
panel.GetComponent<Renderer>().material.mainTexture = texture;
Instead, you can just use the sprite texture directly;
mat.SetTexture ("_MainTex", spr[0].texture);