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 Slobdell · Jun 09, 2013 at 01:17 AM · c#arraysearchtextasset.txt

Splitting textasset into an array and binary search

I'm making a word game, all words are stored in a SORTED txt file in my resources folder. I've learned the only way to be platform independent is to use the resources.load method. However, when I split my text asset into an array I am having trouble searching the array. Here's my code`

  TextAsset asset = (TextAsset)Resources.Load("words");
  string[] words = asset.text.Split('\n')`

Now if I print out random positions in the array the words print out as expected, so they are all properly loaded up. There are no extra characters. But for some reason when I do a binary search on it it never finds my word.

  int result = Array.BinarySearch(words, "hello");

This will return a negative value indicating it couldn't be found, even if I print out the exact location that I know where it is in the array and it prints out "hello". To my eye they always look identical and this is driving me nuts. Could it have something to do with text encoding? It was working no problem when I did some file IO and split the file like string[] words = System.IO.File.ReadAllLines("Path");

But if I do that I have to figure out the file path for each platform(this is mobile iOS, Android, shortly Windows phone).

I'm at a loss here....any help would be appreciated or other ideas. Thanks!

Steve

Comment
Add comment · Show 4
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 robertbu · Jun 09, 2013 at 01:31 AM 0
Share

You may have carriage feeds and line feeds in the file which would result in a non-match. You don't have to load the TextAsset from Resources. Just place it in Assets and then drag and drop it onto the 'asset' in the Inspector.

avatar image Slobdell · Jun 09, 2013 at 01:37 AM 0
Share

Sorry I'm a newb, not sure what you mean. Where am I dragging and dropping it? And I should remove it from the resources folder and just put it in the assets folder? Then how do I access it programmatically? Thanks

avatar image Slobdell · Jun 09, 2013 at 01:54 AM 0
Share

Ok thanks I'll give it a try. I'm a little skepticle, however. This solution seems like I'm just moving things around but in the end I'm still just loading a text asset and splitting it, with the only change being two delimiters ins$$anonymous$$d of one. But I'm so annoyed I'd jump off a bridge if someone thought it would solve my issue.

avatar image robertbu · Jun 09, 2013 at 01:57 AM 0
Share

What I suspect is that your strings in the file look like:

"hello\\r\\n"

So after the split they will look like:

"hello\\r"

And the '\\r' is preventing the match

The rest of the moving around has nothing to do with your issues. I just wanted to make a point that you don't have to load something from Resources to be platform independent...that you can directly link in the text asset to your script file at edit time.

1 Reply

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

Answer by robertbu · Jun 09, 2013 at 01:46 AM

Move the TextAsset out of Resources to anywhere in Assets (including a subfolder you create). At the top of the class you will put:

 public TextAsset asset;

Your script above will be attached to a game object. Select the game object in the Hierarchy. Then drag and drop the text asset on to the asset variable (where it says 'None (TextAsset)').

As for splitting the lines, I am inexperienced with C# text manipulation, but I would do it this way:

 char[] archDelim = new char[] { '\r', '\n' };
 words = asset.text.Split(archDelim, StringSplitOptions.RemoveEmptyEntries); 

I think you need to do a 'using System.Text;' in order to get 'StringSplitOptions'.

Comment
Add comment · Show 2 · 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 Slobdell · Jun 09, 2013 at 02:09 AM 0
Share

Thank you sooooooo much!!!!! It was indeed the carriage returns. Unreal, I never would have figured that out on my own. Awesome

Steve

avatar image robertbu · Jun 09, 2013 at 02:14 AM 0
Share

Glad it worked. I converted my comment to an answer. Please click the checkmark to close out the question.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Double Parse a text file into 2d array or jagged array 1 Answer

Distribute terrain in zones 3 Answers

Brute Force Search Algorithm 0 Answers

Racing Game Leaderboard with Photon 0 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