Swift

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.

Source:
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/RevisionHistory.html#//apple_ref/doc/uid/TP40014097-CH40-XID_1636

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:

DeleteAll Removes all keys and values from the preferences. Use with caution.
DeleteKey Removes key and its corresponding value from the preferences.
GetFloat Returns the value corresponding to key in the preference file if it exists.
GetInt Returns the value corresponding to key in the preference file if it exists.
GetString Returns the value corresponding to key in the preference file if it exists.
HasKey Returns true if key exists in the preferences.
Save Writes all modified preferences to disk.
SetFloat Sets the value of the preference identified by key.
SetInt Sets the value of the preference identified by key.
SetString Sets 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
scoreValue+=10;

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

Source:

http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

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 = Vector2.zero;
	
	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;
					break;
					
				case TouchPhase.Canceled :
					/* The touch is being canceled */
					isSwipe = false;
					break;
					
				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 = Vector2.zero;
						
						if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){
							// the swipe is horizontal:
							swipeType = Vector2.right * Mathf.Sign(direction.x);
						}else{
							// the swipe is vertical:
							swipeType = Vector2.up * Mathf.Sign(direction.y);
						}

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

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

					}
						
					break;
				}
			}
		}

	}
}