- Home /
Clearing pixel from texture changes the asset as well?
My code creates a sprite texture from a asset. It then clears the bottom pixel of that sprite. That works fine, but what I don't get is that the actual itself that I created the sprite from also has a cleared pixel, and until I reset Unity it stays that way. So why/how is that happening? here's a screen shot Unity to show you what I mean.
And here's the code I'm using that does that.
backSprite = Resources.Load(assetName, typeof(Sprite)) as Sprite;
//Create a new gameobject
obj = new GameObject("RedSquare");
//Attach a SpriteRenender to the newly created gameobject
SpriteRenderer rend = obj.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
//obj.transform.position = new Vector2 (0, 0);
//Assign the sprite to the SpriteRenender
rend.sprite = backSprite;
increment = 0;
speed = 4;
startPoint = transform.position;
endPoint = transform.position;
myTexture = backSprite.texture;
MyPixel = myTexture.GetPixel(0,0);
myTexture.SetPixel (0, 0, Color.clear);
myTexture.Apply ();
Answer by hav_ngs_ru · Feb 02, 2015 at 09:56 PM
its funny behavoiur but yes, iit changes. But does not saves (so just reload your texture or all project - and you will see original texture, without changings).
create new texture, copy origin data to it and assign it to the object, as workaround.
I guess its feature, not a bug... but it still not obviously for me.
Answer by opponent019 · Feb 02, 2015 at 10:46 PM
Mmh if you're always gonna clear the same pixel then you could create another texture with that pixel missing from the beginning and change between those two when needed. If not, then try instantiating? Or look around for a reload sprite/texture command so that it can do it automatically or in the case that you need your complete texture again in-game.
I ended up posting a connected issue in the forums http://forum.unity3d.com/threads/strange-issue-with-clearing-a-pixel-on-a-texture.295323/#post-1945791 I'm trying to figure out how to instantiate a texture and then a new game object, and set the texture to the game object and clear the pixel on that new version.
Oh then, I was right! :D I'm no pro with program$$anonymous$$g, still learning, so it feels good to see that hehe