Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by luisvilbro · Jan 28, 2015 at 02:29 PM · c#listaddtextasset

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameVortex · Jan 28, 2015 at 03:13 PM 0
Share

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.

avatar image luisvilbro · Jan 28, 2015 at 03:25 PM 0
Share

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.

avatar image GameVortex · Jan 28, 2015 at 03:44 PM 0
Share

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.

avatar image luisvilbro · Jan 28, 2015 at 04:16 PM 0
Share

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?

avatar image luisvilbro · Jan 29, 2015 at 08:38 AM 0
Share

Thank you for your responses, the game is now working as it should :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges