16th February 2012

Link

Open Source Unity Plugin for Urban Airship →

This is why I was trying to figure out how to modify Xcode projects with AppleScript. The plugin needs some frameworks added, some resources added, a source file patched, etc.

This now works well enough and patches clean enough that I thought people might want to use it. It allows you to use Urban Airship’s Rich Push and Urban Airship’s StoreFront in a Unity iOS project.

Also, if you’re in a situation where you’re sick of adding stuff to the Unity Xcode project every time you build, you should take a look at the PostProcessBuildPlayer_Airship script to see how I’m automating a bunch of project modifications. I have a bunch of mods that need to be applied specified in a .projmods (JSON format) file that gets loaded, and applied.

Could come in handy for some people.

Tagged: Unity3Dgamedeviosurban airshipopen source

()

15th February 2012

Post

Modifying an Xcode Project with AppleScript is BROKEN…Use Python Instead

I hope this saves some future Google searcher some time.

TL;DR I wrote a python module that can be used to modify an Xcode project.

https://bitbucket.org/darktable/mod-pbxproj

I spent half a week trying to figure out how to modify an Xcode 4 project using AppleScript. It seems that it worked in Xcode 3, but it’s been broken in Xcode 4 from the beginning.

The wonderful part is that it’s not consistently broken. Some stuff breaks loudly, some stuff fails silently, and some stuff works as documented. This leads you into a spiral where you’re sure it’s just some little syntax error you’re making, and once you nail that it will all fall into place.

NO. MODIFYING AN XCODE PROJECT WITH APPLESCRIPT IS BROKEN.

Then you see a new beta of Xcode (4.3), and you think to yourself, “they must have gotten around to fixing this stuff now, it’s been like 2 years.”

NO. MODIFYING AN XCODE PROJECT WITH APPLESCRIPT IS BROKEN.

So, eventually I just sat there staring at the raw .pbxproj file that Xcode loads, and I noticed that once you remove all the comments the syntax is very similar to JSON. I’ve monkeyed with JSON before, I’ve fixed JSON parsers before, so I decided to try parsing and manipulating the pbxproj file directly.

It works.

So if you have a situation where you need to programmatically modify an Xcode project, take a look at my script, it may save you a ton of time.

References:

UPDATE: I just learned that the file format the .pbxproj file uses is an OpenStep plist. There’s a utility that comes with OS X called plutil that can convert this file to JSON or XML. This greatly simplifies the parsing of the file.

Tagged: XcodeAppleScriptgamedevpythonUnity3Dpatchingaggravation

()

1st December 2011

Link with 10 notes

Chat in Unity 3D using PubNub →

I’ve been working on a port of the C# PubNub library that can be used from in Unity3d. I put this together as a test for the library.

Tagged: CSharpPubNubUnity3Dgamedev

()

1st December 2011

Link with 4 notes

Updated MiniJSON Script →

MiniJSON.cs

Ages ago I found a script for encoding and decoding JSON and popped it up on the Unity forums. Wasn’t great, but it did the job. Yesterday I took a look at it and spotted a bunch of ways it could be cleaned up and optimized.

Even though I still recommend using JsonFX, MiniJSON can still be useful for situations where you just want something really simple (Array, Dictionary, etc.) encoded and decoded and size is a consideration.

Careful though, this script has changed a lot, so it isn’t a drop-in replacement for the old version.

Tagged: CSharpJSONUnity3Dgamedev

()

21st November 2011

Link with 3 notes

Quick demo of JsonFx usage in Unity. →

A couple of people have asked how to use the JsonFx library that I put up on bitbucket. The version I’m using is 1.4 from JsonFx.net, and it is poorly documented.

The two key methods are:

JsonReader.Deserialize<T>(string json);

and

JsonWriter.Serialize(object src);

With Deserialize you need to specify the type (T) you want to unpack to. This can be almost any C# class that has public variables or properties. In the example unitypackage linked above, I created two quick classes for Twitter search results, and an individual tweet.

What’s great about JsonFx is that it can deserialize nested classes, so the array of Tweets that is returned in search results gets deserialized too. This is something that I had issues with in most other libraries I evaluated.

The Serialize method just take the object you want to turn into a JSON string and does its thing.

You then have a string you can post to a web server, using the WWWForm and WWW classes in Unity.

If you don’t want to create your own classes for Serialization/Deserialization, you can just use Dictionary<string,object>, or an Array, or whatever.

There are some other things that are hidden inside JsonFx, like a JsonFx.IgnoreAttribute for public fields that you don’t want to be serialized, etc. You’ll have to root around in the assembly or source code for the exact syntax for that stuff. Remember, I didn’t write JsonFx, I just commented out a couple lines to make it work better in Unity.

Here’s the C# script that’s in the above Unity package:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DB = UnityEngine.Debug;
using DC = DebugConsole;
using JsonFx.Json;

public class Tweet {
    public System.DateTime created_at;
    public string from_user;
    public long from_user_id;
    public string from_user_id_str;
    public string from_user_name;
    public string geo;
    public long id;
    public string id_str;
    public string iso_language_code;
    public Dictionary<string, string> metadata;
    public string profile_image_url;
    public string source;
    public string text;
    public long to_user_id;
    public string to_user_id_str;
}

public class TwitterSearchResults {
    public float completed_in;
    public long max_id;
    public string max_id_str;
    public string next_page;
    public int page;
    public string query;
    public string refresh_url;
    public Tweet[] results;
    public int results_per_page;
    public long since_id;
    public string since_id_str;
}

