SetPixel and Sprites [noob advice needed]

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 :

  1. 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.

  2. 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 :slight_smile:
Booga.

I think when you drag an image into the scene, or hierarchy, Unity automatically makes it a sprite. An image is a UI element that takes a sprite.

I don’t know about the set pixel thing.

Thanks, but to use SetPixel I need a texture…
I’m seriously thinking about moving to Texture2D and the issue becomes… how do I import the file as Texture2D in the editor to create a prefab??
I was sure this was easy, but it becomes a sprite when I try…
I’ll try following http://wiki.etc.cmu.edu/unity3d/index.php/Importing_Textures tomorrow… although I don’t want a material… blah!

Help if you can…