-
-
Save codehoose/7eead7e9c08c5b4ac2f5bfa83131bfa7 to your computer and use it in GitHub Desktop.
CSV to Dictionary - All credit to Sloane Kelly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Card | |
{ | |
public int CardID { get; set; } | |
public int CardLvl { get; set; } | |
public bool CardPlayed { get; set; } | |
public string CardText { get; set; } | |
public Card(int cardID, int cardLvl, bool cardPlayed, string cardText) | |
{ | |
CardID = cardID; | |
CardLvl = cardLvl; | |
CardPlayed = cardPlayed; | |
CardText = cardText; | |
} | |
public Card() | |
{ | |
CardID = 0; | |
CardLvl = 0; | |
CardPlayed = false; | |
CardText = ""; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using UnityEngine; | |
using System.Linq; | |
using LINQtoCSV; | |
public class DeckBuilder : MonoBehaviour | |
{ | |
private Dictionary<int, Card> CompleteDeck = new Dictionary<int, Card>(); | |
[Tooltip("The file that contains the list to be converted to cards. Must be in Resources folder")] | |
public TextAsset CardList; | |
private void CreateDeck() | |
{ | |
CompleteDeck.Clear(); | |
CsvFileDescription inputFileDescription = new CsvFileDescription | |
{ | |
SeparatorChar = ',', | |
FirstLineHasColumnNames = true | |
}; | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
using (StreamWriter txtWriter = new StreamWriter(ms)) | |
{ | |
using (StreamReader txtReader = new StreamReader(ms)) | |
{ | |
txtWriter.Write(CardList.text); | |
txtWriter.Flush(); | |
ms.Seek(0, SeekOrigin.Begin); | |
CsvContext cc = new CsvContext(); | |
cc.Read<Card>(txtReader, inputFileDescription) | |
.ToList() | |
.ForEach(card => | |
{ | |
CompleteDeck[card.CardID] = new Card | |
{ | |
CardLvl = card.CardLvl, | |
CardPlayed = card.CardPlayed, | |
CardText = card.CardText, | |
}; | |
}); | |
} | |
} | |
} | |
//(Text, P1, P1Gender, P2, P2Gender, P3, P3Gender, P4, P4Gender); | |
string output = string.Format((CompleteDeck[2].CardText), "a", "b"); | |
Debug.Log(output); | |
} | |
public Dictionary<int, Card> ReturnLevelDeck(int level) | |
{ | |
CreateDeck(); | |
Debug.Log(CompleteDeck.Count); | |
Dictionary<int, Card> returnDictionary = new Dictionary<int, Card>(); | |
for (int i = 0; i < CompleteDeck.Count; i++) | |
if (CompleteDeck[i].CardLvl == level) | |
{ | |
Debug.Log("found"); | |
returnDictionary[CompleteDeck[i].CardID] = CompleteDeck[i]; | |
} | |
return returnDictionary; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class GameController : MonoBehaviour | |
{ | |
public DeckBuilder Deck; | |
public void QuitGame() | |
{ | |
Application.Quit(); | |
} | |
private void Awake() | |
{ | |
TestStart(); | |
} | |
public void TestStart() | |
{ | |
//string output = string.Format((Deck.CompleteDeck[2].CardText), "a", "b"); | |
Dictionary<int, Card> Level0Cards = Deck.ReturnLevelDeck(0); | |
//I appreciate that this doesn't make any proper use of the IEnumerable nature of the dictionary, but I wanted to see what was going on.. | |
for (int i = 0; i < Level0Cards.Count; i++) | |
{ | |
string retstring = string.Format((Level0Cards[i].CardText), "James", "Bob"); | |
Debug.Log(retstring); | |
} | |
} | |
//TEST CODE ONLY DOWN HERE | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CardID | CardLvl | CardPlayed | CardText | |
---|---|---|---|---|
0 | 0 | FALSE | {0} Card 0 {1} Card 0 | |
1 | 0 | FALSE | {0} Card 1 {1} Card 1 | |
2 | 1 | FALSE | {0} Card 2 {1} Card 2 | |
3 | 1 | FALSE | {0} Card 3 {1} Card 3 | |
4 | 2 | FALSE | {0} Card 4 {1} Card 4 | |
5 | 2 | FALSE | {0} Card 5 {1} Card 5 | |
6 | 3 | FALSE | {0} Card 6 {1} Card 6 | |
7 | 3 | FALSE | {0} Card 7 {1} Card 7 | |
8 | 4 | FALSE | {0} Card 8 {1} Card 8 | |
9 | 4 | FALSE | {0} Card 9 {1} Card 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment