Jump to content

  • Log In with Google      Sign In   
  • Create Account

Lior Tal's Sandbox

Developing for WebGL with Unity 5

Posted by Lysergide, 20 April 2015 - - - - - - · 660 views
unity, unity5, webgl, il2cpp
Hey All !

The slides from our presentation on "Unity and WebGL" are now available here: http://bit.ly/1G8Ju5f

We talked about the process of getting our game "Wonderball Heroes" to WebGL, and all the challenges, optimizations and other bugs we faced along the way.

It's pretty technical, but may be really helpful for all of you planning to support this platform with Unity 5.

Check it out, and feel free to comment / contact me in case you have any questions !

Auto Save for Unity

Posted by Lysergide, 23 January 2015 - - - - - - · 2,933 views
unity, unity editor, autosave and 3 more...
Auto Save for Unity This article was originally posted on Random Bits. Check it out for more Unity related content.
What can you do when a team member suffers a recurring Unity editor crash, losing all level design work ? My answer was to write a small and helpful editor extension - auto save! (and then blog about it).


This post describes a simple solution for implementing auto save in Unity that saves the currently open scene every 5 minutes (configurable).
The code is available for easy consumption in a few ways:
  • GitHub - Also updated in case i get any feedback / future posts
  • Gist
The code is imported and works out of the box, import / paste it into your Unity project and you're good to go.


It started a few days ago: a member of our team started experiencing occasional Unity editor crashes a few times daily. We do not know the exact reason for crashes, but we suspect it may be related to memory issues combined with voodoo magic. No matter what the root cause was, these crashes caused real damage in lost data (game levels) which we could not afford having.
In order to keep lost work to a minimum, I suggested to implement a basic auto save solution, so at least we can go back to a backup in case the editor crashes.

Solution - AutoSave

The solution uses pretty simple editor scripting to the rescue. The process can be described in 3 main steps:
  • Hook a delegate to EditorApplication.update.
  • In this method, check if the scene should be saved (if the configured time has elapsed. The default is 5 minutes).
  • In case we need to save, generate a new unique name for the scene and save it to disk.
In order to have the code up and running when you launch the editor, the class is marked with the [InitializeOnLoad] attribute and initialization is done in its static constructor.

Show Me the Code

This is the complete code, you can paste it into your project:
using System;
using System.IO;
using System.Globalization;
using UnityEditor;
using UnityEngine;
public class AutoSaveScene
    private const string SAVE_FOLDER = "Editor/AutoSaves";
    private static System.DateTime lastSaveTime = System.DateTime.Now;
    private static System.TimeSpan updateInterval;
    static AutoSaveScene()
        // Register for autosaves.
        // Change this number to modify the autosave interval.
    public static void RegisterOnEditorUpdate(int interval)
        Debug.Log ("Enabling AutoSave");
        updateInterval = new TimeSpan(0, interval, 0);
        EditorApplication.update += OnUpdate;
    /// <summary>
    /// Makes sure the target save path exists.
    /// </summary>
    private static void EnsureAutoSavePathExists()
        var path = Path.Combine(Application.dataPath, SAVE_FOLDER);
        if (!Directory.Exists(path))
    /// <summary>
    /// Saves a copy of the currently open scene.
    /// </summary>
    private static void SaveScene()
        Debug.Log("Auto saving scene: " + EditorApplication.currentScene);
        // Get the new saved scene name.
        var newName = GetNewSceneName(EditorApplication.currentScene);
        var folder = Path.Combine("Assets", SAVE_FOLDER);
        EditorApplication.SaveScene(Path.Combine(folder, newName), true);
    /// <summary>
    /// Helper method that creates a new scene name.
    /// </summary>
    private static string GetNewSceneName(string originalSceneName)
        var scene = Path.GetFileNameWithoutExtension(originalSceneName);
        return string.Format(
    private static void OnUpdate()
        if ((System.DateTime.Now - lastSaveTime) >= updateInterval)
            lastSaveTime = System.DateTime.Now;
Built In AutoSave

It should be noted that apparently Unity does autosave the current scene every time you enter play mode. If this is enough for you (for example - the game crashed during play mode), a copy of the scene can be found in YourProject/Temp/__EditModeScene.


The code in this post helps ensuring no scene data is lost when experiencing editor crashes. I deliberately kept it short & simple so it can be easily "digested". Autosaving can be further visited by adding any of the following:
  • Configuration - Allow controlling autosave (turning on/off, setting time interval) from an editor window or menu item.
  • Capping number of autosaves - Nobody really needs 50 copies of the open scene; instead, only 1 or 2 copies can be saved and recycled on every new save.
  • New save triggers - The current code saves every X minutes. The code can be adapted to save under different scenarios.
Related Resources

Finding Missing References in Unity

Posted by Lysergide, 06 December 2014 - - - - - - · 851 views
unity, unity-editor and 1 more...
While working with Untiy, missing references can occur for many different reasons. They are simply a broken "link" between objects and can result in the game not behaving correctly or even crash.

I just posted a new article on my blog on how to find missing references in Unity: http://www.tallior.com/fixing-missing-references/

Check it out and let me know if it was helpful :)

