Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprite.Create not working.

Discussion in 'Editor & General Support' started by Astrosaur, Nov 20, 2013.

  1. Astrosaur

    Astrosaur

    Joined:
    Nov 20, 2013
    Posts:
    11
    Neither of these do anything. Nothing is added to the hierarchy. I'm sure the code is being run. New to 2d and the documentation seems to be limited right now, so I hope someone can help.

    Code (csharp):
    1.  
    2.         public Texture2D[] heads;
    3.         public Texture2D head1;
    4.    
    5.     void Awake ()
    6.     {
    7.         //trial 1
    8.         Sprite.Create(heads[Random.Range(0,2)],new Rect(0,0,400,400), new Vector2(0,0), 0.1f);
    9.         //trial 2
    10.         Sprite.Create(head1,new Rect(0,0,400,400), new Vector2(0,0), 0.1f);
    11.         print ("head created");
    12.     }
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Sprite.Create doesn't add anything to the hierarchy, it just creates a Sprite (which is a logical object only). You have to assign the sprite to a SpriteRenderer.sprite

    GetComponent<SpriteRenderer>.sprite = Sprite.Create(...)
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,397
    You probably just want to use a public Sprite variable, and assign that to a GameObject which has a SpriteRenderer component.

    --Eric
     
  4. Astrosaur

    Astrosaur

    Joined:
    Nov 20, 2013
    Posts:
    11
    Can you show me the code to do that Eric?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,397
    Code (csharp):
    1. public Sprite spriteImage;
    2.  
    3. void Start () {
    4.     new GameObject("MySprite").AddComponent<SpriteRenderer>().sprite = spriteImage;
    5. }
    --Eric
     
  6. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76
    Hi Guys

    When I create Sprite using Sprite.Create() it's genarte wrong sprite from Image.
    Code (csharp):
    1. LiveIcon
    is
    Code (csharp):
    1. Texture2D
    object and read texture from Game Cache path
    Code (CSharp):
    1. GameIcon.sprite=Sprite.Create(LiveIcon,newRect(0,0,LiveIcon.width,LiveIcon.height),newVector2(0f,1f),100);
    Actual Texture is like "Maths_Mania_2_1436427298.png" and result of the code is "Screen Shot 2015-07-10 at 5.00.39 pm.png"
     

    Attached Files:

  7. Suguma

    Suguma

    Joined:
    May 29, 2015
    Posts:
    26
    I am having the exact same problem as PAHeartBeat mentioned above. My sprite comes out exactly the same way as his attached files, and I am using Sprite.Create with the same parameters.

    I first encountered this at Unity 5.0.2f. When I upgraded to 5.2, it disappeared in the Editor, but not in my iOS Build. As the unity version influenced on the result, I can only assume this is a bug. Has anyone encountered this problem?

    @PAHeartBeat, did you manage to fix it?
     
  8. PAHeartBeat

    PAHeartBeat

    Joined:
    Jul 11, 2012
    Posts:
    76

    yes I find the solution or that one. it was little tricky in my case. Here is the solution for that.

    Code (csharp):
    1.     public Texture2D DefaultIcon = null;
    2.     private Texture2D LiveIcon = null;
    3.     public void ChangeIcon() {
    4.             if(IO.File.FileExists(<<PATH_OF_IMAGE>>)) {
    5.                 LiveIcon = LoadTextrure(<<PATH_OF_IMAGE>>);
    6.  
    7.             }
    8.             if(LiveIcon != null) {
    9.                 GameIcon.maskable = true;
    10.                 GameIcon.sprite = Sprite.Create(LiveIcon, new Rect(0, 0, LiveIcon.width, LiveIcon.height),
    11.                     new Vector2(0.5f, 0.5f), 100, (uint)0);
    12.             } else {
    13.                 GameIcon.sprite = DefaultIcon;
    14.             }
    15.         }
    16.     }
    you have to give exact size of image to create sprite from it. other wise it will create nearest PowerOf2(POT) size sprite and it will create wrong sprite. The issue will only occur with NPOT size image.

    edit: forget the add actual trick which hidden when I load texture from disk.

    Here is method of load Texture from image. The trick is here in this code to get proper texture. First load texture from file bytes array then read image height and width and then reload it with identified size value and provide High Quality Image Setting via `TextureFormat.RGBA31`


    Code (csharp):
    1.    protected internal static Texture2D LoadTextrure(string fileName) {
    2.             return LoadTextrure(string.Empty, fileName);
    3.         }
    4.         protected internal static Texture2D LoadTextrure(string path, string fileName) {
    5.             Texture2D tex = new Texture2D(0, 0);
    6.             //texTexture2D finalTex;
    7.  
    8.             string dataPath = path + fileName;
    9.             byte[] imageData;
    10.             try {
    11.                 if(System.IO.File.Exists(dataPath)) {
    12.                     imageData = System.IO.File.ReadAllBytes(dataPath);
    13.                     tex.LoadImage(imageData);
    14.                     tex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
    15.                     tex.LoadImage(imageData);
    16.                 } else {
    17.                     tex = null;
    18.                 }
    19.             } catch(System.Exception ex) {
    20.                 Debug.LogError(ex.Message);
    21.                 tex = null;
    22.             }
    23.             return tex;
    24.         }
     
    Last edited: Sep 19, 2015
  9. EricNumeri4D

    EricNumeri4D

    Joined:
    Sep 24, 2013
    Posts:
    4
    Thanks for the trick, it's always working on Unity 4.6.8f1.
    When using Sprite.Create we have to load a texture at the exact size of the texture loaded or it does not work as expected !

    Thanks again
     
  10. Suguma

    Suguma

    Joined:
    May 29, 2015
    Posts:
    26
    I just came upon this issue again and managed to solve it quite easily. Previously, all the solutions, including loading the texture at the exact size did not work for me.
    My solution was to disable mipmaps for the new texture, which is what @PAHeartBeat did in his code, so maybe that's why it worked...?
    Anyway, mipmap false.