PFonseca

Developer

View site on GitHub Statuses Archives

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
scoreValue+=10;

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

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

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 = 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;
        }
      }
    }

  }
}

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

Unity 5

New Features:
  • High-end shaders
  • Cross-platform real-time Global Illumination
  • New sound mixers, snapshots and predefined effects
  • WebGL
  • 64-bit Unity editor
  • Unity homescreen
  • 2D Physics effectors
  • PhysX 3.3
  • New job queue
  • Level streaming
  • SpeedTree support
  • New Mecanim features
  • Plugin inspector
  • Navmesh updates
  • Incremental Asset Bundles

unity-5-4 unity-5 unity-5-2 unity-5-3 unity-5-1 Source:
  • http://unity3d.com/5
  • http://www.itsartmag.com/features/unity-5-gdc-2014-preview/
  • Published on 07/01/2015 at 00h51 by pfonseca, tags

    Accelerometer Input Unity

    using UnityEngine;
    using System.Collections;
    
    public class AccelerometerInput : MonoBehaviour
    {
        void Update ()
        {
    
            transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
        }
    }

    Published on 07/01/2015 at 00h24 by pfonseca, tags


    Powered by Publify