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
1
Question by pickle chips · Aug 30, 2013 at 05:15 PM · textstringnpcdialogue.txt

NPC dialogue - Loading a text file to a string?

Hi! So when the player in my game talks to npcs, I use a string variable to show what they say (in a GUI.Label), and i edit this string in the inspector. But if the NPC has a long thing to say, in the inspector I can't do any "\n" or anything, and it also just isn't very convenient because the little box it has for you to enter isn't that great.

So I was thinking, maybe if i type out the things for npc's to say in a text file, then load that file through a script and save line to as a string variable? How would i do this?

Or is there a better alternative? - Thanks

Comment
Add comment · Show 9
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 Daphnis26 · Aug 30, 2013 at 06:42 PM 0
Share

Do you use playmaker? I bet that would be the easiest tool for something like this.

avatar image ForgeStudios · Aug 30, 2013 at 07:00 PM 0
Share

I submitted an engine to the asset store that will soon hopefully be live that does exactly this. Look out for it, it's called QuesText.

avatar image pickle chips · Aug 30, 2013 at 09:03 PM 2
Share

I'd rather not use another application, because i'm making my game as a learning experience, so that would sorta defeat the purpose..

avatar image getyour411 · Aug 30, 2013 at 09:07 PM 0
Share

Take a look at the X$$anonymous$$L methods in the public domain. I'm starting my $$anonymous$$ission$$anonymous$$anager and starting to ask the same kind of questions.

avatar image AlfredDuler · Aug 30, 2013 at 09:11 PM 0
Share

If you still want to work on the length of the text, take a look at this post:

http://answers.unity3d.com/questions/190800/wrapping-a-textmesh-text.html (c#)

In this post used for a text mesh but it works for every string.

Just put this function in a script and call it.

The first argument would be obviously the text you want to display, and the second, the number of character before line break

Show more comments

1 Reply

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

Answer by TrickyHandz · Aug 30, 2013 at 11:13 PM

Here is an example of where you might want to start with this using a TextAsset:

C#

 using UnityEngine;
 using System.Collections;
 
 public class TextRead : MonoBehaviour 
 {
 
     public TextAsset textFile;
     string[] dialogLines;
     
     // Use this for initialization
     void Start () 
     {
         // Make sure there this a text
         // file assigned before continuing
         if(textFile != null)
         {
             // Add each line of the text file to
             // the array using the new line
             // as the delimiter
             dialogLines = ( textFile.text.Split( '\n' ) );
         }
     }
 }

JavaScript

 var textFile : TextAsset;
 var dialogLines : string[];
 
 function Start()
 {
     // Make sure there is a text
     // file assign before continuing
     if(textfile)
     {
         // Add each line of the text file
         // to the array using the new line
         // as the delimiter
         dialogLines = (textFile.text.Split("\n"[0]));
     }
 }

When you want to display a certain line from your file just call the appropriate index in the array of strings like this:

C#

 // Assign the first string
 // in the array to a variable
 string dialog = dialogLines[0];

JavaScript

 // Assign the first string
 // in the array to a variable
 var dialog : string = dialogLines[0];
Comment
Add comment · Show 4 · 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 pickle chips · Aug 31, 2013 at 12:22 AM 0
Share

Ah, this seems perfect, thanks a bunch! one question first though, is it possible to split it based on the amount of characters in the string? For example, split it after every 100th character?

If not it's not an issue, it would just be nicer for me :p

thanks again :)

avatar image TrickyHandz · Aug 31, 2013 at 12:55 AM 0
Share

It is possible, but it would take a different approach with the code. I'm away from my dev machine right now, but I will post an update to the answer when I can.

avatar image pickle chips · Aug 31, 2013 at 01:38 AM 1
Share

Actually it's okay, I've been fiddling with it more and it's actually perfect the way it is :p now it's muuch easier for me to create dialogue, thanks again

avatar image TrickyHandz · Aug 31, 2013 at 02:22 AM 0
Share

No problem. Glad it works for you.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a text appear gradually? 0 Answers

String.Remove not working 1 Answer

How can I create an NPC Dialogue with UI Text? 1 Answer

GUI text label using variable values and text 1 Answer

How can I detecting whether certain characters appear in any strings in my array of strings? 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