Skip to content

Instantly share code, notes, and snippets.

@animepauly
Last active August 29, 2015 14:16
Show Gist options
  • Save animepauly/36affad1e91a46ac464f to your computer and use it in GitHub Desktop.
Save animepauly/36affad1e91a46ac464f to your computer and use it in GitHub Desktop.
A simple image loading manager in C# for the Unity StrangeIOC framework. I had to stop getting all images at once as I was running out of threads. This solution is my first attempt
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using strange.extensions.context.api;
using strange.extensions.dispatcher.eventdispatcher.api;
using Parse;
namespace adquizition.quizapp
{
public class LoadImageFromWebService
{
[Inject(ContextKeys.CONTEXT_VIEW)]
public GameObject contextView{get;set;}
[Inject]
public FulfillImageFromWebSignal FulfillImageFromWebSig{get;set;}
[Inject]
public IRoutineRunner RoutineBehavior{get;set;}
private List<ImgDataVO> _imgDataVOQueueList;
private int _imgQueueLimit = 4;
private IEnumerator _loadImageRoutine;
private int _imgsLoadedInt = 0;
public LoadImageFromWebService ()
{
}
public void LoadImage(string __urlStr, Image __image)
{
Debug.Log ("LoadImageFromWebService::LoadImage called..."+__urlStr);
ImgDataVO __imgDataVO = new ImgDataVO();
__imgDataVO.Img = __image;
__imgDataVO.UrlStr = __urlStr;
if (_imgDataVOQueueList == null)
{
_imgDataVOQueueList = new List<ImgDataVO>();
}
_imgDataVOQueueList.Add(__imgDataVO);
//if queue has not reached the limit, load as normal. I am setting the limit very low as I don't know the thread ceiling on mobile
//I know it's more than 2 but keeping it that way for now.
if (_imgDataVOQueueList.Count < _imgQueueLimit)
{
_loadImageRoutine = _loadImage(__imgDataVO);
RoutineBehavior.StartCoroutine(_loadImageRoutine);
}
_loadImageRoutine = _loadImage(__imgDataVO);
RoutineBehavior.StartCoroutine(_loadImageRoutine);
}
private void _loadNextImage()
{
Debug.Log ("LoadImageFromWebService::_loadNextImage called");
//Ensure the img data exists
if (_imgDataVOQueueList.Count > 0 && _imgDataVOQueueList[0] != null)
{
//it does exist, grab it to be passed into the Enumerator
ImgDataVO __imgDataVO = _imgDataVOQueueList[0];
//Remove from list right away
_imgDataVOQueueList.Remove(__imgDataVO);
_loadImageRoutine = _loadImage(__imgDataVO);
RoutineBehavior.StartCoroutine(_loadImageRoutine);
}
}
private IEnumerator _loadImage(ImgDataVO __imgDataVO)
{
Debug.Log ("LoadImageFromWebService::_loadImage called");
WWW www = new WWW(__imgDataVO.UrlStr);
int __triesInt = 0;
// yield return www;
while(!www.isDone)
{
Debug.Log ("Trying to load image "+__imgDataVO.UrlStr+", "+__triesInt);
__triesInt++;
if (__triesInt >= 30)
{
if (_imgDataVOQueueList.Contains(__imgDataVO))
{
_imgDataVOQueueList.Remove(__imgDataVO);
}
RoutineBehavior.RemoveRoutine(_loadImageRoutine);
yield break;
}
else
{
yield return null;
}
}
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogWarning("POSSIBLE ISSUE LOADING IMAGE!");
// yield break;
}
Debug.Log ("LoadImageFromWebService::LoadImage::_loadImage complete! "+__imgDataVO.UrlStr);
//creating the sprite to be sent via signal below
Sprite __sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f));
//incrementing integer so I know how many have loaded before I have to load the next set
_imgsLoadedInt++;
//this is kind of weird here but set up for the imageData that got loaded in right away vs the _loadNextImg call
//Will probably streamline this more later
if (_imgDataVOQueueList.Contains(__imgDataVO))
{
_imgDataVOQueueList.Remove(__imgDataVO);
}
Debug.Log ("_imgsLoadedInt = "+_imgsLoadedInt);
//if images loaded equals queuelimit, I can load the next set. otherwise keep running routine until the previous set is complete
if (_imgsLoadedInt == _imgQueueLimit)
{
_imgsLoadedInt = 0;
for (int __i = 0; __i < _imgQueueLimit; __i++)
{
_loadNextImage();
}
}
//I have a coroutine manager that keeps track of all routines, this doesn't stop the routine at all
RoutineBehavior.RemoveRoutine(_loadImageRoutine);
//send a signal to the view that the sprite is ready
FulfillImageFromWebSig.Dispatch(__sprite, __imgDataVO.Img);
yield break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment