- Home /
Create an array of sprites and create them at start.
Hi all! I'm here to ask you how you would achive this thing i want to do. I'm experimenting with the new features of Unity4.3. What i want to do is to take all letters i've inside a folder and create fromt these a keyboard placing them dinamically on the screen. What i did since now is this:
using UnityEngine;
using System.Collections;
public class Letters : MonoBehaviour {
private char[] keyboard = new char[] {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
};
float posX = -4.5F;
float posY = -0.65F;
// Use this for initialization
IEnumerator Start () {
for(int i = 0; i <= keyboard.Length; i++){
posX = posX + 0.65F;
if(posX > 3.5F){
posY = -1.5F;
posX = -4.15F;
}
string path = "file://"+Application.dataPath+"/Tastiera/"+keyboard[i]+".jpg";
SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
Sprite sprite = new Sprite();
WWW www = new WWW(path);
yield return www;
sprite = Sprite.Create(www.texture, new Rect(0, 0, 0, 50),new Vector2(posX, posY),100.0f);
renderer.sprite = sprite;
}
}
// Update is called once per frame
void Update () {}
}
Ofc, it doesn't work, what it do is creating every time a sprite replacing the one it created before. What i tought to solve this was: At start i fill an array with sprites then i iterate on this array and create N istance of these letters.
How can i should do that? thanks a lot.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers
How to assign List>Vector3>[] 2 Answers
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers