Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 frhilyana118 · Jan 30, 2020 at 03:00 AM · parsing

attach the game object with its data from a .txt file

hi, i'm new with unity and using c#

each of the floor shall contain its own data which obtained from a .txt file. so this is the outline

First, we read the schedule CSV as a series of strings for each line split it into values (using space/tab/semicolon). The first field will give us the entity ID Traverse the scene and compare the name of the object with the ID. If found, add the data to the entity, using a custom component with two list of strings: keys and values.

this is what i did based on this blog http://cad-3d.blogspot.com/2016/03/getting-bim-data-into-unity-part-5.html

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using System.Linq;
 using System.Text.RegularExpressions;
 
 
 public class Schedule_Parser : MonoBehaviour
 {
     
     //the internal file name private
     private string fileToParse = "";
 
     //some public variables to configure this script
     public string filePath = "filePath";
     public string fileName = "fileName";
     public string fileExtension = "txt";
 
     //declaring another 2 because want to fill into key and value as in array
     public int headersLineNumber = 0;
     public int valuesFromLine = 1;
 
     
 
 
     // use this for initialization
     void Start()
     {
         fileToParse = filePath;
         fileToParse = Path.Combine(fileToParse, fileName);
         fileToParse = fileToParse + "." + fileExtension;
 
         FileInfo theSourceFile = null;
         TextReader reader = null;  // NOTE: TextReader, superclass of StreamReader and StringReader
 
         // Read from plain text file if it exists
         theSourceFile = new FileInfo(Path.Combine(Application.dataPath, fileToParse));
         if (theSourceFile != null && theSourceFile.Exists)
         {
             reader = theSourceFile.OpenText();  // returns StreamReader
             Debug.Log("Created Stream Reader for " + fileToParse + " (in Datapath)"
          );
         }
 
         if (reader == null)
         {
             Debug.Log(fileName + " not found or not readable");
         }
         else
         {
             
   
          
             // Read each line from the file/resource
             bool goOn = true;
             int lineCounter = 0;
             string[] headers = new string[0];
             while (goOn)
             {
                 string buf = reader.ReadLine();
                 if (buf == null)
                 {
                     goOn = false;
                     return;
                 }
                 else
                 {
                     Debug.Log("Current Line : " + lineCounter + " : " + buf);
 
                     string[] values;
 
 
     
                     if (lineCounter == headersLineNumber)
                     {
                         headers = buf.Split(',');
                         Debug.Log("--> Found header " + headers[0]);
                     }
                     if (lineCounter >= valuesFromLine)
                     {
                         // now we get a , ; or -delimited string with data
                         // ID ...
                         values = buf.Split(',');
                         string ID = values[0];
                         Debug.Log("--> Found values " + values[0]);
 
 
                         GameObject go;
                         go = GameObject.Find(ID);
                         if (go == null)
                         {
                             foreach (var gameObj in
                              FindObjectsOfType(typeof(GameObject)) as GameObject[])
                             {
                                 if (gameObj.name.Contains(ID.ToString()))
                                 {
                                     go = gameObj;
                                 }
                             }
                         }
 
 
                         if (go != null)
                         {
                             Debug.Log("    Found ID : " + ID);
                             go.AddComponent<Metadata>();
                             Metadata Meta = go.GetComponent<Metadata>();
                             Meta.values = values;
                             Meta.keys = headers;
                         }
                         else
                         {
                             Debug.Log("    No objects found with ID: " + ID);
                         }
 
 
                     }
                 }
               
 
                 lineCounter++;
             }
 
         }
     }
 }

I'm trying to attach each of the data from the .txt file to its object

2 script were conducted. one is metadata script containing the variable of keys and values

and the second one is the schedule parsing script, to assign the parameter and the values in the metadata.

the error that i obtained is that no object were found with the ID although both of the game object and the .txt file contain the same ID so that they are able to be relate to each other.

alt text

Is there any error that i might missed out?

the last picture is the expectation. when i hit the play button, the data retrieved by schedule_parser script will be displayed in the metadata (that I've created earlier as the scrip 1). alt text

result.png (228.7 kB)
expectation.png (36.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Jan 30, 2020 at 07:52 AM

Wow thats a lot of code to parse a csv try:


https://www.dotnetperls.com/textfieldparser

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

118 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ReadLine to a Variable 2 Answers

Parsing error and unexpected symbol `}' (25,41) 1 Answer

OpenFilePanel alternative 1 Answer

Saving serializable class list at runtime. 1 Answer

urldecode function or library for Unity? 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