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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating a sprite from Code

Discussion in '2D' started by CalaveraX, Feb 10, 2015.

  1. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Hi Everyone!

    i was just wondering.... how can i create a new sprite and point it to a "Unity Sprite Packed Atlas" sprite ?

    All of my sprites are not inside the resources folder, so i dont have the textures duplicated, and i'm using the integrated sprite packer of unity.

    I need to create a new sprite from a c# script, but i'm not sure about how to get the reference to a certain sprite...

    Any hints on this??

    Thanks in advance!
     
  2. Cinema

    Cinema

    Joined:
    Sep 19, 2013
    Posts:
    18
    You can instantiate a prefab by using:
    Code (csharp):
    1.  
    2. Instantiate(Object original, Vector3 position, Quaternion rotation);
    3.  
     
  3. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Thanks Cinema, but that's not what i'm looking for, i dont need to instantiate any object, i need to get the reference to a sprite packed inside a unity sprite packer generated atlas.

    Cheers!
     
  4. Kafeen

    Kafeen

    Joined:
    Aug 5, 2011
    Posts:
    26
    If you have your resources imported as sprites
    Code (csharp):
    1.  
    2. var gameObject = new GameObject ();
    3. var spriteRenderer = gameObject.AddComponent<SpriteRenderer> ();
    4. var sprite = Resources.Load<Sprite> ("Sprites/enemy-big");
    5. spriteRenderer.sprite = sprite;
    6.  
    If you have your resources imported or loaded as textures
    Code (csharp):
    1.  
    2. var gameObject = new GameObject ();
    3. var spriteRenderer = gameObject.AddComponent<SpriteRenderer> ();
    4. var texture = Resources.Load<Texture2D>("Sprites/power-up");
    5. var sprite = Sprite.Create(texture, new Rect(0, 0, 32, 32), new Vector2(16,16));
    6. spriteRenderer.sprite = sprite;
    7.  
    or obviously you can also set your resources through the inspector rather than using Resources.Load().
     
    PersianKiller likes this.
  5. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    That's the problem.

    As i said on the first post, my textures are not in the Resources Folder to avoid getting my resources duplicated (If you have your textures on resources folder, and you still pack them on atlas you will end having in your compilated game all of the generated atlases PLUS the cutted off original sprites)
    so, to avoid this you dont put the sprites on resources folders if you are going to use the unity atlas "Sprite Packer"

    what i'm willing to do, is find out how to create a sprite from that "Packed Atlas"

    Cheers!
     
  6. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    Anyone had trouble with this?

    I'm Still struggling on how to get a sprite packed by the unity integraded sprite packer... :(