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

How to assign Sprite from Script?

Discussion in 'UGUI & TextMesh Pro' started by T2Unity, Aug 22, 2014.

  1. T2Unity

    T2Unity

    Joined:
    Aug 21, 2014
    Posts:
    26
    Hi All.

    I`m using Sprite Packer.

    Sprite Packer doesn`t work at Resoureces Folder.
    So We can`t use Resources.Load.

    I want to assign (or load) sprite from Script.
    How to do this?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class LoadSprite : MonoBehaviour {
    6.    private Image m_Image;
    7.    
    8.    [SerializeField] private Sprite[] m_Sprites;
    9.    private int m_CurrentSpriteIndex = 0;
    10.    
    11.    private void Start () {
    12.      m_Image = GetComponent <Image> ();
    13.    }
    14.    
    15.    private void Update () {
    16.      m_CurrentSpriteIndex = m_CurrentSpriteIndex + 1;
    17.      if (m_CurrentSpriteIndex >= m_Sprites.Length) {
    18.        m_CurrentSpriteIndex = 0;
    19.      }
    20.  
    21.      m_Image.sprite = m_Sprites [m_CurrentSpriteIndex];
    22.    }
    23. }
     
  3. T2Unity

    T2Unity

    Joined:
    Aug 21, 2014
    Posts:
    26
    Thank you for reply.

    Now I do such like it.
    But It`s not cool.

    I want to know concept for Sprite Packer.