View site on GitHub Statuses Archives

Swift Update 2014-08-18

Swift update with iOS 8 beta 6.

  • Added a new section about Initializer Requirements in protocols.

  • Added a new section about Class-Only Protocols.

  • Assertions can now use string interpolation. Removed a note to the contrary.

  • Updated the Concatenating Strings and Characters section to reflect the fact that String and Character values can no longer be combined with the addition operator (+) or addition assignment operator (+=). These operators are now used only with String values. Use the String type’s append method to append a single Character value onto the end of a string.

  • Added information about the availability attribute to the Declaration Attributes section.


Published on 07/01/2015 at 02h28 by pfonseca, tags ,

How to persist simple data on Unity

There is a way to persist simple data like max score. It's possible to set, get and remove data.
Available Methods:

DeleteAllRemoves all keys and values from the preferences. Use with caution.
DeleteKeyRemoves key and its corresponding value from the preferences.
GetFloatReturns the value corresponding to key in the preference file if it exists.
GetIntReturns the value corresponding to key in the preference file if it exists.
GetStringReturns the value corresponding to key in the preference file if it exists.
HasKeyReturns true if key exists in the preferences.
SaveWrites all modified preferences to disk.
SetFloatSets the value of the preference identified by key.
SetIntSets the value of the preference identified by key.
SetStringSets the value of the preference identified by key.

How to use:
// get a value stored in a constante "score"
int scoreValue = PlayerPrefs.GetInt("score");

// increment score

// Store value on a constante "score"
PlayerPrefs.SetInt("score", scoreValue);


Published on 07/01/2015 at 02h26 by pfonseca, tags

Swipe detection on Unity

This is a simple class to use swipe on mobile devices with Unity.

using UnityEngine;
using System.Collections;

public class SwipeScript : MonoBehaviour {

  private float fingerStartTime  = 0.0f;
  private Vector2 fingerStartPos =;
  private bool isSwipe = false;
  private float minSwipeDist  = 50.0f;
  private float maxSwipeTime = 0.5f;

  // Update is called once per frame
  void Update () {
    if (Input.touchCount > 0){

      foreach (Touch touch in Input.touches)
        switch (touch.phase)
        case TouchPhase.Began :
          /* this is a new touch */
          isSwipe = true;
          fingerStartTime = Time.time;
          fingerStartPos = touch.position;
        case TouchPhase.Canceled :
          /* The touch is being canceled */
          isSwipe = false;
        case TouchPhase.Ended :

          float gestureTime = Time.time - fingerStartTime;
          float gestureDist = (touch.position - fingerStartPos).magnitude;
          if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist){
            Vector2 direction = touch.position - fingerStartPos;
            Vector2 swipeType =;
            if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){
              // the swipe is horizontal:
              swipeType = Vector2.right * Mathf.Sign(direction.x);
              // the swipe is vertical:
              swipeType = Vector2.up * Mathf.Sign(direction.y);

            if(swipeType.x != 0.0f){
              if(swipeType.x > 0.0f){
                // MOVE RIGHT
                // MOVE LEFT

            if(swipeType.y != 0.0f ){
              if(swipeType.y > 0.0f){
                // MOVE UP
                // MOVE DOWN



Published on 07/01/2015 at 02h20 by pfonseca, tags

Powered by Publify