Profile Manager Menu is added.

posted in: Weekly Progress | 0

Profile manager is added to store the Players information like name and his score  in the database of the game.

 

 

 

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text;
using System;

public class ProfileManager : MonoBehaviour
{
public InputField UserName;
public GameObject ProfileMenu;
public GameObject MainMenu;
public static ProfileManager Instance {get; private set;}
public ProfileSetting ProSettings;
private char[] _letters = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890”.ToCharArray();

public struct ProfileSetting
{
public string Name;
public string UniqueKey;
public string FinalNameAndKey;
public int HighestScore;

public ProfileSetting(string name , string uniqueKey,string finalNameAndKey,int highestScore )
{
Name = name;
UniqueKey = uniqueKey;
FinalNameAndKey = finalNameAndKey;
HighestScore = highestScore;
}
}

public void Awake()
{
//for debugging
//ResetSettings();
ProSettings = new ProfileSetting(“”, “”, “”, 0);
Instance = this;
}

void Start()
{
LoadSettingsOfProfile();

if (ProSettings.Name == String.Empty)
{
ProfileMenu.SetActive(true);
}
else
{
ProfileMenu.SetActive(false);
MainMenu.SetActive(true);
}
}

public void LoadSettingsOfProfile()
{
ProSettings.Name = PlayerPreffManager.Instance.LoadString(“ProfileName”);
ProSettings.UniqueKey = PlayerPreffManager.Instance.LoadString(“ProfileKey”);
ProSettings.FinalNameAndKey = PlayerPreffManager.Instance.LoadString(“ProfileNameAndKey”);
ProSettings.HighestScore = PlayerPreffManager.Instance.LoadInt(“HighestScore”);
}

public void SaveSettingsOfProfile()
{
PlayerPreffManager.Instance.SaveString(“ProfileName”, ProSettings.Name);
PlayerPreffManager.Instance.SaveString(“ProfileKey”, ProSettings.UniqueKey);
PlayerPreffManager.Instance.SaveString(“ProfileNameAndKey”, ProSettings.FinalNameAndKey);
PlayerPreffManager.Instance.SaveInt(“HighestScore”, ProSettings.HighestScore);
}

public void OnButtonClickOk()
{
SetProfileName();
}

public void SetProfileName()
{
if(UserName.text == String.Empty)
UserName.text = “Player-“+GenerateString(6);
ProSettings.Name = UserName.text;
Debug.Log(“your name is ” + UserName.text);
ProSettings.UniqueKey = GenerateString(10);
Debug.Log(“your unique id is ” + ProSettings.UniqueKey);
ProSettings.FinalNameAndKey = ProSettings.Name + ProSettings.UniqueKey;
Debug.Log(“your name and key is ” + ProSettings.FinalNameAndKey);
ProSettings.HighestScore = 0;

SaveSettingsOfProfile();
if( (GameManager.Instance.GetPlatform() != GameManager.ePlatform.Platform_KONGREGATE) && (GameManager.Instance.GetPlatform() != GameManager.ePlatform.Platform_FACEBOOK))
DBManager.Instance.AddPlayerInfo(ProSettings.Name,ProSettings.FinalNameAndKey, ProSettings.HighestScore , true);
ProfileMenu.SetActive(false);
MainMenu.SetActive(true);
}

public string GenerateString (int ValueForString)
{
string randomstring = “”;
string finalID = “”;

for (int i = 0; i <= ValueForString; i++)
{
randomstring += _letters[UnityEngine.Random.Range(0,_letters.Length)].ToString();
}

for(int i = 0 ; i < ValueForString; i++)
{
int index = UnityEngine.Random.Range(0,randomstring.Length);
finalID += randomstring.Substring(index, 1);
}

return finalID;//value ,randomstring
}

public void ResetSettings()
{
PlayerPreffManager.Instance.SaveString(“ProfileName”, “”);
PlayerPreffManager.Instance.SaveString(“ProfileKey”, “”);
PlayerPreffManager.Instance.SaveString(“ProfileNameAndKey”, “”);
PlayerPreffManager.Instance.SaveInt(“HighestScore”, 0);
}
}

Leave a Reply