The end goal of this project/platform is to be able to gain full control over the game, both server and clientside. In order to achieve this, we're going to need to be able to load mods a bit more complex than DizzyHack. One on of the main steps here is the ability to load custom Unity assets. Let's jump in and see how we can achieve this using features built in to Unity, and a 3rd party program or two.
Goal 1: Spawn a basic object
In Unity, there are a few basic objects that you can easily create in a few clicks via the editor. These include a cube, a sphere, a cylinder, a sphere, and a plane. These basic objects are called "primitives". In two lines of code, we can create a primitive object (in this case a cube) and place it near to the player:
This is a solid start, but let's see if we can ramp it up a bit; how about streaming a texture off the web?
Goal 2: Stream a texture
This goal, thanks to Unity, ends up being a fairly simple one. Not quite two lines, but near enough. You see, Unity has this great class for dealing with the web, called "WWW". Since it's tied so closely with the engine, it actually lets you stream a .png file off the web and stick in right into a material's texture hole, such as on our cube. Allow me to demonstrate:
The result? A beautiful "earth cam" cube:
Don't ask me why I used this image. It was in Unity's example docs so EarthCam it is.
Goal 3: Stream a mesh
Fuck cubes - let's take this up another level.
Actually, I should probably explain to you what a mesh is: a mesh (in Unity) basically describes how an object is shaped. So if I wanted to create a little character for my game, I'd need to create a mesh. Meshes are then rendered by a component called a "Mesh Renderer" - pretty self-explanatory, right?
Now, back to business. Unfortunately, unlike our textures, Unity doesn't seem to have anything built in for streaming or downloading meshes over the net, however, what I did find was this awesome little set of scripts called MeshSerializer2. These scripts basically allow us to save and load meshes in and out of Unity. For now we're interested in loading into the game, but we'll get into loading out soon (hint hint). I found a mesh of this little character dude hanging around, so we'll see if we can load him up shall we?
Bada-boom, bada-bing, in my game I will find a little alien thing!
Goal 4: Export a mesh from Unity
Remember how I mentioned MeshSerializer had the functionality to save meshes too? Let's give that a go shall we?
The MeshSerializer wiki page also provides a pre-made script for saving a mesh already loaded into Unity. All we do is add it to our object, give it a filename, and hit play:
Easy! Now we have a Ladder.data file that we can load into our game. I'm also going to save the texture, as theoretically we can also load that up using the same technique we used for our EarthCam cube. Now we just stick these files on our web server so the game has somewhere to download them from. Aaaaaand:
Looks like that's it (for now) doesn't it? Next, and one of the most important things to ensure this works well, is going to be finding a good way to store meshes and textures in memory. That way we're not smashing the web server or our potato internet connection. It'll also mean there's no delay when we load in an item.
As usual, I posted a video showing this on my YouTube which you can watch below. I hope you're enjoying the update logs so far and hopefully we can have something in game for you all to play around with soon!