Unity + ngui create a tool class for asynchronous loading of network images and local cache (1)

Source: Internet
Author: User

In mobile development, Asynchronous Network image loading is often used. Although assetbundle exists in unity, game resources are loaded first before entering the scenario, however, Asynchronous Network image loading and caching mechanisms can be used in many other places.

I have also written about the Asynchronous Network image loading helper class in two versions of IOS. So today I want to use the helper class as well as the implementation principle.

First, we need to load a network image. The steps are as follows:

0. Set a fixed image as the occupied Bitmap (placeholder) before the start, indicating that our image has not been loaded, to fill the current image control area, let users know

1. This image has a URL address. When our program loads the image of this URL address for the first time

A. Download the image asynchronously.

B. Save it to a specified directory.

C. Replace the bitmap on the image control with the downloaded image. You can add an animation for switching the image)

2. Our program has loaded the image of this URL address

A. Read the file from the last saved directory and convert it to an image.

B. Replace the bitmap on the image control with the downloaded image.



In general, our program should be the first time we load a batch of network images, which of the following images are needed and then displayed, yes, it can be read directly from the file system (the third case is to read directly from the memory, depending on the device configuration, this will not be done here)


Before starting this tool class, you must first know some special restrictions on Unity:

1. startcoroutine, a member method of the monobehaviour base class, cannot be called in static methods to enable asynchronous tasks.

2. the subclass of the monobehaviour base class cannot be directly created using the new keyword. Accordingly, an empty gameobject needs to be created and the addcomponent method of this object is called for instantiation, that is to say, all Script objects in unity can be executed only when they are attached to Game objects.

3. Because of the cross-platform feature of unity, the file directory structure varies with different platforms.


Based on some of the above special features, I plan to make this tool Class A subclass of monobehaviour and let other scripts call it in the form of a singleton. The Asynchronous Network request can use the startcoroutine function.

The first is the implementation method of this singleton:


Using unityengine; using system. collections; using system. io; public class asyncimagedownload: monobehaviour {public texture placeholder; public static asyncimagedownload instance = NULL; private string Path = application. persistentdatapath + "/imagecache/"; // construct the singleton public static asyncimagedownload createsingleton () {If (! Directory. exists (application. persistentdatapath + "/imagecache/") {directory. createdirectory (application. persistentdatapath + "/imagecache/");} gameobject OBJ = new gameobject (); obj. addcomponent <asyncimagedownload> (); asyncimagedownload loader = obj. getcomponent <asyncimagedownload> (); instance = loader; loader. placeholder = resources. load ("Placeholder") as texture; return loader ;}


The constructor is not used to create a singleton. The reason is that the sub-class of monobehaviour does not support the new keyword at all. So how can I write the constructor?

Then the method for building the Singleton is similar to the constructor, but not the new method, but the class name. method Name () to create this singleton. When creating a Singleton, you need to create a blank game body to attach our script component, and point the static member instance to the created singleton. You only need to use the class name when using this Singleton multiple times. instance () to obtain the existing script component Singleton.

Resources. load ("placeholder") You need to note that you need to use code to read the image resources in the project directory and convert them to texutre objects. You need to create the resources folder in assets in the project window, then, import any image and get it in the code without the suffix.


After this script is written, we should execute the following code during program initialization to create this singleton:

AsyncImageDownload.CreateSingleton()

After loading a network image for a widget, we should add a public void setasyncimage (string URL, uitexture texture) method to this Singleton, so the simplified writing can be written as follows:

AsyncImageDownload.Instance.SetAsyncImage ("http://www.cfanz.cn/uploads/jpg/2013/07/13/0/XEPLd7d2C5.jpg", page.GetComponentInChildren<UITexture> ());

 



This method will be completed in the second part of this blog

Unity + ngui create a tool class for asynchronous loading of network images and local cache (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.