Easier Prefab Creation

“This script literally rocks my raincoat.”
— Mary Baker Allen

At the request of fellow code conjuror Brad Keys, I wrote a simple editor script for Unity that creates a prefab containing the contents of the currently selected game object. The new prefab bears the name of the selected game object and is placed in the project’s root directory. This function is accessed from the GameObject → Create Prefab From Selected menu.

While setting up prefabs is a trivial task, this menu item simplifies the process somewhat. It’s not perfect — the selected game object doesn’t become an instance of the newly created prefab, and an existing prefab with the same name is replaced — but perhaps you’ll find it useful if excess clicking isn’t your game of golf.

Download the script here or contribute your own improvements at the Unify Wiki.

Posted on by Matthew Miner | Leave a comment

GeekTool: Quick Access to Unity’s Debug Logs

“I am neither a geek nor a tool.”
— Joe Camel

Yesterday I mentioned how terribly inconvenient having to fire up OS X’s Console.app to view Unity’s web player debug log is. The in-game console script I supplied eases some of that pain, but there are still times when I find myself wanting to view Unity’s logs and dreaming of a fantasy land where this is easier to do. Propitiously, that fantasy land exists in a free utility named GeekTool, which through careful calculation I’ve determined kicks severe amounts of ass.

According to its website, GeekTool allows you to “display on your desktop different kind of informations.” Indeed it does. It can display images, the results of shell scripts, or in my case, the contents of text files. This is especially useful when the Unity editor crashes and I need to know why. At a glance I can see any errors that have occurred and all sorts of other funky information that gets sent to the Unity logs. When I’m not in development mode and don’t want lines of cryptic text cluttering my desktop, I simply turn off the “Unity” group from GeekTool’s menu bar dropdown.

Highly recommended.

Posted on by Matthew Miner | Leave a comment

Consolitis

“I had my consoles removed when I was a child.”
— Virginia Ann Cooksey

Like any self-respecting Unity developer, I monitor the contents of the Console window religiously. The editor presents a slick interface, but when it comes to keeping tabs on the warnings and exceptions output by a web player game, a trip to OS X’s Console.app is necessary. I’m apparently not the only developer to suffer through this insufferable task, and in-game console windows have been developed to banish my inconvenience.

Unfortunately, scripts like the one linked to above require that messages be logged using a separate class. I want to continue using Unity’s trusty Debug class and be free to drop the script into an existing project without any code changes. Luckily Unity allows logged messages to be rerouted through your own callback method using Application.RegisterLogCallback. Each message can be saved to a list and displayed in a GUI window when a hotkey is pressed.

