HELP Convertendo double para string , e vise versa para save e contas / double.TryParse / double.parse

Olá esse é meu primeiro post com duvida, vou tentar ser o mais claro e sucinto possível com meu problema.

Peço desculpa de ante mão pelo meu código, é meu primeiro projeto de estudo que faço.

Questão do problema:

  1. - Estou criando um script em Unity para desenvolver um app para android, um jogo tipo idle

 Estou usando double para as contas matemáticas e armazenamento de grandes números.

   2. - Pesquisando descobri que iria precisar converter as variáveis de double para string, para poder armazenar os dados em um save.

 Depois  retornar novamente para double para manipular os dados matematicamente.

   3. - Pesquisando descobre como fazer as conversões.

 coins = double.Parse(strcoins);  Ou  coins = double.TryParse

 strcoins = Convert.ToString(coins);   

Até aqui estava tudo indo bem e alguns dados ja estavam sendo convertidos e armazenados com sucesso, ai que veio o Problema em si.

   4. - PROBLEMA: Alguns numero convertidos em string, quando era convertido de novo para double aparecia um problema de numeração por região, e acabava me retornando um valor null que não pode ser lido obvio.

 Pesquisando encontrei o método double.TryParse

 Para corrigir o problema de região tornando as letras convertidas em números com um formato CultureInfo.InvariantCulture.

Ex: double.TryParse( "0.0000", numberStyle, CultureInfo.InvariantCulture, out myVar)

Minha duvida é para implementar esse código corretamente, estou tendo erros de argumentos

Eu ia colocar uma foto para ficar mais claro, mas não posso postar fotos aqui ainda.

Segue abaixo o script completo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Web;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System;
using System.Numerics;
using System.Globalization;

