Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by qvatra · Apr 12, 2014 at 12:02 PM · texturetexture2ddrawmerge

How to merge two textures of different size?

I'm developing 2d Tower Defense game and I want to be able to modify background texture(draw textures of dead units on it). I don't want to use separate objects to put on top of the background because I assume this will significantly slow down my game (number of units suppose to be VERY large).

I've found a code that allow to merge textures of the same size:

 var tex1 : Texture2D;
 var tex2 : Texture2D;
 
 function Start (){
 
         var pix1 = tex1.GetPixels32();
         var pix2 = tex2.GetPixels32();
 
         if (pix1.Length != pix2.Length) {
             Debug.Log("Images need to be the same size");
             return;
         }
 
         for (var i = 0; i < pix1.Length; i++) {
 
             if (pix2[i].a != 0) {
                pix1[i] = pix2[i];
             }
         }
 
         tex1.SetPixels32(pix1);
         tex1.Apply();
 
         renderer.material.mainTexture = tex1;
 }

How can I modify this code to be able to merge background texture with small textures (of dead units) at specific position?

Comment
Add comment · Show 2
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 tomekkie2 · Apr 12, 2014 at 05:22 PM 0
Share

Possibly you could deter$$anonymous$$e the combined size and then set the canvass in both textures to it, supllementing the textures withtransparent pixels.

avatar image qvatra · Apr 12, 2014 at 05:46 PM 0
Share

yes, it's possible but how then I could place small texture in specific position? Let's say I want to place it at the center of bigger texture.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by qvatra · Apr 12, 2014 at 09:51 PM

The solution is here:

 #pragma strict
 
 var texToDraw : Texture2D;
 var x: int;
 var y: int;
 
 function Start (){
         var background : Texture2D = Instantiate(renderer.material.mainTexture);
         var pix1 = background.GetPixels32();
         var pix2 = texToDraw.GetPixels32();
 
         for (var j = 0; j < texToDraw.height; j++){
             for (var i = 0; i < texToDraw.width; i++) {
                 pix1[background.width/2 - texToDraw.width/2 + x + i + background.width*(background.height/2-texToDraw.height/2+j+y)] = pix2[i + j*texToDraw.width];
             }
         }
         
         background.SetPixels32(pix1);
         background.Apply();
         renderer.material.mainTexture = background;
 }
Comment
Add comment · Share
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

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

22 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

Related Questions

Correct way to draw on screen for 2D game 1 Answer

Replacing Gui.DrawTexture or gui textures in C#? 0 Answers

How to not see white background on texture 1 Answer

Unity changes pixel colors 2 Answers

Texture Compression format for UWP,Texture 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