如何用程序将同一精灵多次放在一起
本文关键字:在一起 精灵 何用 程序 | 更新日期: 2023-09-27 18:22:17
我正试图在场景中仅使用一个精灵生成多个精灵。当我尝试使用精灵数组和精灵渲染器时,我发现这很困难,因为在我的场景中,我只看到一个精灵。我也想在我的统一场景中的不同区域显示精灵。
主要代码:
using UnityEngine;
using System.Collections;
public class SampleCode : MonoBehaviour {
float posX = -4.5F;
float posY = -0.65F;
public IEnumerator Start(){
for(int i = 0; i < 5; i++){
posX = posX + 0.65F;
if(posX > 3.5F){
for(float x = 0.50F; x < 2.5F; x = x + 0.50F){
posY = -1.5F + x;
posX = -4.15F + x;;
}
}
string path = "file:///C:/Users/Pankaj Sharma/Documents/untitled.bmp";
SpriteRenderer rend = this.GetComponent<SpriteRenderer>();
Sprite sprite = new Sprite();
WWW www = new WWW(path);
yield return www;
sprite = Sprite.Create(www.texture, new Rect(0, 0, 50, 50),new Vector2(posX, posY),100.0f);
rend.sprite = sprite;
}
}
}
您只有一个SpriteRenderer
,这就是为什么您只能看到一个精灵。
如果场景中有多个带有SpriteRenderer
组件的对象,则可以将场景中所需对象的sprite
设置为GetComponent<SpriteRenderer>().sprite
。
这对你来说有意义吗?
您实际上在代码中,忘记了更改它们的位置。所以他们在一起。
你改变开始方法的位置,最终,x和y位置只是最终计算环路的5倍*0.65..
把所有的都放在那里
尝试将create方法放入for循环中,在该循环中可以计算posx-posy。