Created
January 13, 2018 15:26
-
-
Save blue0513/68a5de12a973efa648b06cfbe3182dc3 to your computer and use it in GitHub Desktop.
Load Images Sequentially for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// Load Images from start to end | |
public class ImagesLoader : MonoBehaviour { | |
// index for images | |
private int imageIndex=0; | |
// number of images to load | |
private int numberOfImages; | |
private int unloadCounter; | |
void Update() { | |
resourcesUnloadIfNeeded(); | |
} | |
private void resourcesUnloadIfNeeded() { | |
if(unloadCounter > 5) { | |
unloadCounter = 0; | |
Resources.UnloadUnusedAssets(); | |
} | |
} | |
private WWW basicWWW (string loadPath) { | |
// for iPad, iPhone | |
return new WWW ("file://" + Application.streamingAssetsPath + "/" + loadPath); | |
} | |
private IEnumerator _loadPicture(GameObject obj, string name, float delay) { | |
yield return new WaitForSeconds(delay); | |
using (WWW www = basicWWW (name)) { | |
yield return www; | |
obj.GetComponent<Renderer> ().material.mainTexture = www.textureNonReadable; | |
obj.GetComponent<Renderer> ().enabled = true; | |
} | |
} | |
// Load pictures sequentially (by auto) | |
private IEnumerator _changePictures(GameObject obj, int numberOfImages, float interval) { | |
for (int i=0; i<numberOfImages; i++) { | |
loadPicture(obj, i); | |
yield return new WaitForSeconds(interval); | |
} | |
} | |
// Load picture and Set it to "obj" | |
public void loadPicture(GameObject obj, float delay=0f) { | |
if(imageIndex >= numberOfImages-1) return; | |
string name = imageIndex.ToString("00") + ".jpg"; // name for images | |
StartCoroutine(_loadPicture(obj, name, delay)); | |
// inc index for the next image to load | |
imageIndex++; | |
unloadCounter++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment