Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Adagio-81 · Oct 17, 2015 at 04:40 PM · texturespritegraphicscopy

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? alt text

example.jpg (68.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adagio-81 · Oct 19, 2015 at 08:56 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges