using System.Collections.Generic; using UnityEngine; public class DictionaryExample : MonoBehaviour { DictionarymyDictionary; void Start () { myDictionary = new Dictionary (); myDictionary.Add("A", 1); myDictionary.Add("B", 2); myDictionary.Add("C", 3); int valueA = myDictionary.Get("A"); int valueD = myDictionary.Get("D", 4); Debug.Log(valueA); // outputs: 1 Debug.Log(valueD); // outputs: 4 } }
using System.Collections.Generic; using System.IO; class FileLoader { private DictionaryThis example shows how to load data from a CSV file into a dictionary using the VariableDictionary.Get method. It also shows how to access that data by calling the "Get" method on the dictionary object. This code uses the System.IO package library.fileData = new Dictionary (); public void LoadData(string filePath) { var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); using var reader = new StreamReader(fileStream); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); fileData[values[0]] = values[1]; } } public string Get(string key) { return fileData.Get(key); } public static void Main(string[] args) { FileLoader fileLoader = new FileLoader(); fileLoader.LoadData(@"C:\temp\myfile.csv"); string value = fileLoader.Get("John"); System.Console.WriteLine(value); } }