This isn’t much of a how-to. I don’t know much about LED cubes, so I can’t offer help with whatever technology you may be working on. It’s more of an outline of the steps I took to get Unity to work with one of these things.

I recently spent a weekend building a 3D version of “Pong” that runs on a 25x25x8 array of LEDs. The game runs on a bit of Unity code that I hacked together to map a Unity camera and the area in space that it films onto the display. The display itself was built by a friend of a friend, who wrote some controller software for it that would display a special video file format, reading in a sequence of configurations for the LEDs, and playing that sequence back frame by frame.

First I modified the source of the controller software so that it would display real-time graphics instead of video files. These modifications weren’t particularly sophisticated, and they weren’t pretty. I just took out anything that had anything to do with playback and managing playlists, until only the parts remained that connected to the display and sent it data. There was a place in the source at which the data representing the LEDs’ state was just one long string of 0s and 1s. I edited that part, supplying my own strings, until I figured out the format that the display required. Then I grabbed some code from a simple client-server tutorial and added the server parts to the controller. The idea was that it would listen for connections on localhost, and accept streams of “0″ and “1″s from any client on the same machine, and it would update the display accordingly. That way it would be easy to control the display from all kinds of different sources, such as Unity, Processing, or even Flash.

On Unity’s side of things, I needed to slice an area in space into 8 layers, to cover the 25x25x8 pixels that are available on the LED matrix. The approach I took was to write a component for an isometric camera that would render the scene 8 times per frame. For each of those times, I’d set the near and far clipping planes of the camera to different values so as to only render small slices of the scene, and I’d have the camera render the result into a RenderTexture.

After rendering each slice of the scene, I would copy the contents of that RenderTexture into a Texture2D. Then I’d access that texture’s pixel data and go over each pixel: if it was fully transparent, then that’d mean the corresponding LED should be off. Otherwise, it’d be on.
What’s nice about the setup is that it allowed me to preview the data that would be sent to the display, by creating 8 planes in Unity that would each have a texture representing one of the 8 layers. This way I could actually develop without the display, preview right in Unity, then just hook everything up when I was done.