public class JsonFxDemo : MonoBehaviour {

    public string query = "#Unity3d";

    // Use this for initialization
    void Start () {
        DC.IsOpen = true;

        StartCoroutine(PerformSearch(query));
    }

    void PrintResults(string rawJson) {
        // Raw output:
        DB.Log(DC.Log("******** raw string from Twitter ********"));
        DB.Log(DC.Log(rawJson));

        // Turn the JSON into C# objects
        var search = JsonReader.Deserialize<TwitterSearchResults>(rawJson);

        // iterate through the array of results;
        DB.Log(DC.Log("******** search results ********"));


        foreach (var tweet in search.results) {
            DB.Log(DC.Log(tweet.from_user_name + " : " + tweet.text));
        }

        DB.Log(DC.Log("******** serialize an entity ********"));

        // this turns a C# object into a JSON string.
        string json = JsonWriter.Serialize(search.results[0]);

        DB.Log(DC.Log(json));
    }

    IEnumerator PerformSearch(string query) {
        query = WWW.EscapeURL(query);

        using (var www = new WWW(string.Format("http://search.twitter.com/search.json?q={0}", query))) {
            while (!www.isDone) {
                yield return null;
            }

            PrintResults(www.text);
        }
    }
}

Tagged: CSharpExampleJSONUnity3Dgamedev

()

15th November 2011

Link

MonoDevelop Code Templates for Unity 3D →

templates

Big day, right? I’m having a little staycation and finally had the time to commit some repositories and write some posts. Tomorrow it’ll be back to playing games (gotta finish Arkham City).

It always annoyed me that MonoDevelop didn’t offer code completion for OnGUI, OnEnable, OnCollision, etc. events. Then I discovered that MonoDevelop had support for code snippets.

On the Mac you put these templates into ~/.config/MonoDevelop-Unity/templates/code/. Not sure where they live on PC. Then when you start typing the beginning of a Unity event (On...) it will show you a list of methods to insert. Select one and hit tab to insert it, and then tab again to insert the snippet.

This is only set up for C# Unity files.

Tagged: CSharpMonoDevelopUnity3Dgamedev

()

15th November 2011

Link

Unity 3D Mercurial (hg) Automation Script →

Unity Pro has a function that enables external version control. It’s great because you don’t have to pay $500 for a Unity Asset Server license, but it’s kind of a mess because it adds a ton of .meta files, and if you don’t commit the .meta file with the regular file you’ve broken version control.

Unity Pro also has some powerful asset post processor functions that let you customize the importing pipeline. Maybe these could work together (hint).

This script uses an asset post processor function to find added/deleted/moved files and correctly apply those changes to a local Mercurial (hg) repository (including the associated .meta files).

Mercurial is a great distributed version control system that can have large binary support enabled. Mercurial.net is an awesome .net library that lets you interact with Mercurial repositories in C#. I mixed that chocolate and peanut butter with a Unity script to automate some of the grunt work of keeping a Unity repository in sync.

The script doesn’t do push/pull/commit/merge (yet), so you’ll still need to use the command line or GUI client to do that stuff. I use MacHg, SourceTree, and TortoiseHg.

I don’t know if there’s a similar .net library for git, but if there is it should be relatively easy to do the same thing for a git repot. SVN and Perforce integration might be possible too, but since those are coming in Unity 3.5 anyway, it’s probably not worth the effort.

I don’t know if I’ll have time to support this script, but hopefully it can be a useful starting place for other developers.

Tagged: CSharpMercurialUnity3Dhggamedev

()

15th November 2011

Link

Script for Auto-saving Assets in Unity 3D. →

AutoSaveAssets.cs

Ummm…I think this works. It waits interval seconds and then saves any modified assets.

This may not seem like a big deal to you, but frequently prefabs and materials in a Unity 3D project aren’t saved until you either quit Unity or select “Save Project” from the File menu. This script sorta fixes that. This can be important if you’re using version control.

Tagged: CSharpUnity3Dgamedev

()

15th November 2011

Link with 20 notes

Here's my modified version of DebugConsole.cs (for Unity) →

DebugConsole.cs

DebugConsole.cs has been really useful to me. Especially when I’ve been trying to debug iPhone or standalone builds of Unity projects.

When I first added the DebugConsole to my project I didn’t understand a lot of the magic it contained. I’m pretty sure it was my first introduction to C# delegates. Since then I’ve leveled up a few times and add a ton of stuff to the DebugConsole script.

I’m sharing my modifications to the DebugConsole, because learning how it worked was so helpful to me.

Here are a couple of the changes that I’ve made, you’ll have to scour the source and learn some stuff to discover all of it.

  • Generic WatchVar. This should let people watch any object that has a ToString() method.
  • Added an FPS counter. This is based off one of the scripts in UnifyCommunity.com
  • Removed GUILayout stuff. This should let it work better on mobile devices (haven’t tried in a while though).
  • Console Command delegate returns an object which will be printed to the console, so the functions can return info to the console.
  • Hitting <enter> now submits the command line if it has focus.
  • Added a toggle key that shows/hides the console (default is “`”, same as the Quake console).
  • Pressing up arrow goes back through command history (like a Unix prompt).
  • Log functions return the object that they were sent. This lets you double up logging ( Debug.Log(DebugConsole.Log(“blah”)); ) This is useful because in the editor you can continue to get log messages that can be double clicked on to take you into the source file.

Tagged: CSharpDebugUnity3Dgamedev

()

23rd May 2011

Link

Top 6 Misconceptions About Unity Dev →

Tagged: Unity3Dgamedev

()