Unity Autosave on Background

All current (free) Unity autosave solutions depend on having the autosave editor opened at all times. With the little help from this blogpost, I have put together a solution that using multi-threading automatically saves your work on background every specific time interval. Plugin is loaded automatically with the project, and reload after every code compilation. It saves both scene and all assets.

The plugin is open-source and can be downloaded from GitHub. Below you’ll find some instructions:

Installation

Copy files into your scripts folder, maintining them in the Editor folder

Usage

In Window > AutoSave configure the time interval of the auto save.
Here you can also enable/disable the autosave as well as configure the verbosity of the plugin.

Implementation instructions

Plugin is using the multi-threading possibilities of Unity editor (which are rather limited). Unity in general obliges to perform any project related actions to be executed on the main thread. This blogpost explains, how to achieve this in Unity Editor (not the game). This blogpost explains how to add mutli-threading into your game. We have adjusted the script from the previously mentioned blogpost and execute the Threader fro the static method rather than depending on the Scriptable object what led into memory leak of the editor.

Apart from the multi-threading operations, we needed to save the user options, such as time interval of auto save. First option was to use a ScriptableObject, but it’s behaviour proved to be rather unexpected when reloading it after code complation. Therefore, we are storing the options in PlayerPrefs and expose public properties such as following:

public static bool ShowMessage { 
    get {
        return PlayerPrefs.GetInt(ShowMessagePref) == 1;
    } 
    set {
        if (value && PlayerPrefs.GetInt(ShowMessagePref) == 0 ||
            !value && PlayerPrefs.GetInt(ShowMessagePref) == 1) 
        {
            PlayerPrefs.SetInt(ShowMessagePref, value ? 1 : 0);
            PlayerPrefs.Save();
        }
    }
}
Advertisements

How to create a 3D sound of a large area (e.g. river) in Unity 3D

In our project, we have created a landscape with a large river passing through. The problem arrived, when we tried to add sound effect of running water which would trigger close to the water. Adding several sound effect zones seems highly inefficient and leads to problems when several sound zones overlap.

Instead we call upon help from scripting and some basic linear algebra. The idea is very simple: We will have only one sound source, which will move along a pre-defined linear path defined with a set of waypoints and at any given time it will move to the location closest to the player. We summarise this approach in a set of steps:

  1. Create a set of waypoints along the river bed. Here we have used the Simple Waypoint System
  2. Create an empty GameObject and attach AudioSource with the 3D sound to it.
  3. Attach SoundFollow behaviour, listed below
  4. Done

Following is a script that will follow the path made of waypoints.

using UnityEngine;
using System.Collections;
using SWS;

public class SoundFollow : MonoBehaviour {

    // path manager from the "Simple Waypoints" (http://www.rebound-games.com/?page_id=39)
    // you can assign waypoints directly in waypoints
    public PathManager manager;

    // this can be made public and assigned directly
    private Vector3[] waypoints;

    private Transform player;
    private Transform trans;

    void Awake()
    {
        // specific functionality
        waypoints = new Vector3[manager.waypoints.Length];
        for (var i=0; i<manager.waypoints.Length; i++) {
            waypoints[i] = manager.waypoints[i].position;
        }

        player = GameObject.FindGameObjectWithTag ("Player").transform;
        trans = transform;
    }

    // Update is called once per frame
    void Update () {

        // sort waypoints by distance
        System.Array.Sort<Vector3> (waypoints, delegate(Vector3 way1, Vector3 way2) {
            return Vector3.Distance(way1, player.position).CompareTo (Vector3.Distance(way2, player.position));
        });

        // get the two closest waypoints and find a point in between them
        trans.position = Vector3.Lerp(trans.position, ClosestPointOnLine (waypoints [0], waypoints [1], player.position), Time.deltaTime * 2);
    }

    // thanks to: http://forum.unity3d.com/threads/math-problem.8114/#post-59715
    Vector3 ClosestPointOnLine(Vector3 vA, Vector3 vB, Vector3 vPoint)
    {
        var vVector1 = vPoint - vA;
        var vVector2 = (vB - vA).normalized;

        var d = Vector3.Distance(vA, vB);
        var t = Vector3.Dot(vVector2, vVector1);

        if (t <= 0)
            return vA;

        if (t >= d)
            return vB;

        var vVector3 = vVector2 * t;

        var vClosestPoint = vA + vVector3;

        return vClosestPoint;
    }
}

You can easily drop the “Simple Waypoint System” and manually specify the set of waypoints.

Video

How to create low-poly hair for Unity 3D using Blender 3D and Hair Factory (tutorial)

In this tutorial I want to show you how to create low poly hair for Unity 3D using Hair Factory. Below you’ll find a link to the Hair Factory project as well as the code for the Transparent, Double Sided Unity Shader.

Shader "Transparent/Double-Sided Vertex Lit" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }

    SubShader {
        Tags {"RenderType"="Transparent" "Queue"="Transparent"}
        // Render into depth buffer only
        Pass {
            ZWrite On
            Blend SrcAlpha OneMinusSrcAlpha
            ColorMask RGB
            Cull off
            Material {
                Diffuse [_Color]
                Ambient [_Color]
            }
            Lighting On
            SetTexture [_MainTex] {
                Combine texture * primary DOUBLE, texture * primary
            }
        } 
    }
}

IMPORTANT: If you’ll find ant improvement in the shader or any faults in the video, please let me know.