- Home /
How to have Database of Dictionary elements with online images???
Hi guys, I'm new in Unity and I have this kind of problem. I'm doing Android/iOS game similar to Higher Lower Game where you compare 2 elements. My goal is to have databese of elements and when player is online be able to show image attached to element. I want to have 2 modes: when i click first button in first scene, second scene will be loaded and i will compare for example speed of element and when i click second button ,second scene will be loaded but i will compare price of element. I suppose i should use JSON and give my photos to dropbox and somehow shere them to game is this good idea or it's total rubbish? Now i have dictionary with key and 2 values, float and sprite. Part of my GameManager script. Thanks for answers. using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using UnityEngine.UI; using System; using UnityEngine.SceneManagement;
public class ListManager : MonoBehaviour {
public Image carImage1;
public Image carImage2;
public Image carImage3;
public Sprite lamborghiniVeneno;
public Sprite bughattiVeyron;
public Sprite mcLarenP1;
public Sprite mcLarenF1;
public Sprite porscheCarera;
public Sprite henneseyGT;
private string randomKey1;
private string randomKey2;
private string randomKey3;
private float randomValueSpeed1;
private float randomValueSpeed2;
private float randomValueSpeed3;
private Sprite randomValueImage1;
private Sprite randomValueImage2;
private Sprite randomValueImage3;
Dictionary<string,ValueAndSpriteClass> database = new Dictionary<string, ValueAndSpriteClass>();
private class ValueAndSpriteClass
{
public float carSpeed { get; set; }
public Sprite carImage { get; set; }
}
public void AddToDatabase()
{
database.Add("Lamborghini Veneno",new ValueAndSpriteClass { carSpeed=440,carImage=lamborghiniVeneno});
database.Add("Bughatti Veyron", new ValueAndSpriteClass { carSpeed = 420, carImage = bughattiVeyron });
database.Add("Porchce Carera", new ValueAndSpriteClass { carSpeed = 400, carImage = porscheCarera });
database.Add("Hennesey GT", new ValueAndSpriteClass { carSpeed = 460, carImage = henneseyGT });
database.Add("MCLaren P1", new ValueAndSpriteClass { carSpeed = 370, carImage = mcLarenP1 });
database.Add("MCLaren F1", new ValueAndSpriteClass { carSpeed = 430, carImage = mcLarenF1 });
}
private void ChangeRandomElement1()
{
var rnd = new System.Random();
var randomEntry1 = database.ElementAt(rnd.Next(0, database.Count));
randomKey1 = randomEntry1.Key;
randomValueSpeed1 = randomEntry1.Value.carSpeed;
randomValueImage1 = randomEntry1.Value.carImage;
database.Remove(randomKey1);
if (randomKey3 != null)
{
database.Add(randomKey3, new ValueAndSpriteClass { carSpeed = randomValueSpeed3, carImage = randomValueImage3 });
}
Debug.Log(randomKey1 + randomValueSpeed1);
}
private void ChangeRandomElement2()
{
var rnd = new System.Random();
var randomEntry2 = database.ElementAt(rnd.Next(0, database.Count));
randomKey2 = randomEntry2.Key;
randomValueSpeed2 = randomEntry2.Value.carSpeed;
randomValueImage2 = randomEntry2.Value.carImage;
database.Remove(randomKey2);
if (randomKey1 != null)
{
database.Add(randomKey1, new ValueAndSpriteClass { carSpeed = randomValueSpeed1, carImage = randomValueImage1 });
}
Debug.Log(randomKey2 + randomValueSpeed2);
}
private void ChangeRandomElement3()
{
var rnd = new System.Random();
var randomEntry3 = database.ElementAt(rnd.Next(0, database.Count));
randomKey3 = randomEntry3.Key;
randomValueSpeed3 = randomEntry3.Value.carSpeed;
randomValueImage3 = randomEntry3.Value.carImage;
database.Remove(randomKey3);
if (randomKey2 != null)
{
database.Add(randomKey2, new ValueAndSpriteClass { carSpeed = randomValueSpeed2, carImage = randomValueImage2 });
}
Debug.Log(randomKey3 + randomValueSpeed3);
}
}
Your answer
Follow this Question
Related Questions
Accessing a Class's Variables from within a Dictionary Element 1 Answer
Determine an index of a closest number in a database, from a user's input 1 Answer
Assigning a value to a Dictionary key changes other key values 0 Answers
How do I create a Key and Values Dictionary array in C# 1 Answer
Storing Items in a Building Blueprint 0 Answers