Wanzyee Studio

Basically a wrapper of UnityEngine.WWW for work easier. More...

Inherits CustomYieldInstruction.

Public Member Functions

void Cancel ()
 Cancel the loading process, only effective before done. More...
 
override string ToString ()
 Return the info. More...
 

Static Public Member Functions

static void ClearCache (bool ancestor=false)
 Delete the cache folder contains all cache files if cacheable. More...
 
static void DeleteCache (WwwOption option)
 Delete the cache file by corresponding URL or WwwOption if cacheable. More...
 
static FileInfo RetrieveCache (WwwOption option)
 Retrieve the cache file by corresponding URL or WwwOption if cacheable. More...
 
static WwwLoader Post (WwwOption option, Action< WwwLoader > handler=null)
 Post data to specified URL or WwwOption. More...
 
static WwwLoader LoadBytes (WwwOption option, Action< WwwLoader, byte[]> handler)
 Load bytes data from specified URL or WwwOption. More...
 
static WwwLoader LoadText (WwwOption option, Action< WwwLoader, string > handler)
 Load a text data from specified URL or WwwOption. More...
 
static WwwLoader LoadAsset (WwwOption option, Action< WwwLoader, AssetBundle > handler, bool unload=true)
 Load an UnityEngine.AssetBundle from specified URL or WwwOption. More...
 
static WwwLoader LoadAudio (WwwOption option, Action< WwwLoader, AudioClip > handler)
 Load an UnityEngine.AudioClip from specified URL or WwwOption. More...
 
static WwwLoader LoadImage (WwwOption option, Texture2D texture, Action< WwwLoader, Texture2D > handler=null)
 Load a UnityEngine.Texture2D from specified URL or WwwOption. More...
 
static WwwLoader LoadMovie (WwwOption option, Action< WwwLoader, MovieTexture > handler)
 Load a UnityEngine.MovieTexture from specified URL or WwwOption. More...
 

Public Attributes

override bool keepWaiting => !isDone
 Indicates if coroutine should be kept suspended. More...
 

Properties

static bool isCacheable [get]
 Determine if is cache supported, i.e., the platform is not WebGL. More...
 
static string cacheFolder [get, set]
 The folder to save cache files. More...
 
string url [get]
 The full URL passed to UnityEngine.WWW. More...
 
string info [get]
 The info describes the used URL or WwwOption. More...
 
DateTime time [get]
 The time when loading started. More...
 
float duration [get]
 The duration whole process cost in seconds, set when done. More...
 
float progress [get]
 The progress of UnityEngine.WWW, between 0~1. More...
 
string error [get]
 The error message of UnityEngine.WWW or loading canceled. More...
 
bool isDone [get]
 Flag if loading process done. More...
 

Detailed Description

Basically a wrapper of UnityEngine.WWW for work easier.

Provide simple API to implement Fire'n'Forget by callback when loading process done. This invokes the handler even process failed, check WwwLoader.error before using loaded data in case. The instance returned by the loading methods is the same as the one passed to the callback for convenience. This derives from UnityEngine.CustomYieldInstruction that you can yield return it in a coroutine in case.

This can cache loaded data to local file system, but situations below is not cacheable:

  1. Web player or WebGL, not supported to save local file.
  2. When posting bytes data to a web server, needless in common and hard to track.
  3. When loading a local file, needless since the cache is also local.

An example to load image into an existed UnityEngine.Texture2D below:

WwwLoader.LoadImage(
"http://some.where/photo.png",
GetComponent<Renderer>().material.mainTexture as Texture2D
);

Or to load a local JSON file to setup your class:

WwwLoader.LoadText(
"../folder/doc.json",
(loader, text) => { if(null == loader.error) doc = JsonConvert.DeserializeObject<Doc>(text); }
);

Member Function Documentation

static void ClearCache ( bool  ancestor = false)
static

Delete the cache folder contains all cache files if cacheable.

Only valid when the cacheFolder isn't drive root. Optional to clear ancestor folders if empty.

Parameters
ancestorIf set to true clear ancestor folders if empty.
static void DeleteCache ( WwwOption  option)
static