For your enjoyment, I’ve uploaded the script I use for my own console popup window that implements such functionality. In addition to displaying debug messages, it has a few nice features like different text colours for warnings and errors as well as a “collapse” feature that will be familiar to users of the Unity console. Simply drag this script onto any game object then use the ` backquote key (this can be changed in the inspector) to invoke it when your game is playing. No code changes necessary.

Now, using the callback to show messages in a GUI window is its obvious use. Alternatively, you can send all error messages from your deployed game to a web server where they’re recorded for later perusal. This might make for a great way to implement gameplay analytics. The possibilities are endless. Like a möbius strip. Almost exactly like a möbius strip.

Posted on by Matthew Miner | Leave a comment

Changing Unity’s Boilerplate Script Code

“Those files be hidden mate. Hidden, but not buried. That just wouldn't make any sense.”
— Jan de Bouff

Quick tip: if you’re not a fan of the boilerplate code that Unity spits out when you create a new script (for example, if you’d rather have an OnGUI function ready to go rather than Start), you can easily change it to whatever suits you best. In Mac OS X, simply right click on the Unity application, choose “Show Package Contents,” and navigate to Contents → Resources. You’ll see three files named NewBehaviourScript. Open those up, modify them to your liking, and save. The next time you create a script in Unity, you’ll notice with groundless excitement that your changes are there.

Posted on by Matthew Miner | 1 Comment

Unity Cutscene Editor Initial Release

“I'm going to remake The Godfather... in 3D!”
— Zhang Rou

Today I’m releasing the cutscene editor I worked on as part of Unity’s Summer of Code last year. The version number is 0.1 — it’s several features short of what I envisioned and it’s not as user-friendly as I’d like it to be — but I feel it’s important to get the existing code out there for other eyes to see. What’s missing now is a sample scene, proper documentation, transitions, and a decent live preview, among other things. The “filters” concept (which basically applies full-screen effects to cameras) is half-baked. Transitions didn’t even make it to the oven. In other words, not all functionality you might expect is there, and the usability is frightening.

With that disclaimer out of the way, I intend to continue improving the editor and get it to the point where it’s indispensable to developers building cutscenes. I have set up a project on Google Code where the .unitypackage can be downloaded. The code hasn’t yet been saved to a subversion repository, but it will be shortly. The code is licensed under the liberal MIT license, so feel free to use it however you wish. I welcome any and all contributions; additional developers would be fantastic. Bug reports and feature requests, which I encourage you to make, can be logged on the issue tracker.

If you have any questions or would like to lend your developer talent, you can email me at matthew@matthewminer.com. In the coming weeks I’ll make some posts on how to use the tool, and hopefully some improvements as well.

Posted on by Matthew Miner | 3 Comments

Dashing Coroutines for an Improved Coding Experience

“I've had enough of your mumbo jumbo. Unless your mumbo jumbo encompasses coroutines. Those I dig.”
— Kaj Gottlob

After my last two borderline useless posts, perhaps today’s code snippet will be more valuable. I’m optimistic.

In the dimeRocker Unity client, of which I’m a developer, coroutines are used to send information to and from the servers. Conveniently, coroutines can execute in parallel with your own code or they can be called from another coroutine in order to wait for their completion. Such functionality is useful in Unity for a number of purposes, and seasoned developers will certainly be familiar with them.

IEnumerator Start () {
	// Wait for user information to be retrieved
	yield return StartCoroutine(drClient.User.FetchUser());
	// Download friends' profiles without waiting
	StartCoroutine(FetchAllFriends());
}

In the code above, the first coroutine finishes execution before the second one starts. An easily made mistake though is to call a coroutine without using StartCoroutine, as you would a regular method. In this scenario, the coroutine instructions aren’t executed and no errors are thrown. Luckily there’s a way to prevent such a ghastly mistake. Let’s say I have the following coroutine.

IEnumerator FetchSomeInfoCoroutine () {
	...
}

Tantalizing, I know. If I add an additional function that returns a Coroutine object…

public Coroutine FetchSomeInfo () {
	return StartCoroutine(FetchSomeInfo());
}

I can now execute the contents of the coroutine simply by calling the public FetchSomeInfo method. I don’t even have to be aware that I’m executing a coroutine. If I wish to wait for the instructions to complete, I can still pair the method with a yield statement.

IEnumerator Start () {
	// Execute contents of coroutine
	FetchSomeInfo();
	// Execute contents of coroutine and wait for it to finish
	yield return FetchSomeInfo();
}

As you can see, eliminating the need for StartCoroutine makes the code cleaner and more bulletproof (I myself have made the mistake of calling a coroutine directly, to great frustration). Of course, there is a downside to this method of calling coroutines, which is the reason why the dimeRocker Unity client still requires, for the most part, the use of StartCoroutine: developers currently calling coroutines are familiar with using StartCoroutine, and sending it a method that returns a Coroutine object will cause the compiler to throw an error. But, hopefully developers working on libraries to be used with Unity will adapt this system and folks will get used to it, and we can all have the elegant, “there’s no way you can screw this up” code we desire. I’m optimistic.

Posted on by Matthew Miner | Leave a comment

Blink Blink Blink

“Alternating visibility of letters, you say? What a splendid invention.”
— Eustice P. McGargle

As promised: a blinking text script for Unity, to bring back the glory days of Netscape Navigator. Sophistication starts here.

public float interval = 1;
bool visible = true;

void Start () {
	// Start blinking immediately
	TriggerBlink();
}

void OnGUI () {
	if (!visible) {
		// Set the GUI to be invisible
		GUI.color = Color.clear;
	}

	GUILayout.Label("History is gonna change.");
}

void TriggerBlink () {
	visible = !visible;
	// Continuous loop
	Invoke("TriggerBlink", interval);
}
Posted on by Matthew Miner | Leave a comment

A New Medium for the Scrolling Marquee

“Letters in motion, you say? What a splendid invention.”
— Eustice P. McGargle

Recently when reminiscing about that custodian of excellent web design that is was GeoCities, I decided to give the <blink> and <marquee> tags a whirl, for old time’s sake. Alas, Safari refuses to recognize these bastions of 90s tastefulness. Tears splashed on my keyboard as I mourned the death of greatness.

And then a user on the Unity forums asked about scrolling text in-game, to which I enthusiastically responded. Browser manufacturers may have made every attempt to kill the non-standard HTML elements so thoughtfully introduced by Netscape and Microsoft, but they can’t take away these innovations from video games. Class will live on.

public string message = "Where we're going, we don't need roads.";
public float scrollSpeed = 50;

Rect messageRect;

void OnGUI () {
	// Set up message's rect if we haven't already
	if (messageRect.width == 0) {
		Vector2 dimensions = GUI.skin.label.CalcSize(new GUIContent(message));

		// Start message past left side of screen
		messageRect.x      = -dimensions.x;
		messageRect.width  =  dimensions.x;
		messageRect.height =  dimensions.y;
	}

	messageRect.x += Time.deltaTime * scrollSpeed;

	// If message has moved past right side, move it back to left
	if (messageRect.x > Screen.width) {
		messageRect.x = -messageRect.width;
	}

	GUI.Label(messageRect, message);
}

Stay tuned for blinking text in Unity, for even greater user enjoyment.

Posted on by Matthew Miner | Leave a comment

The Contrivance of Dr. Bromegrass

“It’s hideous! It’s horrible! Such blasphemy, so deplorable.”
— Cold-hearted Intellectuals

Earlier I posted the script fashioned for my end-of-year motion picture at Vancouver Film School. The finished product has been in the can and uploaded to the world wide web for some time now, but I never bothered to spread the word. But now I’m bothering, and I invite you to enjoy my humble attempt at a musical.

Update: Switched to Vimeo’s sparkly new Universal Player. High five for HTML5.

Posted on by Matthew Miner | Leave a comment

Summer of Code: Progress Report on Unity3D Blog

“I am overjoyed at your ability to use hyperlinks.”
— D.B. Cooper

Recently a somewhat detailed progress report about my Cutscene Editor was posted on the Unity3D blog. Rather than copying and pasting the post here, I reckon I’ll link to the article instead. Enjoy!

Posted on by Matthew Miner | Leave a comment