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 crzyone9584-2 · Mar 11, 2011 at 04:46 AM · guitextloadfile

Loading text into Gui.Box

I've searched couldn't find a clear answer. Maybe I missed it. I have a text area that i want to load text into from a text file that I've added as an asset. Is there a way in C# to load the text directly to the GUI.Box along with line breaks so it wont just be one long line of text in the Gui.Box?

Comment
Add comment
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

2 Replies

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

Answer by Molix · Mar 11, 2011 at 05:59 AM

I just did a quick test and the line breaks came in no problem in a default GUI.Box from a TextAsset. e.g.

public TextAsset textAsset;
void OnGUI()
{
  GUI.Box( new Rect( 0, 0, Screen.width, Screen.height ), textAsset.text );
}

Maybe if you're not seeing the line breaks there could be an issue with the text asset itself (line breaks often have platform specific quirks).

Comment
Add comment · Show 5 · 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
avatar image crzyone9584-2 · Mar 11, 2011 at 06:34 AM 0
Share

the file i'm trying to load is about.txt. Would I change textAsset to About or would i leave it named textAsset?

avatar image Molix · Mar 11, 2011 at 02:37 PM 0
Share

Add the script to a game object, then in the Inspector assign (e.g. by dragging from the Project window) the text file to the variable. Its just like if you were assigning an audio file or texture to those types of variables.

avatar image crzyone9584-2 · Mar 12, 2011 at 05:28 PM 0
Share

Thanks, it works. Only problem is the width and height. I tried to scale it down but it wouldn't wrap the words correctly. Is there a code I can add into a text file to make it have a new line?

avatar image Molix · Mar 13, 2011 at 07:00 AM 0
Share

Check out GUIStyle -- you can pass a style to the GUI/GUILayout functions. One of the options is to wrap text.

avatar image jong · Feb 04, 2013 at 06:23 AM 0
Share

Thanks, the answer helped me ^_^. Anyway, How if i want to change the textAsset.text into textAsset2.text when i press the button?

avatar image
0

Answer by Extrakun · Mar 11, 2011 at 07:01 AM

You can also use the WWW class to load resources from the hard disk, by using Application.dataPath

var www : WWW; var text_: string;

function LoadAndDisplay() { www = new WWW(Application.dataPath + "/file.txt"); yield www; text_ = www.text; }

function OnGUI() { if (text_) GUI.Label(new Rect(0,0,100,30), text_);

}

Comment
Add comment · Show 1 · 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
avatar image crzyone9584-2 · Mar 11, 2011 at 07:29 AM 0
Share

application.dataPath won't work on a web build will it? Trying to keep the code simple so it can go across windows and the web. Eventually $$anonymous$$ac also.

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

1 Person is following this question.

avatar image

Related Questions

How to get info from a text file 1 Answer

Executing code from a text file and writing changes to it 1 Answer

How to save and load text in a GUI TextArea 1 Answer

Load and save text from web server 2 Answers

Problem Reading A Text File 2 Answers


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