Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to draw dynamic sprites at the fixed position in the Unity 5?

Discussion in 'Scripting' started by Albeoris, Jul 5, 2015.

  1. Albeoris

    Albeoris

    Joined:
    Oct 1, 2013
    Posts:
    11
    Hello ladies and gentelemens!

    I am used TexturePacker to creating of texture atlases.
    It's making a sprite collection after import to the Unity.

    Questions:
    1. How to dynamically create these spites at the scene? (For expample: 4 same potions in the inventory)
    2. How to draw these sprites at the fixed position of scene? (For example: animated face of a hero)

    Thank you for your attention.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I would make a prefab that has all the settings you want, with a placeholder sprite. The use Instantiate to add the prefab to the scene and finally change the sprite to the correct sprite. For example:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AddSprite : MonoBehaviour {
    5.     public GameObject myPrefab
    6.    
    7.     void AddNewSprite(Vector2 position, sprite newSprite) {
    8.         GameObject go = Instantiate(myPrefab, position, Quaternion.identity) as GameObject;
    9.  
    10.         SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
    11.  
    12.         sr.sprite = newSprite;
    13.     }
    14. }
    15.  
     
  3. Albeoris

    Albeoris

    Joined:
    Oct 1, 2013
    Posts:
    11
    Thank you!