Snippets

David Fraj Crear un sprite en tiempo de ejecucionen Unity

Created by David Fraj
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class canvas : MonoBehaviour {

	//Me creo un entero para saber el numero de cartas
	private int numCartas=0;

	// Use this for initialization
	void Start () {
		numCartas = 0;
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void cambiarCarta(){



		GameObject nueva = new GameObject();
		Image imagen = nueva.AddComponent<Image>(); //Add the Image Component script
		imagen.sprite = Resources.Load<Sprite>("Cartas/103"); //Set the Sprite of the Image Component on the new GameObject

		nueva.GetComponent<RectTransform>().SetParent(gameObject.transform); //Assign the newly created Image GameObject as a Child of the Parent Panel.

		nueva.GetComponent<RectTransform> ().position = new Vector3 (40*numCartas+40,300,0);

		nueva.GetComponent<RectTransform> ().sizeDelta= new Vector2 (90, 130);

		nueva.SetActive(true); //Activate the GameObject

		numCartas++;

	}
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.