Alastair Callum Unity Developer

Stop calling them scripts

Or don't, I'm not a cop!

Ok, this is more a personal habit but a sensible one - hear me out. The most commonly referenced thing in unity is the idea of a Script, Unity even uses it in its context menus when creating new C# Scripts. This idea is a remnant from the days when programming in Unity was made to look approachable by non-technical game developers and frustratingly stuck around.

However it makes more sense to look at a Script as just a Class that extends MonoBehaviour. It might not seem like much but this distinction can help structure your games in Unity. Take a look at the hierarchy below, this is from Flocks Sake.

undefined

I use two distinct terms to organise my code, Behaviours and Classes, a folder called Scripts is nowhere to be found.

  • Behaviours - Classes that extend the Monobehaviour base class and execute the Monobehaviour methods
  • Classes - Standalone classes that might extend other classes but do not act as Monobehaviour instances in the game

This keeps all of your Monobehaviours away from Editor code, keeps game data classes away from your game controllers. So don't make that Scripts folder in your next Unity project and do it properly. It's always a great idea to have project hierarchy style just like you have a programming style.

Oh and on a final note be sure you are using the reserved folder names correctly, putting all your models in the Resources folder might seem like a good idea but could impact negatively on your project.

Links in this post

You are not a web designer

You are a game developer!

Excuse me while I jump on my high horse for this one. Personal portfolio websites are a great way of cataloging your games and projects but be careful about how much time you put into creating your website over the projects you want to show.

I've been guilty of this before - spending weekend are weekend finding and customising the perfect WordPress template to show off all my work.. only to be left with something that looks nothing like the original and completely distracts visitors from what I wanted to show them. Now this isn't a dig at WordPress - it's a great framework, I once built a Unity project entirely around it's API to upload and post community driven content.

But you must keep in mind if people are interested in giving you a job making games, it's going to be unlikely they will care that you've got a responsive layout and a jQuery driven image gallery.

It's a little difficult to preach about this when my portfolio pages aren't exactly the picture of perfection - but I'm trying, and so should you. My advice is stay away from flashy javascript animations, carousels, and responsive layouts. Keep things simple, a couple of images with a video, and a short paragraph or bullet list of the skills used is more than enough. Provide a download link if necessary but do not expect all visitors to bother with it. It might be worth looking at a different framework, there are a handful from Mashable and WebHostingSearch.

I'm not saying don't take pride showcasing your work - just be honest to yourself about your skill set and where your time is best placed.

Links in this post

Search period

One of the main features of the EVE Online Market view is filtering history by a date range, allowing the player to see trends over 1 year, 6 months, 3 months, and the last 7 days.

Luckily all entries in the history come with a date time string, which can be parsed directly to a .NET DateTime object. Compare this with a date from six months ago and we can filter the results.

DateTime cutOffDate = DateTime.Today.AddMonths(-6);

foreach (Item _item in hist.items) {

    // Grab the date from the history entry and compare against the cutoff date
    DateTime _date = DateTime.Parse(_item.date);
 
    if (_date >= cutOffDate) {
        // Add the history
    }
}

Making use of the new Dropdown UI element in Unity 5.3, the date range can be set directly from the UI event callback.

undefined

It is important to remember each entry in the history is a daily average, high, and low price of the specified item. In time, data about current buy/sell orders will be polled. It is this data that should benefit from the CREST API.

Links in this post

  • MSDN DateTime - Represents an instant in time, typically expressed as a date and time of day

JSON to C#

You shall not parse!

With a knowledge of the CREST API endpoints, requests can start being made for data - in Unity this is as simple as initialising a new WWW object with the specified endpoint URL. For example, a request for the history of an item on the market can be made with the following endpoint:

_www = new WWW("https://public-crest.eveonline.com/market/10000002/types/34/history/");

This will return a rather impressive JSON object containing nearly 400 entries for the price history of Tritanium in a market region known as The Forge. The problem here is that this string is pretty useless without being restructured in memory, the solution then is to parse or deserialise it into an object oriented .NET structure.

There are many ways to do this, SimpleJSON is a parser and builder written in C# but relies on knowing what index you are after. .NET's JSON Serialisation does a similar thing but converts directly to and from .NET Framework types. For the amount of data returned from the CREST API the latter solution would be preferred; being able to navigate a class structure representative of the JSON string.

And how about Unity?

Unity 5.3 comes to the rescue again, in this release the engine can now serialise and deserialise classes and structs marked with the System.Serialisable attribute. Unfortunately the class or struct type is needed before the deserialisation can be done with the Unity JsonUtility.

public static T FromJson<T>(string json); 

So how can the desired class structure be created when we do not know the structure of the source data? The Unity Manual suggests the following:

Deserialize the JSON into a class or struct that contains ‘common’ fields, and then use the values of those fields to work out what actual type you want. Then deserialize a second time into that type.

Not very helpful. Please welcome json2cshap.com. This tool will create the C# class structure from a JSON string - there's only a couple of things that need to be looked at.

By default the tool will create the members of any classes in the JSON string as properties, with getter and setter attributes. This appears to dissagree with Unity, so remove any getters/setters from the class and leave them as fields. The System.Serialisable attribute needs to be added to the classes too.

[System.Serialisable]
public class RootObject {
    public string totalCount_str;
    public List<Item> items;
    public int pageCount;
    public string pageCount_str;
    public int totalCount;
}

[System.Serialisable]
public class Item {
    public string volume_str;
    public int orderCount;
    public double lowPrice;
    public double highPrice;
    public double avgPrice;
    public double volume;
    public string orderCount_str;
    public string date;
}

This makes it possible to deserialise the item history into the above class structure using the code below.

RootObject _history = new RootObject();
_history = JsonUtility.FromJson<RootObject>(_www.text);

By default the json2csharp.com tool will use generic class names such as RootObject and Item, all that needs doing is changing these to something more semantic such as ItemHistory and HistoryEntry for example.

Links in this post

Home