[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class UserGameSettings : MonoBehaviour

{
// Variaveis Geral

// Objetos de Texto

public Text coinsText;
public Text coinsPerSecText;
public Text coinsPerClickText;
public Text clickUpgradeText;

public Text production1LVLText;
public Text production2LVLText;
public Text production3LVLText;
public Text production4LVLText;

public Text production1UpgradeText;
public Text production2UpgradeText;
public Text production3UpgradeText;
public Text production4UpgradeText;

public Text production1CoinsPerSecText;
public Text production2CoinsPerSecText;
public Text production3CoinsPerSecText;
public Text production4CoinsPerSecText;

public Text nometeste;
public Text nometeste2;
// public Text ButtonCompraMultiText; // Creio que nao vá precisar

// Variavel de Controle

public double coins; // Total de Moeda

public double coinsPerSec; // Total de moedas por Seg
public double coinsPerClick; // Total de coisn por click
public double clickValue; // Controle de valor do click

public double production1; // Armazena valor de coins por seg ganho
public double production2;
public double production3;
public double production4;

public double production1LVL; // Quantos Coins por seg esta gerando
public double production2LVL;
public double production3LVL;
public double production4LVL;

public double production1UpgradeMult; // Mutiplicador de produção mutavel
public double production2UpgradeMult;
public double production3UpgradeMult;
public double production4UpgradeMult;
public double clickUpgradeMult;

// Variaveis do botão multiplicador de compras 10x 25x 50x 100x e Max
public double ButtonCompraMult10;
public double ButtonCompraMult25;
public double ButtonCompraMult50;
public double ButtonCompraMult100;
public double ButtonCompraMultMax;


// Variaveis de Status

public double clickUpgradeLVL; // Quantas vezes comprou o upgrade de click

public double productionUpgrade1LVL; // Quantas vezes comprou o upgrade de produção
public double productionUpgrade2LVL;
public double productionUpgrade3LVL;
public double productionUpgrade4LVL;

public double production1Power; // Quanto sera adicionado na próxima compra
public double production2Power;
public double production3Power;
public double production4Power;

// Variaveis de Compra

public double clickUpgradeCost; // Quanto custa o upgrade de click

public double production1UpgradeCost; // Quanto custa o upgrade de produção
public double production2UpgradeCost;
public double production3UpgradeCost;
public double production4UpgradeCost;


// *** STRINGS COPIA DAS DOUBLE ***

public static string strcoins; // Total de Moeda

public string strcoinsPerSec; // Total de moedas por Seg
public string strcoinsPerClick; // Total de coisn por click
public string strclickValue; // Controle de valor do click

public string strproduction1; // Armazena valor de coins por seg ganho
public string strproduction2;
public string strproduction3;
public string strproduction4;

public string strproduction1LVL; // Quantos Coins por seg esta gerando
public string strproduction2LVL;
public string strproduction3LVL;
public string strproduction4LVL;

public string strproduction1UpgradeMult; // Mutiplicador de produção mutavel
public string strproduction2UpgradeMult;
public string strproduction3UpgradeMult;
public string strproduction4UpgradeMult;
public string strclickUpgradeMult;

// Variaveis do botão multiplicador de compras 10x 25x 50x 100x e Max
public string strButtonCompraMult10;
public string strButtonCompraMult25;
public string strButtonCompraMult50;
public string strButtonCompraMult100;
public string strButtonCompraMultMax;


// Variaveis de Status

public string strclickUpgradeLVL; // Quantas vezes comprou o upgrade de click

public string strproductionUpgrade1LVL; // Quantas vezes comprou o upgrade de produção
public string strproductionUpgrade2LVL;
public string strproductionUpgrade3LVL;
public string strproductionUpgrade4LVL;

public string strproduction1Power; // Quanto sera adicionado na próxima compra
public string strproduction2Power;
public string strproduction3Power;
public string strproduction4Power;

// Variaveis de Compra

public string strclickUpgradeCost; // Quanto custa o upgrade de click

public string strproduction1UpgradeCost; // Quanto custa o upgrade de produção
public string strproduction2UpgradeCost;
public string strproduction3UpgradeCost;
public string strproduction4UpgradeCost;

// *** FIM DAS STRINGS DAS DOUBLES ***

public GameObject X3Berrys;
public GameObject X2Mushrooms;
public GameObject X1eMeioJellyBear;
public GameObject X1eMeioIceCream;
public GameObject X3ClickUpgrade;

// Start is called before the first frame update
void Start()
{
if (coins == 0 && strcoins == "")
{

clickValue = 1;
coinsPerSec = 0;
coinsPerClick = 1;
clickUpgradeLVL = 0;


production1 = 0; //Geração de $ por seg da produção
production2 = 0;
production3 = 0;
production4 = 0;

production1LVL = 0; //Quantos upgrades foi comprado
production2LVL = 0;
production3LVL = 0;
production4LVL = 0;

clickUpgradeCost = 10; //Quanto custa o proximo Upgrade
production1UpgradeCost = 25;
production2UpgradeCost = 500;
production3UpgradeCost = 2500;
production4UpgradeCost = 15000;

production1UpgradeMult = 1.03; //Valor de Multiplicador de produção que sera modificado
production2UpgradeMult = 1.03;
production3UpgradeMult = 1.03;
production4UpgradeMult = 1.03;
clickUpgradeMult = 1.03;

production1Power = 1; // Quanto vai ganhar com upgrade
production2Power = 5;
production3Power = 15;
production4Power = 30;
}

else
{
// Load Game

strcoins = PlayerPrefs.GetString("strcoins");
strcoinsPerSec = PlayerPrefs.GetString("strcoinsPerSec");

strcoinsPerClick = PlayerPrefs.GetString("strcoinsPerClick");
strclickUpgradeCost = PlayerPrefs.GetString("strclickUpgradeCost");
strclickValue = PlayerPrefs.GetString("strclickValue");
strclickUpgradeLVL = PlayerPrefs.GetString("strclickUpgradeLVL");
strclickUpgradeMult = PlayerPrefs.GetString("strclickUpgradeMult");

strproduction1LVL = PlayerPrefs.GetString("strproduction1LVL");
strproduction2LVL = PlayerPrefs.GetString("strproduction2LVL");
strproduction3LVL = PlayerPrefs.GetString("strproduction3LVL");
strproduction4LVL = PlayerPrefs.GetString("strproduction4LVL");

strproduction1UpgradeCost = PlayerPrefs.GetString("strproduction1UpgradeCost");
strproduction2UpgradeCost = PlayerPrefs.GetString("strproduction2UpgradeCost");
strproduction3UpgradeCost = PlayerPrefs.GetString("strproduction3UpgradeCost");
strproduction4UpgradeCost = PlayerPrefs.GetString("strproduction4UpgradeCost");

strproduction1Power = PlayerPrefs.GetString("strproduction1Power");
strproduction2Power = PlayerPrefs.GetString("strproduction2Power");
strproduction3Power = PlayerPrefs.GetString("strproduction3Power");
strproduction4Power = PlayerPrefs.GetString("strproduction4Power");

strproduction1UpgradeMult = PlayerPrefs.GetString("strproduction1UpgradeMult");
strproduction2UpgradeMult = PlayerPrefs.GetString("strproduction2UpgradeMult");
strproduction3UpgradeMult = PlayerPrefs.GetString("strproduction3UpgradeMult");
strproduction4UpgradeMult = PlayerPrefs.GetString("strproduction4UpgradeMult");

strproduction1 = PlayerPrefs.GetString("strproduction1");
strproduction2 = PlayerPrefs.GetString("strproduction2");
strproduction3 = PlayerPrefs.GetString("strproduction3");
strproduction4 = PlayerPrefs.GetString("strproduction4");

// END LOAD GAME


// INICIO DA CONVERSAO DA STRING PARA DOUBLE #$#
coins = double.Parse(strcoins);
coinsPerSec = double.Parse(strcoinsPerSec);
coinsPerClick = double.Parse(strcoinsPerClick);
clickValue = double.Parse(strclickValue);

production1 = double.Parse(strproduction1);
production2 = double.Parse(strproduction2);
production3 = double.Parse(strproduction3);
production4 = double.Parse(strproduction4);

production1LVL = double.Parse(strproduction1LVL);
production2LVL = double.Parse(strproduction2LVL);
production3LVL = double.Parse(strproduction3LVL);
production4LVL = double.Parse(strproduction4LVL);

// production1UpgradeMult = double.TryParse("N1", NumberStyles.Number, CultureInfo.InvariantCulture, out strproduction1UpgradeMult);

production1UpgradeMult = double.Parse(strproduction1UpgradeMult);
production2UpgradeMult = double.Parse(strproduction2UpgradeMult);
production3UpgradeMult = double.Parse(strproduction3UpgradeMult);
production4UpgradeMult = double.Parse(strproduction4UpgradeMult);
clickUpgradeMult = double.Parse(strclickUpgradeMult);

ButtonCompraMult10 = double.Parse(strButtonCompraMult10);
ButtonCompraMult25 = double.Parse(strButtonCompraMult25);
ButtonCompraMult50 = double.Parse(strButtonCompraMult50);
ButtonCompraMult100 = double.Parse(strButtonCompraMult100);
ButtonCompraMultMax = double.Parse(strButtonCompraMultMax);

clickUpgradeLVL = double.Parse(strclickUpgradeLVL);

productionUpgrade1LVL = double.Parse(strproductionUpgrade1LVL);
productionUpgrade2LVL = double.Parse(strproductionUpgrade2LVL);
productionUpgrade3LVL = double.Parse(strproductionUpgrade3LVL);
productionUpgrade4LVL = double.Parse(strproductionUpgrade4LVL);

production1Power = double.Parse(strproduction1Power);
production2Power = double.Parse(strproduction2Power);
production3Power = double.Parse(strproduction3Power);
production4Power = double.Parse(strproduction4Power);

clickUpgradeCost = double.Parse(strclickUpgradeCost);

production1UpgradeCost = double.Parse(strproduction1UpgradeCost);
production2UpgradeCost = double.Parse(strproduction2UpgradeCost);
production3UpgradeCost = double.Parse(strproduction3UpgradeCost);
production4UpgradeCost = double.Parse(strproduction4UpgradeCost);

}

// FIM DA CONVERSAO DE STRINGS

// ---------------------------------------- # # -------------------------------------
//Convertendo STRING PARA DOUBLE E VISE VERSA

string STR1 = "1.234";
string STR2;
double DOUBLE1;
double DOUBLE2 = 9.876;

DOUBLE1 = double.Parse(STR1); //Convertendo STRING para DOUBLE
STR2 = Convert.ToString(DOUBLE2); //Convertendo Double para STRING
nometeste.text = "Resultado: " + DOUBLE1;
nometeste2.text = "Resultado2: " + STR2;

// ---------------------------------------- # # -------------------------------------


}

// Update is called once per frame

void Update()
{

// Soma total de produção por seg
coinsPerSec = production1 + production2 + production3 + production4;

// Atualiza o total de coins
coinsText.text = "$ " + coins.ToString("N1");

// Atualiza o total de coins por seg
coinsPerSecText.text = "Golds/s: " + coinsPerSec.ToString("N1");

// Atualiza total de coins por click
coinsPerClickText.text = "lvl: " + clickUpgradeLVL.ToString("N1") + "\nGold Click: " + clickValue.ToString("N1");

// Atualiza total de upgrade por click
clickUpgradeText.text = "Upgrade Click\nCost: " + clickUpgradeCost.ToString("N1") + "\nPower: " + coinsPerClick.ToString("N1");


// Atualiza quantas vezes comprou upgrade de Produção
production1LVLText.text = "lvl: " + production1LVL.ToString("N1");
production2LVLText.text = "lvl: " + production2LVL.ToString("N1");
production3LVLText.text = "lvl: " + production3LVL.ToString("N1");
production4LVLText.text = "lvl: " + production4LVL.ToString("N1");


// Atualiza Status de Valores e Upgrades
production1UpgradeText.text = "Red Berrys\nCost: " + production1UpgradeCost.ToString("N1") + " \nPower: " + production1Power.ToString("N1");
production2UpgradeText.text = "Mushrooms\nCost: " + production2UpgradeCost.ToString("N1") + " \nPower: " + production2Power.ToString("N1");
production3UpgradeText.text = "Jelly Bear\nCost: " + production3UpgradeCost.ToString("N1") + " \nPower: " + production3Power.ToString("N1");
production4UpgradeText.text = "Ice Cream\nCost: " + production4UpgradeCost.ToString("N1") + " \nPower: " + production4Power.ToString("N1");
//
production1CoinsPerSecText.text = "Golds/s: \n" + production1.ToString("N1");
production2CoinsPerSecText.text = "Golds/s: \n" + production2.ToString("N1");
production3CoinsPerSecText.text = "Golds/s: \n" + production3.ToString("N1");
production4CoinsPerSecText.text = "Golds/s: \n" + production4.ToString("N1");

//Calculo de Gold mais Golds por segundo
coins += coinsPerSec * Time.deltaTime;

}

// Buttons de Compra

public void ButtonClick()
{

coins += clickValue;

}

public void ButtonUpgradeClick()
{
if (coins >= clickUpgradeCost)
{

coins -= clickUpgradeCost;
clickValue += coinsPerClick;
clickUpgradeLVL++;
clickUpgradeCost *= 1.03;
coinsPerClick *= clickUpgradeMult;

if (clickUpgradeLVL == 100)
{
clickValue *= 3;
coinsPerClick *= 3;
clickUpgradeCost /= 1.05; //Precisa ser testado a diminuiçao do custo
clickUpgradeMult += 1.05;

X3ClickUpgrade.SetActive(true);

}

}

}

public void ButtonProduction1()
{
if (coins >= production1UpgradeCost)
{
coins -= production1UpgradeCost;
production1LVL++;
production1 += production1Power;

production1Power *= clickUpgradeMult;
production1UpgradeCost *= 1.03;


if (production1LVL == 100)
{
production1 *= 3;
production1Power *= 3;
production1UpgradeMult += 1.5;
X3Berrys.SetActive(true);

}

}

}

public void ButtonProduction2()
{
if (coins >= production2UpgradeCost)
{
coins -= production2UpgradeCost;
production2LVL++;
production2 += production2Power;

production2Power *= production2UpgradeMult;
production2UpgradeCost *= 1.03;

if (production2LVL == 100)
{
production2 *= 2;
production2Power *= 2;
production2UpgradeMult += 1.5;
X2Mushrooms.SetActive(true);
}


}

}

public void ButtonProduction3()
{
if (coins >= production3UpgradeCost)
{
coins -= production3UpgradeCost;
production3LVL++;
production3 += production3Power;

production3Power *= production3UpgradeMult;
production3UpgradeCost *= 1.03;


if (production3LVL == 100)
{
production3 *= 1.5;
production3Power *= 1.5;
production3UpgradeMult += 1.5;
X1eMeioJellyBear.SetActive(true);
}


}

}

public void ButtonProduction4()
{
if (coins >= production4UpgradeCost)
{
coins -= production4UpgradeCost;
production4LVL++;
production4 += production4Power;

production4Power *= production4UpgradeMult;
production4UpgradeCost *= 1.03;

if (production4LVL == 100)
{
production4 *= 1.5;
production4Power *= 1.5;
production4UpgradeMult += 1.5;
X1eMeioIceCream.SetActive(true);

}

}

}


public void ButtonNewGame()
{
coins = 0;
}


public void LateUpdate()

{

// Convertendo DOUBLES EM STRING PARA SAVE !*!

strcoins = Convert.ToString(coins);
strcoinsPerSec = Convert.ToString(coinsPerSec);
strcoinsPerClick = Convert.ToString(coinsPerClick);
strclickValue = Convert.ToString(clickValue);

strproduction1 = Convert.ToString(production1);
strproduction2 = Convert.ToString(production2);
strproduction3 = Convert.ToString(production3);
strproduction4 = Convert.ToString(production4);

strproduction1LVL = Convert.ToString(production1LVL);
strproduction2LVL = Convert.ToString(production2LVL);
strproduction3LVL = Convert.ToString(production3LVL);
strproduction4LVL = Convert.ToString(production4LVL);

strproduction1UpgradeMult = Convert.ToString(production1UpgradeMult);
strproduction2UpgradeMult = Convert.ToString(production2UpgradeMult);
strproduction3UpgradeMult = Convert.ToString(production3UpgradeMult);
strproduction4UpgradeMult = Convert.ToString(production4UpgradeMult);

strclickUpgradeMult = Convert.ToString(clickUpgradeMult);

strButtonCompraMult10 = Convert.ToString(ButtonCompraMult10);
strButtonCompraMult25 = Convert.ToString(ButtonCompraMult25);
strButtonCompraMult50 = Convert.ToString(ButtonCompraMult50);
strButtonCompraMult100 = Convert.ToString(ButtonCompraMult100);
strButtonCompraMultMax = Convert.ToString(ButtonCompraMultMax);

strclickUpgradeLVL = Convert.ToString(clickUpgradeLVL);

strproductionUpgrade1LVL = Convert.ToString(productionUpgrade1LVL);
strproductionUpgrade2LVL = Convert.ToString(productionUpgrade2LVL);
strproductionUpgrade3LVL = Convert.ToString(productionUpgrade3LVL);
strproductionUpgrade4LVL = Convert.ToString(productionUpgrade4LVL);

strproduction1Power = Convert.ToString(production1Power);
strproduction2Power = Convert.ToString(production2Power);
strproduction3Power = Convert.ToString(production3Power);
strproduction4Power = Convert.ToString(production4Power);


strclickUpgradeCost = Convert.ToString(clickUpgradeCost);

strproduction1UpgradeCost = Convert.ToString(production1UpgradeCost);
strproduction2UpgradeCost = Convert.ToString(production2UpgradeCost);
strproduction3UpgradeCost = Convert.ToString(production3UpgradeCost);
strproduction4UpgradeCost = Convert.ToString(production4UpgradeCost);

// FIM DA CONVERSAO PARA STRING !*!
// SAVE GAME

PlayerPrefs.SetString("strcoins", strcoins);

PlayerPrefs.SetString("strcoinsPerSec", strcoinsPerSec);

PlayerPrefs.SetString("strcoinsPerClick", strcoinsPerClick);
PlayerPrefs.SetString("strclickUpgradeCost", strclickUpgradeCost);
PlayerPrefs.SetString("strclickValue", strclickValue);
PlayerPrefs.SetString("strclickUpgradeLVL", strclickUpgradeLVL);

PlayerPrefs.SetString("strproduction1LVL", strproduction1LVL);
PlayerPrefs.SetString("strproduction2LVL", strproduction2LVL);
PlayerPrefs.SetString("strproduction3LVL", strproduction3LVL);
PlayerPrefs.SetString("strproduction4LVL", strproduction4LVL);

PlayerPrefs.SetString("strproduction1UpgradeCost", strproduction1UpgradeCost);
PlayerPrefs.SetString("strproduction2UpgradeCost", strproduction2UpgradeCost);
PlayerPrefs.SetString("strproduction3UpgradeCost", strproduction3UpgradeCost);
PlayerPrefs.SetString("strproduction4UpgradeCost", strproduction4UpgradeCost);

PlayerPrefs.SetString("strproduction1Power", strproduction1Power);
PlayerPrefs.SetString("strproduction2Power", strproduction2Power);
PlayerPrefs.SetString("strproduction3Power", strproduction3Power);
PlayerPrefs.SetString("strproduction4Power", strproduction4Power);

PlayerPrefs.SetString("strproduction1Power", strproduction1UpgradeMult);
PlayerPrefs.SetString("strproduction2Power", strproduction2UpgradeMult);
PlayerPrefs.SetString("strproduction3Power", strproduction3UpgradeMult);
PlayerPrefs.SetString("strproduction4Power", strproduction4UpgradeMult);

PlayerPrefs.SetString("strproduction1", strproduction1);
PlayerPrefs.SetString("strproduction2", strproduction2);
PlayerPrefs.SetString("strproduction3", strproduction3);
PlayerPrefs.SetString("strproduction4", strproduction4);

PlayerPrefs.Save();

// INICIO DA CONVERSAO DA STRING PARA DOUBLE #$#
coins = double.Parse(strcoins);
coinsPerSec = double.Parse(strcoinsPerSec);
coinsPerClick = double.Parse(strcoinsPerClick);
clickValue = double.Parse(strclickValue);

production1 = double.Parse(strproduction1);
production2 = double.Parse(strproduction2);
production3 = double.Parse(strproduction3);
production4 = double.Parse(strproduction4);

production1LVL = double.Parse(strproduction1LVL);
production2LVL = double.Parse(strproduction2LVL);
production3LVL = double.Parse(strproduction3LVL);
production4LVL = double.Parse(strproduction4LVL);

production1UpgradeMult = double.Parse(strproduction1UpgradeMult);
production2UpgradeMult = double.Parse(strproduction2UpgradeMult);
production3UpgradeMult = double.Parse(strproduction3UpgradeMult);
production4UpgradeMult = double.Parse(strproduction4UpgradeMult);
clickUpgradeMult = double.Parse(strclickUpgradeMult);

ButtonCompraMult10 = double.Parse(strButtonCompraMult10);
ButtonCompraMult25 = double.Parse(strButtonCompraMult25);
ButtonCompraMult50 = double.Parse(strButtonCompraMult50);
ButtonCompraMult100 = double.Parse(strButtonCompraMult100);
ButtonCompraMultMax = double.Parse(strButtonCompraMultMax);

clickUpgradeLVL = double.Parse(strclickUpgradeLVL);

productionUpgrade1LVL = double.Parse(strproductionUpgrade1LVL);
productionUpgrade2LVL = double.Parse(strproductionUpgrade2LVL);
productionUpgrade3LVL = double.Parse(strproductionUpgrade3LVL);
productionUpgrade4LVL = double.Parse(strproductionUpgrade4LVL);

production1Power = double.Parse(strproduction1Power);
production2Power = double.Parse(strproduction2Power);
production3Power = double.Parse(strproduction3Power);
production4Power = double.Parse(strproduction4Power);

clickUpgradeCost = double.Parse(strclickUpgradeCost);

production1UpgradeCost = double.Parse(strproduction1UpgradeCost);
production2UpgradeCost = double.Parse(strproduction2UpgradeCost);
production3UpgradeCost = double.Parse(strproduction3UpgradeCost);
production4UpgradeCost = double.Parse(strproduction4UpgradeCost);
// END SAVE GAME

}

}

Exibições: 14

Anexos

© 2019   Criado por Ramon Durães.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço