Copy texture data from one Sprite to another
I'm in a situation where so far Google has been of no help, hoping a kind soul inhere could show me the way
The situation: In my game I have a sprite with a texture I'm creating at the start of a level. This texture is completely transparent at this point. The texture fills the complete level area I then have a lot of prefabs with Sprites. These Sprites has some preset graphics (some parts transparent, some parts not) and a certain tag for easy finding I would like to go through each of these sprites, take all the visible area of the sprite and copy it to my full level texture I have created at the start of a level in the correct position
I have uploaded a picture with my idea. On the right side you see the actual game view, with a few objects with the tag "Platform". On the left side we have the texture I'm building ingame. The red parts of the texture is where the visible parts of the "Platform" sprites are.
I have written the following code:
// Get needed objects
Transform t = this.transform;
GameObject go = t.gameObject;
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
// Create new Sprite with empty texture
// The object Bounds is a BoxCollider I use to define the size of my level
sr.sprite = Sprite.Create(new Texture2D((int)(Bounds.bounds.size.x * 100) + 10, (int)(Bounds.bounds.size.y * 100) + 10, TextureFormat.ARGB32, false), new Rect(0, 0, Bounds.bounds.size.x * 100, Bounds.bounds.size.y * 100), new Vector2(0.5f, 0.5f));
sr.transform.position = new Vector3(Bounds.transform.position.x, Bounds.transform.position.y);
var platforms = GameObject.FindGameObjectsWithTag("Platform");
foreach (GameObject platform in platforms)
{
Color platformColor = Color.red;
Transform platformTransform = platform.transform;
GameObject platformGameObject = platformTransform.gameObject;
SpriteRenderer platformSpriteRenderer = platformGameObject.GetComponent<SpriteRenderer>();
Sprite s = platformSpriteRenderer.sprite;
Transform parent = platformSpriteRenderer.transform.parent;
// Here at this point I have the sprite and parent
// I assume I need to the parent for position of the sprite
// But how do I get from here to copying the visible parts of this sprite
// to the texture in sr.Sprite?
// I don't need to have the colors of the original sprite, painting the
// pixels in e.g. color red (as shown in the uploaded image) would be fine
}
Any idea how to do this?
I'm getting a bit closer to the solution, but not fully done yet. For those interested this is what I have done so far:
I have added an extra camera. This camera is set to take up all the level area (and a bit extra). A Render Texture was also created with a big enough size to be useful for my needs. Right now it's set to 2048 * 2048 ARGB32 Then I created a $$anonymous$$aterial with a shader that accepts a Render Texture and added the before mentioned Render Texture to it. I used $$anonymous$$obile/Diffuse shader, but then I haven't tried others to see if others would be better On the added extra camera I have set the Target Texture to the Render Texture created.
Then in a script I have added the following code:
[SerializeField]
RenderTexture securityCameraTexture; // drag the render texture onto this field in the Inspector
[SerializeField]
Camera securityCamera; // drag the security camera onto this field in the inspector
public IEnumerator CreateGameView()
{
yield return new WaitForEndOfFrame();
// Get the camera's render texture
RenderTexture rendText = RenderTexture.active;
RenderTexture.active = securityCamera.targetTexture;
// Render the texture
securityCamera.Render();
// create a new Texture2D with the camera's texture, using its height and width
Texture2D cameraImage = new Texture2D(securityCamera.targetTexture.width, securityCamera.targetTexture.height, TextureFormat.ARGB32, false);
cameraImage.ReadPixels(new Rect(0, 0, securityCamera.targetTexture.width, securityCamera.targetTexture.height), 0, 0);
cameraImage.Apply();
RenderTexture.active = rendText;
// Get needed objects
Transform t = this.transform;
GameObject go = t.gameObject;
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
// Create new Sprite with empty texture
sr.sprite = Sprite.Create(cameraImage, new Rect(0, 0, cameraImage.width, cameraImage.height), new Vector2(0.5f, 0.5f));
sr.transform.position = new Vector3(Bounds.transform.position.x, Bounds.transform.position.y);
// Fills with empty. Needed for collider
Color fillColor = new Color(1, 1, 1, 0);
var fillColorArray = sr.sprite.texture.GetPixels();
for (var i = 0; i < fillColorArray.Length; ++i)
{
if (fillColorArray[i].r == 1 && fillColorArray[i].g == 0 && fillColorArray[i].b == 1)
{
fillColorArray[i] = fillColor;
}
}
sr.sprite.texture.SetPixels(fillColorArray);
//sr.sprite.texture.ReadPixels(GetWorldBounds(Bounds), 0, 0, true);
sr.sprite.texture.Apply();
}
It's far from done, but it's getting there
Your answer
Follow this Question
Related Questions
Applying one sprite/texture across multiple gameobjects. (2D) 0 Answers
Using Graphics.CopyTexture with Compressed (DXT) textures 1 Answer
Same animation from multiple spritesheets 1 Answer
Does Unity resize 2D textures in scene automatically? 0 Answers
Applying camouflage without spriting multiple sprites 0 Answers