Delete the cache file by corresponding URL or WwwOption if cacheable.

Parameters
optionOption.
static FileInfo RetrieveCache ( WwwOption  option)
static

Retrieve the cache file by corresponding URL or WwwOption if cacheable.

Return the FileInfo no matter file exists or not, check it first in case. Or return null if not cacheable. The method LoadBytes() is enough in common raw data usage. The purpose of this is for some plugin usage of file path in case.

Parameters
optionOption.
Returns
The cache FileInfo.
static WwwLoader Post ( WwwOption  option,
Action< WwwLoader handler = null 
)
static

Post data to specified URL or WwwOption.

Parameters
optionOption.
handlerCallback handler.
Returns
The instance of WwwLoader.
static WwwLoader LoadBytes ( WwwOption  option,
Action< WwwLoader, byte[]>  handler 
)
static

Load bytes data from specified URL or WwwOption.

Parameters
optionOption.
handlerCallback handler.
Returns
The instance of WwwLoader.
static WwwLoader LoadText ( WwwOption  option,
Action< WwwLoader, string >  handler 
)
static

Load a text data from specified URL or WwwOption.

Parameters
optionOption.
handlerCallback handler.
Returns
The instance of WwwLoader.
static WwwLoader LoadAsset ( WwwOption  option,
Action< WwwLoader, AssetBundle >  handler,
bool  unload = true 
)
static

Load an UnityEngine.AssetBundle from specified URL or WwwOption.

Optional to unload after invoked, as UnityEngine.AssetBundle manual recommends.

Parameters
optionOption.
handlerCallback handler.
unloadIf set to true unload UnityEngine.AssetBundle.
Returns
The instance of WwwLoader.
static WwwLoader LoadAudio ( WwwOption  option,
Action< WwwLoader, AudioClip >  handler 
)
static

Load an UnityEngine.AudioClip from specified URL or WwwOption.

Note, URL must end with a file extension, that unity depends on to determine audio format.

Parameters
optionOption.
handlerCallback handler.
Returns
The instance of WwwLoader.
static WwwLoader LoadImage ( WwwOption  option,
Texture2D  texture,
Action< WwwLoader, Texture2D >  handler = null 
)
static

Load a UnityEngine.Texture2D from specified URL or WwwOption.

Optional to assign an existing texture to load the data into it without the handler. Or only assign the handler just like other load methods. If both are assigned, the texture will be passed to the handler.

Parameters
optionOption.
textureTexture.
handlerCallback handler.
Returns
The instance of WwwLoader.
static WwwLoader LoadMovie ( WwwOption  option,
Action< WwwLoader, MovieTexture >  handler 
)
static

Load a UnityEngine.MovieTexture from specified URL or WwwOption.

Parameters
optionOption.
handlerCallback handler.
Returns
The instance of WwwLoader.
void Cancel ( )

Cancel the loading process, only effective before done.

override string ToString ( )

Return the info.

Returns
A string represents this.

Member Data Documentation

override bool keepWaiting => !isDone

Indicates if coroutine should be kept suspended.

Property Documentation

bool isCacheable
staticget

Determine if is cache supported, i.e., the platform is not WebGL.

string cacheFolder
staticgetset

The folder to save cache files.

It's recommended to set custom folder when initializing, instead while loading. Set empty to use default, a sub folder under Application.temporaryCachePath.

string url
get

The full URL passed to UnityEngine.WWW.

This might be different from which assigned to load method. E.g., when redirect to cache file, or become full path with protocol for a local file path.

string info
get

The info describes the used URL or WwwOption.

Used to identify loader. The URL part targets the same as original, but format might be modified.

DateTime time
get

The time when loading started.

float duration
get

The duration whole process cost in seconds, set when done.

float progress
get

The progress of UnityEngine.WWW, between 0~1.

This only for inspecting, not guarantees success, check error in need.

string error
get

The error message of UnityEngine.WWW or loading canceled.

bool isDone
get

Flag if loading process done.

This doesn't guarantee process success, check error in need.