load sprite from resources unity code example
Example 1: set sprite unity
public Sprite sprite1;
public Sprite sprite2;
private SpriteRenderer spriteRenderer;
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = sprite1;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
ChangeTheDamnSprite ();
}
}
void ChangeTheDamnSprite ()
{
if (spriteRenderer.sprite == sprite1)
{
spriteRenderer.sprite = sprite2;
}
else
{
spriteRenderer.sprite = sprite1;
}
}
Example 2: unity resources load
var textFile = Resources.Load<TextAsset>("Text/textFile01");
var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
var texture = Resources.Load<Texture2D>("Textures/texture01");
var sprite = Resources.Load<Sprite>("Sprites/sprite01");
var audioClip = Resources.Load<AudioClip>("Audio/audioClip01");
Example 3: unity gameobject sprite set native size in script
TargetsGO.GetComponent<Image>().SetNativeSize();