- Home /
How to Convert FileInfo to TextAsset to add to List
I'm creating a game where there are lots of virtual book to read from, but I'm having trouble when it comes to assigning to a list the pages of the book.
Here's how it is supposed to work:
The books are organized into folders, and the chapters of each book are its sub-folders.
When the player chooses the book he wants to read, the script finds that book and organizes all the chapters, and finds all the pages that are inside those chapters folders.
The problem arises here. I need to convert those pages that the script finds to TextAssets and add them to a List of TextAssets. It needs to work this way due to the manner in which the virtual books work: each page is an element of the list.
Here's where the problem happens:
private void DefineBook()
{
//Encontra todos os capitulos do livro escolhido pelo jogador
BookChapters = BookInQuestion.GetDirectories();
int a = 0;
for (int p = 0; p < BookChapters.Length; p++)
{
Pages = new DirectoryInfo("" + BookChapters[p].FullName.ToString());
PagesInfo = Pages.GetFiles();
foreach(FileInfo f in PagesInfo)
{
TotalPages.Add(PagesInfo[a] as TextAsset);
a++;
}
}
//CurrentPage_Left = PagesInfo[0];
//CurrentPage_Right = PagesInfo[_currentPageLeft + 1];
mostrarLivro = !mostrarLivro;
}
Then I get the following error: Cannot convert type 'System.IO.FileInfo' to 'Unity.TextAsset' via built-in conversion.
I mean, I know why it doesn't work, I just don't know how to convert those files. Any help would be much appreciated since I've been stuck on this problem for a while.
As the name suggest TextAsset is an asset (file) in Unity. It cannot be created at runtime. TextAsset is only a container for text stored in a string and in a byte array, that is it. It does nothing else. Why do you want to convert your FileInfo into a TextAsset? You can create your own class to hold the text and add that class to the TotalPages list, or just add the text content directly to the TotalPages list.
Thank you for answering. All I want is to add all the pages, which are text files(.txt), to the TotalPages list, so that I can access each element of that list latter on. I thought that I should tell the script to find the pages first and than add them to the list automatically, since the pages are already created. What is complicating this process is that I want the script to adapt itself to the number of pages and chapters of each book at run-time. This is because some book will be enormous and I will not select manually every single page and add it to the list.
Yes, that makes sense, but it still does not explain why you want to convert the FileInfo into a TextAsset.
When you have loaded the FileInfo you can store it directly in the list and access the text in it at your convenience.
Interesting... And how does one store the info into a list, in which every page of the book is a different element of said list?
Thank you for your responses, the game is now working as it should :)
Answer by Simeon · Jan 28, 2015 at 06:19 PM
Do you want to have each page of the book stored in memory, or do you want to stream the pages at you time. If you want to store all the pages and their data in a custom class, just use the Text Reader at the start, and if you want to stream them, just store their paths into a list and then use the Text Reader to load the data into memory when you want. You can also use the pages Directory Info to organize them into books.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do I add a float variable into a list ? 2 Answers
C# Adding Multiple Elements to a List on One Line 5 Answers
Add item to string by name? 1 Answer
[C#] Programm does not add to list 1 Answer