Aplicación base para móviles

En esta entrada del bloc voy a explicar cómo hacer una base para un proyecto móvil que nos sirva para futuros proyectos. En concreto yo utilizo esta base en Unity para hacer juegos móviles para ahorrarme algo de tiempo y centrarme en el juego.


En concreto el proyecto consiste en 3 escenas que definen la pantalla de Splash, el título del juego y el menú principal. Evidentemente esto sólo es una base, en el titulo podemos poner más cosas: efectos, animaciones, etc. Pero esto ya será en una segunda fase. Ahora solo necesitamos una estructura que funcionen bien en dispositivos móviles y que nos cargue el fichero de configuración del juego y actualice las variables globales del proyecto.



Clase de los datos de configuración:
 using System.Collections.Generic;  
 using System;  
 [Serializable]  
 public class PlayerInfo  
 {  
   public int version = 1;  
   public string gameDateFirstTime;  
   public string playDateFirstTime;  
   public int sessionsCount = 0;  
   public string language = "en";  
   public bool soundPlay = true;    
 }  

Variables globales actuales:
 using UnityEngine;  
 public class GlobalInfo : MonoBehaviour  
 {  
   //General Config  
   public static string configFile = "AttilaCfg";    
   public static bool gameFirstTime;  
   public static bool playFirstTime;  
   public static int sessionsCount;  
   public static int gamesCount;  
   public static string language;  
   public static bool soundPlay = true;  
   //Game  
 }  

Carga de datos:
 using UnityEngine;  
 using System.IO;  
 using System;  
 public class LoadConfig : MonoBehaviour {  
   private string configFileName;  
   private string highScoreFileName;  
   private string fileName;  
      // Use this for initialization  
      void Awake ()  
   {  
     //Configuration  
     configFileName = GlobalInfo.configFile;  
     fileName = Path.Combine(Application.persistentDataPath, "data");  
     fileName = Path.Combine(fileName, configFileName + ".txt");  
     if (!File.Exists(fileName))  
     {  
       PlayerInfo saveData = new PlayerInfo();  
       saveData.gameDateFirstTime = DateTime.Now.ToBinary().ToString();  
       saveData.playDateFirstTime = "";  
       //Save data from PlayerInfo to a file named players  
       DataSaver.saveData(saveData, configFileName);  
       GlobalInfo.gameFirstTime = true;  
       GlobalInfo.playFirstTime = true;  
       GlobalInfo.language = saveData.language;  
       GlobalInfo.soundPlay = true;  
       GlobalInfo.sessionsCount = 0;  
       GlobalInfo.gamesCount = 0;  
     } else  
     {  
       PlayerInfo loadedData = DataSaver.loadData<PlayerInfo>(configFileName);  
       if (loadedData == null)  
       {  
         return;  
       }  
       GlobalInfo.gameFirstTime = false;  
       if (loadedData.playDateFirstTime != "")  
       {  
         GlobalInfo.playFirstTime = false;  
       } else  
       {  
         GlobalInfo.playFirstTime = true;  
       }  
       GlobalInfo.language = loadedData.language;  
       GlobalInfo.soundPlay = loadedData.soundPlay;  
       GlobalInfo.sessionsCount = loadedData.sessionsCount;  
       GlobalInfo.gamesCount = 0;  
     }  
   }  
 }  

