Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Can't update sprite texture after fusing few images to create it.

Discussion in 'Scripting' started by VipekGuy, Feb 11, 2019.

  1. VipekGuy

    VipekGuy

    Joined:
    Sep 14, 2018
    Posts:
    7
    I am trying to fuse few sprites, then update game objects sprite, but it gives me same old (blank) sprite.
    Any ideas? Am I doing it in a completely strange way?

    Edit: to clarify I want to overlap textures into one, new. They have a lot of "holes", so they won't overlap just connect nicely and I will be able to change parts from code.
    I think I'll just try to do it different way, without merging them.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SpriteMerge : MonoBehaviour
    4. {
    5.  
    6.     public Sprite[] images;
    7.     public Sprite characterSprite;
    8.     public GameObject character;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         foreach (Sprite image in images)
    14.         {
    15.             for (int w = 0; w < image.texture.width; w++)
    16.             {
    17.                 for (int h = 0; h < image.texture.height; h++)
    18.                 {
    19.                     Color color = image.texture.GetPixel(w, h);
    20.                     characterSprite.texture.SetPixel(w, h, color);
    21.                 }
    22.             }
    23.         }
    24.         characterSprite.texture.Apply();
    25.     }
    26. }
     
    Last edited: Feb 12, 2019 at 10:40 AM
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,056
    you are repeatedly writing over the same area of the sprite. You need to setpixel to a different area for each image

    Maybe use Texture2D.PackTextures ?
     
  3. VipekGuy

    VipekGuy

    Joined:
    Sep 14, 2018
    Posts:
    7
    What do you mean? I am changing coords with in loop variables w and h, then using them to read pixel from old sprites and setting them into new one. Both are the same size.

    Will check out this PackTextures tho.

    Edit: Edited post maybe it will clarify my problem. I think PackTextures won't work here.
     
    Last edited: Feb 12, 2019 at 10:39 AM
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,056
    for the first loop , it GETs pixels from 0 -> image.width, then SETs pixels onto characterSprite for the same w and h values (0 -> image.width)
    Then for the second image, it GETs from 0 -> image(2).width, and SETs (again) at 0 -> to image(2).width, overwriting the first images pixels
     
  5. VipekGuy

    VipekGuy

    Joined:
    Sep 14, 2018
    Posts:
    7
    No, it gets 1st image gets it's lowest, 1st left pixel, then higher one... when it gets the highest it goes to the 2nd left and start from lowest. After whole image it goes to the next one and do on.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,056
    yes, after the first image it goes to the next image, and w and h RESTART FROM 0 for the next image
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,184
    An image editor like photoshop or gimp is the normal way.
     
  8. VipekGuy

    VipekGuy

    Joined:
    Sep 14, 2018
    Posts:
    7
    Yeah I want them to stack on each other and ''fill the holes" from the last one.
    I needed to make one s from elements that will change depending on player's choice.
    I figured different way tho. Hope it works.