Hey everybody. I have a small question:
So I have this Script that searches for a color in a sprite through converting it into a Texture2D.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorChanger : MonoBehaviour
{
public Color getColor;
public Color newColor;
public SpriteRenderer renderer;
public Sprite size;
// Update is called once per frame
void Update()
{
CopyTexture2D(renderer.sprite.texture);
}
//CopiedTexture is the original Texture which you want to copy.
public void CopyTexture2D(Texture2D copiedTexture)
{
//Create a new Texture2D, which will be the copy.
Texture2D texture = new Texture2D(copiedTexture.width, copiedTexture.height);
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
int y = 0;
while (y < texture.height)
{
int x = 0;
while (x < texture.width)
{
if(copiedTexture.GetPixel(x,y) == getColor)
{
Debug.Log("color found");
texture.SetPixel(x, y, newColor);
}
else
{
texture.SetPixel(x, y, copiedTexture.GetPixel(x,y));
}
++x;
}
++y;
}
texture.Apply();
Rect spriteRect = new Rect(0, 0, size.texture.width, size.texture.height);
renderer.sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f));
}
}
I use the Sprite “size” as an example for the sprite size.
The color-changing works and I get a recolored Sprite back into my SpriteRenderer. But that new Sprite is bigger than my original, even though I use that stuff with “size”.
Does anybody know how I can scale my Sprite as big as before? (128x128px, if that´s important.
Thanks in advance