A summary of Unity's attributes

Posted by Lysergide, 16 November 2014 - - - - - - · 642 views
unity, unity3d, attributes
Attention attention, Unity developers.
I have written a short post, summarizing all of Unity's public attributes (45 in total).

Check it out here to see how many you already know, and to learn about the rest: http://www.tallior.com/unity-attributes/

Wonderball Heroes hits 500K on Google Play

Posted by Lysergide, 10 November 2014 - - - - - - · 723 views
wonderball, android, google-play and 1 more...
Our game - Wonderball Heroes, has recently hit the 500K download mark on Google Play.

For more info (and the download link to check out the game), see this post: http://www.tallior.com/wonderball-heroes-500k-downloads/

Unity Editor Extensions - Menu Items

Posted by Lysergide, 16 September 2014 - - - - - - · 512 views
menu items, unity, editor and 1 more...
Hello fellow devs,

I have posted an article (first one in a planned series) on extending the Unity editor.

The first part deals with creating menu items, and it is available here: http://www.tallior.com/2014/09/16/unity-editor-extensions-menu-items/

Check it out, and feel free to share/leave comments.

Support Bolt Riley - a cool point & click adventure game on Kickstarter

Posted by Lysergide, 08 August 2014 - - - - - - · 405 views

This is a call to all adventure game lovers - a good friend is running a Kickstarter campaign to raise funds for a cool adventure game called Bolt Riley.

Please check it out: https://www.kickstarter.com/projects/soundguy/bolt-riley-a-reggae-adventure-game-chapter-1

If you like it - pledge to make it happen Posted Image

ScriptableObject Factory - a Helper editor extension for Unity

Posted by Lysergide, 06 August 2014 - - - - - - · 869 views
unity, scriptableobject, editor and 6 more...
For those who are working with Unity and ScriptableObjects, one concern is how to actually instantiate scriptable objects.
The editor does not have any out-of-the-box option for doing so, and you have to do that manually in code for every type of object you define.

I have posted a new entry on my blog, containing a generic solution for this issue - an editor extension that will allow you to easily create ScriptableObject instances of any kind directly from the editor.

Please check out the post here - http://www.tallior.com/2014/08/06/unity-scriptableobject-factory/

Let me know if you find it helpful !

Feedback on my newly updated CV

Posted by Lysergide, 10 May 2014 - - - - - - · 725 views

As of yesterday, after being laid off (with pretty much everybody else) form the company I worked in, I am officially unemployed.

I would like to request your assistance for feedback on my newly updated CV - what is missing? what should be added, removed or rearranged?

Any help will be appreciated !

Here's the link: http://goo.gl/htIklF

Code generation fun with Unity

