I spent the first part of this week working on a tool to let my game connect with the internet with less hassle. The UnityEngine lets us contact the internet using a WWWForm and either a WWW object or a UnityWebRequest, depending on the version. I found that I was writing a lot of the same code for each request. I wanted a set of options that I could define and would be used by all requests – unless specified otherwise. I also wanted to send the request in a couple of lines and have functions that would be called when the request finished.
The AdvancedLoader inherits the UnityEngine.Object so it can be created and destroyed in scripts unlike a MonoBehaviour which must be constructed with an AddComponent call. This means that each AdvancedLoader must be able to contact a MonoBehaviour to run it’s coroutines. As overused as singletons are in Unity, it seems the perfect opportunity. The AdvancedLoader will create a singleton to run it’s coroutines if needed.
I also found myself wishing that UnityWebRequest’s cache operations allowed an attempt to the server first, and only if an error occurred, using the cache second. This was easy enough to implement, once the proper error handling was implemented. I also added a timeout module for cases on mobile, when the cellular data is not fast enough to contact the server. This produces it’s own error and the number of attempts and duration of attempts is customizable and separate from other loaders.
I also incorporated the post data, used in the same way as the Unity solution, but handled internally in the AdvancedLoader. This just helped to clean up the network code in the game class. It prevented me from having to worry about the post data, knowing that the AdvancedLoader would handle it properly.
I’m using Xml as my formatting when transmitting data from the server. It frustrated me that I had to convert the text to Xml every time the loader came back. Now the AdvancedLoader will return the data as Xml if it is requested as Xml. My OnGotData function expects an XmlNode instead of a string, letting me perform the game level operations without having to convert text to Xml.
I have personal data on my game’s server, I would be best if the sources could be verified. Currently sources are verified by using md5 encryption, and can be authorised on a game by game basis.
There are some improvements I’ve thought when deciding to release the AdvancedLoader source. The first of which is a PreLoad method. One loader fetches a batch of data from the server, the data is then split up and saved to the cache under given urls using the AdvancedLoader’s method, meaning that should a call be made to a PreLoaded url, the AdvancedLoader can use the cached version. This is to prevent overloading the local network when PreLoading lots of AdvancedLoader calls. The second is a concurrent loader manager, to circumvent the same problem only a set number of AdvancedLoaders will be active at any one time, the rest will be in a queue.
While this package is pending approval on the Unity Asset Store you can download it here. If you’ve any other suggestions for improvements, please let me know.