Hi,
I’m quite new to Unity, and I have a lot of questions and find solutions for most, but the 2 most bothering are :
-
I have created a prefab out of a bmp file, and saw it has a Sprite Renderer component. Is it now a Sprite? An Image? Texture? How do I know?
I can instantiate it ok, but I’m wondering why the Object type isn’t clear…
The code I’m using is
GameObject image_copy = Instantiate(Resources.Load(“prefabs/copy”), new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
I’m sure this is some core idea I’m missing. -
I want the user to be able to change selected pixels in the sprite directly, so I found the SetPixel method that works only on Textures. But (I think) I have a Sprite, so I finally found a way around it, but it seems very costly and very awkward… Is there a simpler way to do all this? Should I stick with a 2DTexture and not Sprites? Please advise.
My awkward code to set pixel is currently :
SpriteRenderer renderer = image_copy.GetComponent();
if (renderer == null)
{
Debug.LogError (“renderer is null :(”);
}
Texture2D tex = renderer.sprite.texture;
Texture2D newTex = (Texture2D)GameObject.Instantiate(tex);
newTex = new Texture2D (tex.width, tex.height, TextureFormat.ARGB32, false);
newTex.SetPixels32(tex.GetPixels32()); // otherwise the image turns gray… (why?!)
newTex.SetPixel (2, 2, Color.black);
newTex.Apply();
renderer.sprite = Sprite.Create(newTex, renderer.sprite.rect, new Vector2(0.5f, 0.5f));
Thanks in advance ![]()
Booga.