I’m adding the source code for the renderer here, to better illustrate the process. It was slapped together in about an hour, and there’s much you could clean up and optimize, but this setup is supposed to only ever run on one machine, and for that, it does its job. Feel free to use it as you want, but obviously, don’t expect that you can drop this into your own project and get any sort of useable result. :)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Modul3DCamera : MonoBehaviour {

  public float nearClip = 4;
  public float farClip = 6;

  public Transform previewPlanesParent;

  private const int numLayers = 8;
  private int layerCounter = 0;

  private RenderTexture renderTex;
  private Texture2D readableTex;
  private Texture2D[] previewTextures;

  private int[] imageData;
  public int[] ImageData {
    get {
      return imageData;
    }
  }

  void Awake () {
    imageData = new int[25*25*numLayers];
    renderTex = new RenderTexture(25, 25, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
    readableTex = new Texture2D(25, 25, TextureFormat.ARGB32, false);
    // Create Preview Planes
    GameObject previewPlanePrefab = Resources.Load ("Modul3D/PreviewPlane") as GameObject;
    previewTextures = new Texture2D[numLayers];
    for (int i=0; i<numLayers; i++) {
      Texture2D tex = new Texture2D(25, 25, TextureFormat.ARGB32, false);
      tex.filterMode = FilterMode.Point;
      previewTextures[i] = tex;
      GameObject pp = Instantiate(previewPlanePrefab) as GameObject;
      pp.layer = 20; // Preview Planes layer
      pp.transform.parent = previewPlanesParent;
      Utils.SetTransformToIdentity(pp);
      pp.renderer.material.mainTexture = tex;
      pp.transform.localPosition = new Vector3(0, 0, -i*0.2f);
    }
  }
  
  void Update () {
    // render all slices
    int imageDataIndex = 0;
    camera.targetTexture = renderTex;
    for (int i=0; i<numLayers; i++) {
      PrepareCameraForLayer(i);
      camera.Render();
      Color[] pixels = readableTex.GetPixels();
      for (int j=0; j<pixels.Length; j++) {
        if (pixels[j].a != 0) {
          pixels[j] = Color.white;
          imageData[imageDataIndex] = 1;
        } else {
          pixels[j] = Color.black;
          pixels[j].a = 0;
          imageData[imageDataIndex] = 0;
        }
        imageDataIndex++;

      }
      previewTextures[i].SetPixels( pixels );
      previewTextures[i].Apply();
    }

    camera.targetTexture = null;
    camera.nearClipPlane = nearClip;
    camera.farClipPlane = farClip;
  }

  void OnPostRender() {
    readableTex.ReadPixels(new Rect(0,0,25,25),0,0);
  }

  /// <summary>
  /// Prepares the camera for layer. Goes from 0 to numLayers-1
  /// </summary>
  /// <param name="layer">Layer.</param>
  void PrepareCameraForLayer(int layer) {
    float step = (farClip-nearClip)/numLayers;
    camera.nearClipPlane = nearClip+layer*step;
    camera.farClipPlane = nearClip+(layer+1)*step;

  }
}

									

 

If you enjoy mobile games, you’ve seen the prompts that pop up after you’ve opened a game for a few times, asking you to rate it on the App Store. My game “Ace Ferrara And The Dino Menace” does this in a slightly different way, which turns out to be quite effective.

To give a bit of context, the game is a “Wing Commander”-style space combat sim with a quirky 80s cartoon story. The story has a colorful cast, with cybernetic cats, jaded adventurers and nefarious dinosaurs. The game is pretty heavy on dialogue, so players that stick with it tend to build up a relationship with the characters.
After around the 10th mission (maybe 1.5 hours into the game), the game shows a new button on the main mission hub’s lower right corner. The button is a drawing of the two main characters, with no further information:

The main game hub

Pressing the button takes you into a dialogue where those main characters discuss how hard it is to make a mobile game as a bedroom developer, and what the player can do to help spread the word. At the end of that, a menu pops up that lets you rate the game or share it on Twitter or Facebook. The dialogue is light-hearted and fun, and by the time you see it (if you haven’t made a habit of skipping all dialogue, in which case you’d miss the prompt), you probably like the characters enough to hear them out.

The rate-this-game dialogue

So how effective is it? According to my Flurry stats, 44 percent of all players that launch the dialogue watch it to the end and then press the “Rate this game” button. The number of people that press the Facebook and Twitter buttons, by contrast, is pretty negligible.

If you’d like to check out the game for yourself, you can get it on the App Store: https://itunes.apple.com/us/app/id901401597?mt=8

 

In MonoDevelop on Mac, I’ve noticed that the mouse cursor sometimes changes from the arrow to something else, like a vertical line, a different arrow, or similar. This seemingly happens at random, and for the longest time I didn’t know what it meant or how to change the cursor back.

Turns out this seems to be a bug, where a custom cursor (such as when you move the mouse over a resizable window’s edge) doesn’t revert back to the default cursor. I don’t know what triggers it, but if it happens, simply move the mouse over a window border that changes the cursor. Once you move the mouse back, the cursor should switch back to the default.

 

Update: This add-in breaks with the new Monodevelop 4 that ships with Unity 4.3. However, Monodevelop 4 comes with a “duplicate line” function for which you can assign a shortcut in the key bindings in preferences.

This is one of those things that you don’t know you need until you have it, and the one feature I’ve missed the most since my Eclipse/FlashBuilder days:
I wrote a little MonoDevelop Add-In that lets you duplicate the line you’re on (or multiple selected lines) by pressing Alt+Ctrl+Up to duplicate above or Alt+Ctrl+Down to duplicate below your current position. In combination with Alt+Up and Alt+Down (which move selected lines around), this comes in super handy for dealing with repetitive blocks of code (such as variable declarations).

You can download the plug-in here (again, only works in Unity up to version 4.3): http://www.philippseifried.com/files/MonoDevDuplicateSelection.zip

To install, close MonoDevelop and copy the DLL into the folder /Applications/Unity/MonoDevelop.app/Contents/MacOS/lib/monodevelop/AddIns on Mac, or [Unity install path]/Unity/MonoDevelop/AddIns on Windows. After starting MonoDevelop again, you can use the keyboard shortcuts, or access the functionality from the Edit menu.

Note that you will need to re-install the Add-In if you update Unity!

The Add-In is under MIT license, so feel free to use and redistribute it as you see fit. I’ve tested it on Windows and Mac (where I’ve been using it daily for a few weeks), but of course standard disclaimers apply: Use at your own risk!

 

Our goal for this article is to extend the SoundManager from part 6 with an architecture that lets us set up a chain of effects devices. Again, I’m not aiming for production-ready code, but for a simple and illustrative set of classes that demonstrate how to solve the problem at hand with a minimum amount of clutter. If you don’t have a good memory of the SoundManager class from part 6, I recommend re-reading that part before moving on.

Continue reading »

 

After you’ve developed in Unity for a while, you’ll be well aware that MonoBehaviour’s Start() will be used for initialization and Update() will be called once per frame. I found that I started erasing the two comment lines that point this out from new classes, so I looked for a way to change Unity’s C# template. This also proved handy for adding “using” statements for frequently used namespaces (System.Collections.Generic in my case).

On Mac, you can edit the default code templates by changing the files in /Applications/Unity/Unity.app/Contents/Resources/ScriptTemplates

(If you’re new to OS X, this means you right-click the Unity app in Finder and select “Show package contents” to get to the Contents folder.)

On Windows, the templates are supposedly in [Unity install path]/Unity/Editor/Data/Resources/ScriptTemplates/ – however I haven’t verified this as I don’t have a Windows machine with Unity on it. (If you’re a Windows user, please leave a comment and I’ll update this post.) – Update: Brent says this is indeed where the templates are.

This is for creating C#, JS and Shader files from inside the Unity editor, which I still find myself preferring to creating new files in MonoDevelop. If you’re looking for a way to change MonoDevelop’s own templates, this blog post should be helpful.

 

Throughout this series, we’ve been converting sample data ByteArrays to and from Vector.<Number>s and any audio processing that we’ve done, we did on these Vectors. While it’s clear that working on Vectors is more comfortable than directly accessing ByteArrays (what with having to explicitly set a ByteArray’s position in order to access a random element in it), you may have been wondering whether all that copying data around between these different structures comes with a huge performance penalty.

In this installment, I want to show you that just the opposite is true. http://www.philippseifried.com/blog/files/misc/AS3_Performance_Tests.zip contains a class with various performance tests. You can download the zip and run them yourself, or just read on and let me take you through their output.

Continue reading »

 

In this part of my series on dynamic audio in ActionScript, we’ll discuss a simple sound manager that can seamlessly mix and string together different parts of a soundtrack, based on Flash 10′s sound API.

This will give us the ability to splice together musical pieces at runtime, which helps to conserve file size (think of a verse-chorus-verse-chorus-solo-chorus-chorus type song, which is composed of only 3 distinctive parts but would be about double the length of these 3 parts combined, if loaded from a single mp3 file). It will also lay the foundation for part 8, in which we’ll start extending the sound manager with real time audio effects.

Please note that the class presented in this tutorial is not intended to be production-ready code (for example, there are no facilities to stop a running sound). If you need a complete sound manager to use in your Flash game, you’ll either have to look elsewhere or write it yourself, building on the principles discussed in this series of articles. This means you’ll need to be able to read and thoroughly understand the code in this tutorial and then write your own, depending on the requirements of your project. I’ll do my best to explain everything, but please don’t expect me to implement any features for you.

 

Continue reading »

 

I’ve compiled a little cheatsheet containing most of the info from yesterday’s post on MonoDevelop tips and tricks. I’d still recommend reading the original article once, as it contains more detailed information, as well as a whole section on customizing the editor.

Either way, here it is: http://www.philippseifried.com/blog/files/misc/Unity-MonoDevelop-Cheatsheet.pdf

Feel free to copy and distribute this PDF as you like. As I’m on a Mac, the keyboard shortcuts are for OS X. If you create a Windows version, please let me know so I can link it here.

 

(Note: This was written in 2012 for the MonoDevelop version that came with Unity 3.5, but most tips are still applicable.)

When I learn a new platform that comes with a new IDE, new paradigms, a new API and a new programming language, the IDE is usually last on my list. Months after our first Unity project, I still knew very little about MonoDevelop. I’ve finally found some time to dig around in the environment and compiled a little list of small things that can go a long way in increasing your productivity.

I’m on a Mac, so for any keyboard shortcuts I list that use CMD, you’ll probably have to substitute CTRL on Windows. Also I program in C#, so there may be some tips that aren’t applicable to JavaScript.

(Update: I compiled most of these tips into a PDF cheatsheet)

Continue reading »