Monday, May 11, 2015
A problem with Unity3D and downloading/caching AssetBundles
Anyone who has struggled with Unity’s asset bundle solution may have faced these issues, and hopefully can provide a solution. Here is what I want out of the APIs surrounding Unity’s asset bundle system, specifically, the discrete abilities to:
- Download an asset bundle
- Load an asset bundle
- Cache an asset bundle
- Check if an asset bundle is cached
But instead, as is all too common with Unity’s APIs, we get bits and pieces of this hobbled together in a way that seems to assume a very specific (and often not ideal) workflow. Here’s essentially what Unity offers via API:
- Load an asset bundle from cache, or download it if it’s not in the cache and be forced to load it if you want to use it
- Download an asset bundle but don’t cache it
- Check if an asset bundle is cached
Long story short, I cannot do what I need to do with these APIs. So instead I need to create a convoluted façade over them which results in much more work than should be required. Further adding a [very large] wrinkle to the problem is that the loading of an asset bundle is intimately tied to the download operation; namely, if I want to use an asset bundle that I’ve just downloaded, I need to load it right away from the download code (since an asset bundle is loaded via accessing the assetBundle property of WWW--no joke). I can’t easily download it, pass it along to another object, and then load it when I’m ready without worrying about the IDisposable nature of the WWW object.
If any of what I’ve said sounds like nonsense, I’d love to hear the solutions to these issues.
Sunday, July 20, 2014
Regarding if/else versus if/implicit else
Let's suppose you have an if statement with a return statement in the block:
if (someCondition)
{
return x;
}
return y;
There's no need to write an else block, because it's implied that the only time return y will be executed is if the if condition is false. Nevertheless, I prefer writing:
if (someCondition)
{
return x;
}
else
{
return y;
}
Why? Because it's more resistant to change. What if I (or someone else) suddenly modifies the true block, eliminating the return statement? In the first code snippet, this may result in a bug. The second one, however, is protected from this.
Sunday, September 8, 2013
Some life updates
Wow, I was so down on myself about Ludum Dare 27, but looking at that little video in my last entry, it’s kind of cute. I’m excited that Unity announced a bunch of new 2D stuff, including Box2D integration (which should make those rope physics simpler). So we’ll see how that plays out.
I recently got an awesome new job as a game developer at a company I have always had a ton of respect for. This really is an incredible situation and I’m almost afraid to boast about it for fear of jinxing it. It’s so nice to be excited to come to work every day. I won’t share the details of my employment out of an irrational fear that I’ll say something objectionable in my blog some day that will come back to haunt me.
The only problematic consequence of having my new awesome job is that I can’t feasibly work on my own independent game projects except as a hobby. I may still work on non-game apps and that sort of thing, which I’ll post about here. And I’m sure I’ll have plenty of demos I work on in Unity just for fun.
Sunday, August 25, 2013
Same ol'
I think I'd rather do the latter, as defeatist as that sounds. I don't feel too bad though, because I learned a lot about dynamically generating a rope/grappling hook using Unity's built-in physics.
I would say "next time", but I know I'll just feel the same way then too.
Saturday, August 24, 2013
Simple rope slinging
Okay, progress has been slow, I know.
But here’s the beginnings of the rope slinging, with no physics or termination or anything. I haven’t decided yet but I may turn this game into pure rope slinging, no running and/or jumping.
My work so far
As always, I’ve been slacking, Redding, eating, walking, etc. when I should be working. But I’ve got a little stick figure that can jump around and land on platforms and sometimes not fall through them:
Friday, August 23, 2013
Ludum Dare 27
There was my entry for LD25, Indoctrination, which I shouldn't have even submitted because it was unfinished. It was an attempt as a propaganda game for rationalism, but with such an important message I really shouldn't give it a half effort.
Then there was my entry for LD22, Loner Jetman, an homage to Solar Jetman. I was disappointed in myself for spending so little effort on this entry (about 10 hours of work), yet at the same time I was happy to see how much can be accomplished in 10 hours in Unity.
There were other jams that I started without anything ever really materialized.
Anyway, with 20 minutes remaining until the jam, I need to quickly set up a BitBucket repository, etc. before then.
Saturday, August 10, 2013
Creating an NGUI Scroll View with a Clipped Panel
After attempting to follow the Clipped Panel Scroll View tutorial at the NGUI website, I realized that it’s lacking some information that probably isn’t obvious for beginners like myself. It omits some crucial details, and adds extraneous UI elements that add unnecessary complexity to the example.
This rough but thorough tutorial is my attempt to fill in the gaps with an example that only includes the necessary UI elements. Please leave a comment if you feel I need to elaborate in any areas. I’m still learning NGUI so my solution may not be ideal, but it should be clearer than the official tutorial.
For what it’s worth, I’m using Unity 4.1.5f1 for Windows, NGUI 2.6.4 (August 3, 2013). I noticed that the tutorials quickly get out of date as the NGUI interface extensions change.
To summarize what we’re going to accomplish, we’re going to create a tiled grid of images that we can drag left and right, which will clip when outside the bounds of the panel they’re contained within. Spoiler alert:
1. Create a new Unity project and import the NGUI package.
2. Delete the Main Camera that appears by default. When we create the NGUI UI, it’ll create the camera we’ll use.
3. Select NGUI->Open the UI Wizard, and you’ll see the following dialog. Accept the defaults and click Create Your UI.
4. You should now see some objects in your Hierarchy created, which are pretty standard for NGUI UIs.
This Panel will be the panel whose bounds will clip content that we add inside it. If you select it, you’ll see that its bounds highlight in green, and match the camera’s bounds currently.
Side note: You’ll notice that the white camera gizmo icon is ridiculously huge. You can scale this down by clicking the Gizmos dropdown in the Scene view and dragging the 3D Gizmos slider to the left as desired.
5. While we have the Panel selected, change its Clipping value to Hard Clip from the Inspector. This will ensure that anything outside the bounds of the Panel will be clipped.
Notice that this revealed some additional properties, Center and Size. Change the Size property to something like X,Y (400, 100). This will create a restrictive clipping range that will allow us to demo the clipping a little bit more easily.
6. Add a Draggable Panel component to the default Panel. With this Panel selected, choose Component->NGUI->Interaction->Draggable Panel. We’ll manipulate its properties a little bit later.
7. Now we want to add a Grid as a child to our Panel. A Grid element, as I understand it, just organizes its children in such a way that they’re spaced evenly. Select GameObject->Create Empty to create a new, empty GameObject in the scene. Drag this GameObject to the Panel to attach it as a child.
Rename this object “Grid” for clarity, and with it selected, choose Component->NGUI->Interaction->Grid to add the Grid component to this object. We’ll discuss the UIGrid component’s properties a bit later.
8. Next we’re going to add children to the grid, which will be the images that we’ll scroll left and right in and out of the clipping region. Specifically, we’re going to add one item as a child, configure it properly, and then duplicate it several times to create each grid item from a template that’s configured exactly as we want it.
First, let’s create a Sprite object as a child of the Grid. Select the Grid object and then choose NGUI->Create a Sprite. With this Sprite object selected, you’ll notice that the UISprite component is pretty bare if you look at it in the Inspector.
This is because we haven’t even created a sprite image yet, and the sprite atlas that will contain this image. Let’s do that now.
9. Using your favorite image editing program, create an image to be the “tile” that will be repeated in a horizontal pattern, and scrollable left and right, clipped at the bounds of our panel. This is just a sample, so create or steal whatever image you want, but let’s make its dimensions 64x64. Here is mine:
Add this image to your project’s Assets folder.
10. With the Sprite selected in the Hierarchy, click the Atlas button. Since there’s no atlas in the project, you’ll see this dialog:
Click the Open the Atlas Maker button to open the Atlas Maker. This isn’t a tutorial on how to create a texture atlas so I’ll just walk through the steps.
With the Atlas Maker open, select your newly added Texture from the Project window. It will appear in the list of Sprites (mine is called “tile_image”).
Click the Create button and all of the assets required for the atlas will be created (Material, GameObject, and the generated image).
11. Now select your Sprite from the Hierarchy again, and this time you’ll be able to select an atlas when you click the Atlas dropdown.
Select your Atlas and your sprite from the Inspector. If you experience what I did, you’ll notice that your sprite doesn’t appear properly. This was because, for some bizarre reason, my Grid object had a scale of XYZ (263, 263, 263). Why? I have no idea. But in any case, you’ll want to change that to (1, 1, 1). At that point, your beautiful sprite image will come into view:
If your image is positioned oddly within the Panel, select the Grid in the hierarchy and move it to a sensible location.
12. Before we start duplicating this sprite (hold your horses), we need to configure some things on the sprite’s GameObject and the Grid. First, click the Make Pixel-Perfect button which should force the scale of the sprite’s object to match the pixel dimensions of the sprite image.
Now, select the Grid and change the Cell Width and Cell Height properties to 64. This essentially says: “Space each item by 64 units, horizontally and vertically”. This matches the size of the image, so they’ll stay snugly together. In our case, only Cell Width matters, but I like consistency.
13. Select the Sprite again in the Hierarchy, and choose Component->NGUI->Interaction->Drag Panel Contents. This is how the UIDraggablePanel component which we added to the Panel knows what to drag.
14. You still can’t drag your image around (you can test this by running in the editor and clicking/dragging your sprite—note that if it’s your first time, you’ll have to make sure you’ve saved your scene). In order to do this, we need to add collision bounds to sprite. Thankfully, this is easy. Choose NGUI->Attach a Collider. This should create a collider around your sprite which conveniently matches its dimensions:
If you run the game in the editor now, you can click and drag your image around, both horizontally and vertically. Neat!
15. Since we only want horizontal scrolling for our example, we need to specify this restriction. The way we do this is by selecting the Panel in the Hierarchy, expanding the Scale property of the UIDraggablePanel component, and setting the scale to XYZ (1, 0, 0). This prevents dragging in the Y and Z directions. Also uncheck “Restrict Within Panel”, because we don’t need that restriction (it would spring the panel contents into the panel if they’re dragged outside).
16. We’re finally ready to duplicate the sprite. Do this simply by selecting the sprite and typing Ctrl+D (or if you’re a n00b like me, right-click the Sprite and select Duplicate from the context menu). Do this 4-5 times.
Note that you will still see just one image. That’s because the new images aren’t yet positioned properly. Positioning within a grid in the editor is not automatic. Select the Grid object and click Reposition now. You should see something like this:
17. Run this in the editor, click your tiled images, and drag left and right. Voila! You can drag the tiles back and forth, and they should properly clip when outside the bounds of the Panel.
Thursday, June 6, 2013
My future development / blogging strategy.
TL;DR: I’m going to start blogging about all the technical hurdles I encounter in my game development, along with solutions, in order to prevent hurdles from killing my motivation.
I am a pretty lazy person. Even if I finally gather the motivation to start working on my game project(s), I’ll run into a brick wall any time I’m challenged technically. This is probably not a good trait for a programmer to have, but it’s what I’ve come to live with as someone who wants to make games.
Of course, it’s especially worse after a long day of work / lack of sleep. The thing is, I’m a very goal-oriented person, and I quickly become frustrated at anything that stands in the way of that goal. I’m likely to just give up altogether if it looks like there will be a ton of hurdles in the way of my goal.
To resolve this, I’m going to try to use this blog to alleviate some of those problems. This has helped me several times in the past. For example, when I had a completely obnoxious coworker who would infuriate me several times each day, I started blogging (anonymously) about him, humorously sharing all the annoying things he did each day. Suddenly, his daily offenses magically converted to “completely frustrating” to “fresh new material”. My entire outlook was changed and his behavior didn’t evoke the same hatred anymore.
As another example, in solving various technical challenges at a previous job with little to know guidance, I found myself regularly extremely frustrated at how many hurdles I had in the way of accomplishing my tasks at work. So, I took to blogging about all of these. Not [just] to complain, but to illustrate the steps I took to try to solve it and the ultimate solution (with a summary for readers in a hurry). I thought to myself, “If this was so frustrating for me, there have got to be others with the same issue.” This simultaneously happened during a time in my career in which I was feeling pretty worthless and undervalued. I was beginning to think that these sorts of frustrations were something that only I experienced. So I figured these blog posts would either elicit hateful responses of “Duh, that’s totally obvious, n00b” or else reveal that I wasn’t alone in my frustration.
Thankfully, the latter happened—more than I could have imagined. I had random posts describing how to solve annoying errors in MS SQL Server or Wacom Tablet software, and almost every one of these got responses from people thanking me profusely. It felt really good to be that very welcome search result which solved someone’s issue.
So, with that in mind, when I run into technical hurdles as I try to develop my game, I’m going to go through the hard work of solving them, and blog about the result. The beauty of this is that it effectively creates a new, singular goal. Each “hurdle” thus becomes a goal in itself.
I truly hope this will help others in the future.
Sunday, May 26, 2013
Well look who came crawling back...
Monday, July 5, 2010
Check out my "almost live-blog"
Tuesday, June 15, 2010
Unresolved questions, resolved*
*subject to change
In my last post I had two unresolved design questions that I’ve tentatively come to a conclusion with. That is to say, I’ve decided which direction I’m going to move forward with but as always that’s subject to change:
1. Will this be an iPhone or Windows Phone game first?
An iPhone game first. Besides the obvious (marketshare), here are some main reasons:
- I will be ordering an iPhone 4 tomorrow, which means I’ll have physical hardware to test on much sooner. For example, I can test the motion-control difference between the iPhone 4 and my current iPhone 3G.
- The Windows Phone toolset is very rough at this point and the performance of the emulator is lacking at best, frightening at worst. I can theoretically complete the iPhone version with plenty of time to spare to get the Windows Phone version done when the toolset is more mature.
- The iPhone 3GS and iPhone 4 support custom shaders, which is a feature I may support in this game if I want to achieve some exceptionally cool effects. Windows Phone does not support custom shaders.
2. Will the levels be random or fixed?
I stressed about this in my head for a long time, but I’ve come up with a compromise that I’m happy with. Rather than create fixed levels (which would necessitate a level editor – which itself would be a large obstacle to tackle since the levels would span humungous distances), I can have fixed themes with randomly generated levels on each theme. For example, I can have a starbase level, a planetary cavern level, etc., and each could be completable (as opposed to a never-ending “Canabalt”-style game). The difficulty of each playthrough would be about equal to any other, but the random variance would keep things interesting. (I realize I’m rationalizing a bit.)
I was wanting to avoid a never-ending single game in which your only accomplishment is the amount of time you stay alive before blowing up. After all, the premise of this game is that you’ve just destroyed the enemy HQ; it’d be a shame if your heroic act always ended in tragedy (though I’m sure I’d get some emo indie points if I designed it that way.)
Friday, June 11, 2010
Upcoming game, codename “Escape”
Here’s a very simplified mock-up of the iPhone and/or Windows Phone game I’m working on.
Not much, right? Well here’s the premise: You know all those sci-fi games / movies in which the hero destroys the enemy mothership/starbase/etc. and escapes in his or her ship just in the nick of time, before the enemy craft explodes behind them? Well the span of time between setting off the chain reaction and escaping safely is the entire premise of this game.
You’ll need to race your ship through corridors, twisting and turning to avoid obstacles and the walls on your sides in order to outrun the explosion approaching behind you. If you’re not quick enough, you’re toast.
I still have many design decisions yet to make, but there are some I am pretty solid with so far:
- The game will feature vector-based, “geo-style” graphics
- The game will be controlled with motion controls (accelerometer and/or gyroscope). I will need to test this on an actual device to ensure that it’s fun, but the goal is that the ship itself doesn’t rotate; the world rotates around the ship as you rotate your device.
- It will be impossible to die aside from letting the explosion behind you catch up to you. Everything else will just slow you down; scraping against the sides of walls, colliding with obstacles, etc.
And here are some I’m not so certain about:
- Will this be an iPhone or Windows Phone game first? The development environment for Windows Phone 7 (XNA, Visual Studio) is much more appealing, but the toolset is still in beta and the performance of the emulator makes me nervous – not to mention the big risk of placing a bet on WP7. Additionally, Steve Jobs just announced the new awesome iPhone4 super precise motion controls, almost as if it were a personal gift to me for this project.
Also, if I go the iPhone route, another decision may be whether or not I limit the game to the newer iPhone graphical capabilities (OpenGL ES 2.0) or if I support older iPhones as well.
- Will the levels be random or fixed? One plus side of the random option is that it avoids the need for a level editor and completely pre-defined levels, but it also eliminates the ability to “master” levels through practice, or be able to compare your accomplishments equally to anothers’. I see a lot more potential for sharing/comparing/feeling a sense of accomplishment with fixed levels. I’d rather escape from The Death Star than Random Level 46024. Random levels would mainly just be a time-saver. Of course, if you guys disagree and think random levels would be cooler, I’d be happy to do it that way.
There are also tons of stylistic considerations regarding how the ship and levels will look, especially related to the simplicity versus intricacy. I may keep it very simple aside from some “juicy” particle effects. But, though personally I am fine with a very emotionless, geometric world, I think the game could benefit from a little bit of “cuteness” or other personal touches.
As always, any feedback is welcome. I’ll post more videos and images as the project progresses.
Wednesday, June 9, 2010
2D Line segment intersection detection in C# / XNA
For a previous game project, I needed a nice line segment intersection detection algorithm. It was surprisingly difficult to find a good one in C#, but I found one in C++ that I converted.
As it turns out, my upcoming game makes heavy use of this algorithm as well. So here it is in case anyone else needs it. This version is a little sloppy in that it uses 3D constructs despite checking 2D intersections, but it would be simple to convert this to its 2D counterpart.
- // Returns true if the lines intersect, otherwise false. If the lines
- // intersect, intersectionPoint holds the intersection point.
- public bool Intersects2D(LineSegment3 otherLineSegment, out Vector3 intersectionPoint)
- {
- float firstLineSlopeX, firstLineSlopeY, secondLineSlopeX, secondLineSlopeY;
- firstLineSlopeX = this.Point2.X - this.Point1.X;
- firstLineSlopeY = this.Point2.Y - this.Point1.Y;
- secondLineSlopeX = otherLineSegment.Point2.X - otherLineSegment.Point1.X;
- secondLineSlopeY = otherLineSegment.Point2.Y - otherLineSegment.Point1.Y;
- float s, t;
- s = (-firstLineSlopeY * (this.Point1.X - otherLineSegment.Point1.X) + firstLineSlopeX * (this.Point1.Y - otherLineSegment.Point1.Y)) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);
- t = (secondLineSlopeX * (this.Point1.Y - otherLineSegment.Point1.Y) - secondLineSlopeY * (this.Point1.X - otherLineSegment.Point1.X)) / (-secondLineSlopeX * firstLineSlopeY + firstLineSlopeX * secondLineSlopeY);
- if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
- {
- float intersectionPointX = this.Point1.X + (t * firstLineSlopeX);
- float intersectionPointY = this.Point1.Y + (t * firstLineSlopeY);
- // Collision detected
- intersectionPoint = new Vector3(intersectionPointX, intersectionPointY, 0);
- return true;
- }
- intersectionPoint = Vector3.Zero;
- return false; // No collision
- }
Tuesday, June 8, 2010
Intro
Hi, thanks for checking out the blog. Funcode is the name I’ve given my small game development operation consisting of myself and whomever is kind enough to work with me on fun projects.
Funcode is my passion; it represents a lifelong desire I’ve had to make games and software with small, personal teams, without the stresses or hassles of the corporate environment. It’s more than a hobby for me; it’s a commitment and a fulfillment of a dream.
I’ll spare you the cheesy bio for now. In my quest to achieve my goal of being able to wake up whenever I want, I’ve created three apps for the iPhone so far:
PhotoToons – allows you to add cartoony “stickers” to your photos as well as paint them, and save them to your image library. My artist friend did most of the artwork for the stamps.
Crab Crawl – A fun “never-ending-falling”-style game in which you play a crab who is desperately trying to avoid getting caught. Along the way you encounter all sorts of hazards and bonuses. This was my first iPhone game and I had a blast working on it. You can check out a video here. My girlfriend can take credit for the art, and her good friend Bernadette Choy provided the great music.
Dave and Steve’s Droppings – This is a promotional app I made for my friend’s humorous podcast (The Dave and Steve Show). This friend also happens to be the artist behind PhotoToons. This app is like a soundboard that lets you play a big selection of audio clips from the show (mostly potty humor), from each of the hosts. It also lets you queue up multiple sound clips and play them in a row, completely out of context and offensive, of course.
I’m currently working on a new game that I’m prototyping on the Windows Phone 7 emulator, but very seriously considering developing on the iPhone initially. I’ll announce more details once I feel it’s worthy of an unveiling.