Showing posts with label English. Show all posts
Showing posts with label English. Show all posts

Monday, August 28, 2017

Using Player Prefs on Unity outside play mode

Often I like to use Player Prefs to store user data, because is really easy to use. But what happens when we want to delete it?. I used to put this line of code:

PlayerPrefs.DeleteAll();

Complite, execute, erase line of code, compile and execute again.

Now I know better!, so we are going to setup a Menu Item to execute this at any time.

Lets create a new C# script, name it  DeletePlayerPrefsData  and add the following code:

using UnityEngine;
using UnityEditor;

public class DeletePlayerPrefsData {
[MenuItem("Assets/Delete Player Prefs")]
public static void DeletePlayerPrefs() {
PlayerPrefs.DeleteAll();
}
}

Here we put the location of the script function on the menu.
[MenuItem("Assets/Delete Player Prefs")]

Lastly we declare a static function just below  [MenuItem]  which would be the function that will execute when  Delete Player Prefs  is clicked.



Wednesday, August 23, 2017

Define code regions for specific platforms

Español

Let's say we want to execute especific code for a certain platform, usually this problem arises when we are targeting multiple platforms and some features/characteristics are different.

Often we want to do this with the input between PC and Mobile, so if we have this on PC.

void Update () {
    if (Input.GetKey(KeyCode.D)) { //Move right
        rigidbody2D.velocity = new Vector2(speed0.0f);
    }
    else if (Input.GetKey(KeyCode.A)) { //Move left
        rigidbody2D.velocity = new Vector2(-speed0.0f);
    }
    else { //Does not move
        rigidbody2D.velocity = new Vector2(0.0f0.0f);
    }
}

On mobile this won't work.

We could add the mobile input code next to it but everything will be executed, so here's where we use Platform #define directives.

    void Update () {
#if UNITY_STANDALONE || UNITY_WEBGL || UNITY_EDITOR //PC Input
        if (Input.GetKey(KeyCode.D)) { //Move right
            rigidbody2D.velocity = new Vector2(speed0.0f);
        }
        else if (Input.GetKey(KeyCode.A)) { //Move left
            rigidbody2D.velocity = new Vector2(-speed0.0f);
        }
        else { //Does not move
            rigidbody2D.velocity = new Vector2(0.0f0.0f);
        }
#elif UNITY_ANDROID || UNITY_IOS //Mobile Input
        if (Input.GetTouch(0).position.x > Screen.width / 2.0f) { //Move right
            rigidbody2D.velocity = new Vector2(speed0.0f);
        }
        else if (Input.GetTouch(0).position.x < Screen.width / 2.0f) { //Move left
            rigidbody2D.velocity = new Vector2(-speed0.0f);
        }
        else { //Does not move
            rigidbody2D.velocity = new Vector2(0.0f0.0f);
        }
#endif
    }

In this we define certain code regions that will only be compiled if the current selected platform matches the property inside the  #if / #elif  statements.


For more info on which property to use check the table.

Usando Player Prefs fuera del modo de juego

Muchas veces usamos la clase Player Prefs para guardar datos. Pero que pasa cuando queremos borrar los datos guardados?. Yo solia colocar e...