一次仅实例化 3 个数组对象的 For 循环代码的开发
Development of a For-Loop Code to Instantiate only 3 Array Objects at a time
所以我有一些代码来创建一个动态画廊,它创建一个文件位置的主数组,一个加载器协程将图像加载到纹理中,以及一个函数来实例化一个采用纹理的预制件,添加它作为精灵,然后相应地为预制件设置父级。最后,for 循环会重复此过程,直到我 运行 遍历整个文件数组并用所有图像填充我的图库。这适用于小型画廊,但随着我的画廊的增长,它会变得笨拙并开始占用大量内存。我的问题是如何修改此代码以一次仅加载 3 张图像(左-中-右)?我将所有图像都放在一个滚动容器中,我希望能够知道图像何时滑出屏幕作为加载下一个(或上一个)的信号。有没有人这样做过,不介意分享一点代码?
public Texture2D tex;
public string[] galleryImages;
GameObject galleryThumbHolder;
string[] arctopithecusImages;
string[] arctopithecusPNGImages;
string[] gulonImages;
string[] scythianWolfImages;
string[] simivulpaImages;
string[] succorathImages;
string[] tatusImages;
int currentIndex = 0;
// Create a master Array of all image files located in all Image locations
void Start()
{
// Build Gallery Arrays
arctopithecusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg");
arctopithecusPNGImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.png");
gulonImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/GULON/", "*.jpg");
scythianWolfImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SCYTHIAN-WOLF/", "*.png");
simivulpaImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SIMIVULPA/", "*.png");
succorathImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SUCCORATH/", "*.png");
tatusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/TATUS/", "*.png");
// Concatenate all Folder Array into single Array
galleryImages =
arctopithecusImages.Concat(arctopithecusPNGImages)
.Concat(gulonImages)
.Concat(scythianWolfImages)
.Concat(simivulpaImages)
.Concat(succorathImages)
.Concat(tatusImages)
.ToArray();
for(int i = 0; i < galleryImages.Length; i++)
{
StartCoroutine("loader", currentIndex);
currentIndex++;
}
}
IEnumerator loader(int indexNum)
{
WWW www = new WWW("file://" + galleryImages[indexNum]); // get the first file from disk
yield return www; // Wait unill its loaded
tex = new Texture2D(512,512); // create a new Texture2D
www.LoadImageIntoTexture(tex); // put the image file into the new Texture2D
createGalleryImages(tex);
}
public void createGalleryImages(Texture2D tex)
{
// Instantiate Gallery Thumb Prefab and Load in Sprite
//GameObject galleryThumb = Instantiate(Resources.Load("Prefabs/GalleryImageHolder")) as GameObject;
GameObject galleryThumb = Instantiate(GameObject.FindGameObjectWithTag("GalleryImgHolder")) as GameObject;
Image galleryImg = galleryThumb.GetComponent<Image>();
Rect rct = new Rect(0, 0, tex.width, tex.height); // Define Rect arg
Vector2 pvt = new Vector2(0.5f, 0.5f); // Define Pivot arg
galleryImg.sprite = Sprite.Create(tex, rct, pvt);
// Set Gallery Thumb Parent
galleryThumb.transform.SetParent(GameObject.FindGameObjectWithTag("GalleryThumbs").transform);
}
我曾经做过类似的事情,我实现它的方式是使用触发器,它检测进入的对象并获取分配给该对象的索引,在这种情况下可能是数组中图像的索引,这样你就可以知道哪个是你在中心显示的当前图像,并且可以在用户向左或向右滚动时加载另一个图像。
希望对您有所帮助。
所以我有一些代码来创建一个动态画廊,它创建一个文件位置的主数组,一个加载器协程将图像加载到纹理中,以及一个函数来实例化一个采用纹理的预制件,添加它作为精灵,然后相应地为预制件设置父级。最后,for 循环会重复此过程,直到我 运行 遍历整个文件数组并用所有图像填充我的图库。这适用于小型画廊,但随着我的画廊的增长,它会变得笨拙并开始占用大量内存。我的问题是如何修改此代码以一次仅加载 3 张图像(左-中-右)?我将所有图像都放在一个滚动容器中,我希望能够知道图像何时滑出屏幕作为加载下一个(或上一个)的信号。有没有人这样做过,不介意分享一点代码?
public Texture2D tex;
public string[] galleryImages;
GameObject galleryThumbHolder;
string[] arctopithecusImages;
string[] arctopithecusPNGImages;
string[] gulonImages;
string[] scythianWolfImages;
string[] simivulpaImages;
string[] succorathImages;
string[] tatusImages;
int currentIndex = 0;
// Create a master Array of all image files located in all Image locations
void Start()
{
// Build Gallery Arrays
arctopithecusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg");
arctopithecusPNGImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.png");
gulonImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/GULON/", "*.jpg");
scythianWolfImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SCYTHIAN-WOLF/", "*.png");
simivulpaImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SIMIVULPA/", "*.png");
succorathImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SUCCORATH/", "*.png");
tatusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/TATUS/", "*.png");
// Concatenate all Folder Array into single Array
galleryImages =
arctopithecusImages.Concat(arctopithecusPNGImages)
.Concat(gulonImages)
.Concat(scythianWolfImages)
.Concat(simivulpaImages)
.Concat(succorathImages)
.Concat(tatusImages)
.ToArray();
for(int i = 0; i < galleryImages.Length; i++)
{
StartCoroutine("loader", currentIndex);
currentIndex++;
}
}
IEnumerator loader(int indexNum)
{
WWW www = new WWW("file://" + galleryImages[indexNum]); // get the first file from disk
yield return www; // Wait unill its loaded
tex = new Texture2D(512,512); // create a new Texture2D
www.LoadImageIntoTexture(tex); // put the image file into the new Texture2D
createGalleryImages(tex);
}
public void createGalleryImages(Texture2D tex)
{
// Instantiate Gallery Thumb Prefab and Load in Sprite
//GameObject galleryThumb = Instantiate(Resources.Load("Prefabs/GalleryImageHolder")) as GameObject;
GameObject galleryThumb = Instantiate(GameObject.FindGameObjectWithTag("GalleryImgHolder")) as GameObject;
Image galleryImg = galleryThumb.GetComponent<Image>();
Rect rct = new Rect(0, 0, tex.width, tex.height); // Define Rect arg
Vector2 pvt = new Vector2(0.5f, 0.5f); // Define Pivot arg
galleryImg.sprite = Sprite.Create(tex, rct, pvt);
// Set Gallery Thumb Parent
galleryThumb.transform.SetParent(GameObject.FindGameObjectWithTag("GalleryThumbs").transform);
}
我曾经做过类似的事情,我实现它的方式是使用触发器,它检测进入的对象并获取分配给该对象的索引,在这种情况下可能是数组中图像的索引,这样你就可以知道哪个是你在中心显示的当前图像,并且可以在用户向左或向右滚动时加载另一个图像。 希望对您有所帮助。