Posted by Lysergide, 11 March 2014 - - - - - - · 2,760 views
unity, t4, codegeneration and 2 more...
This post was originally posted on my blog at http://www.tallior.com
Many Unity code samples use a string identifier, such as the game object’s tag for various things (e.g: checking for a collision with “player”). In this post i will explore a different, safer and automated technique that achieves the same result, but does not require using strings.

The String Problem
Consider the following code:

Posted Image

The code is not type safe: it relies on a string identifier to perform an object lookup. This identifier may change, making this code “out of sync” with the project, or be misspelled, making the code fail. In addition, this string might be used in many different locations of the code, increasing the risk of previous mentioned concerns.

A Solution “Sketch”
A possible solution to this issue is to create a static helper class that will expose all tags as public (static) fields. When needed, Instead of using a string, we’d use the class’s static fields:

Posted Image

Accessing this tag is safer now, since we’re not (directly) relying on the string representation of the tag:

Posted Image

Effectively, the code will operate the same as before, but now we have a single location where the tag is declared.
There are 2 main issues with this approach:
  • In case there are many tags defined in the project, creating the helper class can be a somewhat tedious task (creating a field per tag).
  • In case a tag’s name changes in the Unity editor, you have to also remember to replace it’s corresponding value in the helper class.
It seems that this solution is a step in the right direction, but we need some extra “magic” to make it perfect.

Code Generation To The Rescue
Code generation is an (sometimes) overlooked practice, where code is being automatically generated by some other code, a template or some tool.

In particular, code generation really shines in cases where we want to generate long, repetitive code from an underlying data source.
Translating this to the problem described above, we would like to generate a static helper class with many static fields from an underlying data source (a collection with all of the project’s tags).

Posted Image

To achieve this, we’ll use one particular implementation of a code generation engine called T4:
T4 is a template engine that comes with Visual Studio (which also heavily relies on it for various tasks), and also comes out of the box with Mono (yes, the same one that is installed with Unity).
A T4 template is a file (with a .tt extension) that mixes a body of text with special directives. The template generates a single output file (usually, a code file, although it can generate any other file format).

T4 Templates
In order to add a T4 template to your project, right click on your code project in MonoDevelop, and select: Add->New File. T4 Templates can be found under Text Templating on the left:

Posted Image

T4 Template Types

There are 2 types of available templates (ignore Razor templates as they’re irrelevant for this discussion):
  • T4 Template – a template file that gets transformed during compilation time into the output file. This type of template is used to generate code files that are needed at design time (e.g: think of Microsoft’s Entity Framework, where a set of classes can be generated at design time from a database, instead of being created manually by the developer).
  • Preprocessed T4 Template – a template file that creates an “intermediate” class that can later be used to generate the output code file.
Unity currently does not support adding T4 templates (.tt files) to scripting code – after compilation, all .tt files will be dropped from the code project (I reported this bug here: T4 Bug)

This forces us to use option #2 – creating a one-time “intermediate” class. This class will be used by a Unity edior extension, from which we can generate the class we want and add it to the project.

Show Me the Code!

Here is the preprocessed T4 template that will generate the Tags class for us (although the provided sample uses the same template to generate a Layers class in exactly the same manner):


A few things that should be noted:
  • Any text that not contained within <# #> tags is being output as is.
  • The template is a preprocessed template. This means it does not generate an output code file directly. Instead, it generates an intermediate (partial) class with a TransformText() method that returns the template final output (the text with the generated class).
  • The code prints out a header (the class declaration with some comments), then it iterates all elements in source and outputs a public static readonly field for each item (does a small manipulation to make sure the field name does not have spaces in it).
  • The variables classname, item and source are actually implemented in a code file (a partial class with the same name as the template class. Remember I said the template generates a partial class? this allows mixing the template with some custom code. For more clarity, see the full code in the link below).
In Conclusion

This post aimed to open a hatch to the wonderful world of code generation (and T4 in specific), while showing how it can solve real world problems in a short and simple way.
I did not dive into T4 syntax or more advanced topics (leaving it for you to explore, or as a subject for future posts). For more information regarding T4 – see the links below.


July 2016 »

242526272829 30

Recent Comments