Salvar, leer y convertir datos en disco:
 using UnityEngine;  
 using System;  
 using System.IO;  
 using System.Text;  
 public class DataSaver  
 {  
   //Save Data  
   public static void saveData<T>(T dataToSave, string dataFileName)  
   {  
     string tempPath = Path.Combine(Application.persistentDataPath, "data");  
     tempPath = Path.Combine(tempPath, dataFileName + ".txt");  
     //Convert To Json then to bytes  
     string jsonData = JsonUtility.ToJson(dataToSave, true);  
     byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);  
     //Create Directory if it does not exist  
     if (!Directory.Exists(Path.GetDirectoryName(tempPath)))  
     {  
       Directory.CreateDirectory(Path.GetDirectoryName(tempPath));  
     }  
     try  
     {  
       File.WriteAllBytes(tempPath, jsonByte);  
       Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));  
     }  
     catch (Exception e)  
     {  
       Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/", "\\"));  
       Debug.LogWarning("Error: " + e.Message);  
     }  
   }  
   //Load Data  
   public static T loadData<T>(string dataFileName)  
   {  
     string tempPath = Path.Combine(Application.persistentDataPath, "data");  
     tempPath = Path.Combine(tempPath, dataFileName + ".txt");  
     //Exit if Directory or File does not exist  
     if (!Directory.Exists(Path.GetDirectoryName(tempPath)))  
     {  
       Debug.LogWarning("Directory does not exist");  
       return default(T);  
     }  
     if (!File.Exists(tempPath))  
     {  
       Debug.Log("File does not exist");  
       return default(T);  
     }  
     //Load saved Json  
     byte[] jsonByte = null;  
     try  
     {  
       jsonByte = File.ReadAllBytes(tempPath);  
       Debug.Log("Loaded Data from: " + tempPath.Replace("/", "\\"));  
     }  
     catch (Exception e)  
     {  
       Debug.LogWarning("Failed To Load Data from: " + tempPath.Replace("/", "\\"));  
       Debug.LogWarning("Error: " + e.Message);  
     }  
     //Convert to json string  
     string jsonData = Encoding.ASCII.GetString(jsonByte);  
     //Convert to Object  
     object resultValue = JsonUtility.FromJson<T>(jsonData);  
     return (T)Convert.ChangeType(resultValue, typeof(T));  
   }  
   public static bool deleteData(string dataFileName)  
   {  
     bool success = false;  
     //Load Data  
     string tempPath = Path.Combine(Application.persistentDataPath, "data");  
     tempPath = Path.Combine(tempPath, dataFileName + ".txt");  
     //Exit if Directory or File does not exist  
     if (!Directory.Exists(Path.GetDirectoryName(tempPath)))  
     {  
       Debug.LogWarning("Directory does not exist");  
       return false;  
     }  
     if (!File.Exists(tempPath))  
     {  
       Debug.Log("File does not exist");  
       return false;  
     }  
     try  
     {  
       File.Delete(tempPath);  
       Debug.Log("Data deleted from: " + tempPath.Replace("/", "\\"));  
       success = true;  
     }  
     catch (Exception e)  
     {  
       Debug.LogWarning("Failed To Delete Data: " + e.Message);  
     }  
     return success;  
   }  
 }  

La secuencia básica consiste en:
  1. Carga de la escena Splash
  2. Carga de la escena Título
  3. Carga del fichero de configuración
  4. Actualización de las variables globales
  5. Carga la escena del Menú principal

Vídeo completo del proceso:


Para hacer la carga entre escenas utilizo una función más una animación parar crear un efecto Fade entre ellas, tanto en la entrada como en la salida y en la escena del título un slider para cargar la escena del juego en segundo plano para mostrar la evolución en el slider.

Carga asíncrona de escenas:
 using System.Collections;  
 using UnityEngine;  
 using UnityEngine.UI;  
 using UnityEngine.SceneManagement;  
 public class LoadingBar : MonoBehaviour {  
   private bool loadScene = false;  
   public string loadingSceneName;  
   public float waitSecondsPrev;  
   public float waitSecondsPost;  
   public Slider sliderBar;  
   // Use this for initialization  
      void Start ()  
   {  
     sliderBar.gameObject.SetActive(false);  
     Invoke("Loading", waitSecondsPrev);  
   }  
      // Update is called once per frame  
      void Loading()  
   {  
        if(loadScene == false)  
     {  
       loadScene = true;  
       sliderBar.gameObject.SetActive(true);  
       StartCoroutine("LoadNewScene");  
     }  
      }  
   IEnumerator LoadNewScene()  
   {  
     yield return new WaitForSeconds(waitSecondsPost);  
     AsyncOperation async = SceneManager.LoadSceneAsync(loadingSceneName);  
     while (!async.isDone)  
     {  
       float progress = Mathf.Clamp01(async.progress / 0.9f);  
       sliderBar.value = progress;  
       yield return null;  
     }  
   }  
 }  


No hay comentarios:

Publicar un comentario

Como hacer copias de tu código de Unity con GitHub

Podriamos escribir un libro entero de las bondades de Git para el trabajo colaborativo y la gestión de versiones en un entorno como Unity. D...