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 rbatelaan2 · Dec 14, 2020 at 10:00 PM · coroutinereading data

How can I read text files to determine transparency.

I have 64000 text files in my Resources folder named Wave_Position_### with the numbers corresponding to the position of the object I put the script on. I want to change the transparency based on the text files. Unity freezes when I try to run the code. This exact code used to work. I don't know what to do.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 using System.IO;
 using System.Security.Cryptography;
 using System;
 
 public class Wave : MonoBehaviour
 {
     public string[] lines;
     public string myFile;
     public Color color;
     public Renderer rend;
     string filePath, fileName;
 
     // Start is called before the first frame update
     void Start()
     {
         fileName = $"Wave_Position_{4*transform.position.x}_{4*transform.position.y}_{4*transform.position.z}";
         // filePath = Application.dataPath + "/Wave_Position/" + fileName;
         myFile = Resources.Load<TextAsset>(fileName).text;
         lines = myFile.Split(',');
         // lines = File.ReadAllLines(filePath).ToList();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown("space"))
         {
             StartCoroutine(Animation());
         }
     }
 
     public IEnumerator Animation()
     {
         for (int i = 0; i <= 99; i++)
         {
             rend = GetComponent<Renderer>();
             rend.enabled = false;
             color = this.GetComponent<MeshRenderer>().material.color;
             if (float.Parse(lines[i]) <= 0)
             {
                 rend.enabled = false;
             }
             else
             {
                 rend.enabled = true;
                 color.a = 5*float.Parse(lines[i]);
             }
             this.GetComponent<MeshRenderer>().material.color = color;
             yield return null; // new WaitForSeconds(1f);
         }
     }
 }
 

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 Bunny83 · Dec 15, 2020 at 01:52 PM

How many instances of your Wave class do you have in your scene? The way you use the coordinates and since you mentioned that you have 64k text assets lets us assume that you have probably way too many objects and each object will try to open a file named like the object position. Note that the gameobject's position is a Vector3 and contains floating point numbers. If you have 64000 files with integer coordinates, you can only cover and area of 10x10x10 (so (-5,-5,-5) or (5, 5, 5) or (0,0,0) to (10,10,10)). However the way you create your assetname will most likely fail for some positions due to floating point rounding issues. So you should first pay more attention to your asset name generation and use proper rounding.


Having 64k gameobjects with such a script would be horrible slow. Doing 64k file access and read operations is even slower.


There are many other dangerous things in your code which are not related to performance but code stability. You hardcoded an array length of 100 elements in your loop but you read the elements from the text file. You should use the array length instead.

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

Answer by Llama_w_2Ls · Dec 15, 2020 at 07:46 AM

I have no idea why you would use 64000 text files just to set the transparency of objects, however, try using the StreamReader class from using System.IO. It reads super large files a lot better than File.ReadAllLines and I think that your program is just taking a really long time to read the files.

For example:

 StreamReader reader = new StreamReader("fileDirectory");
 
 string line;
 // Reads through entire text file line-by-line
 while ((line = reader.ReadLine()) != null)
 {
     Debug.Log(line);
 }


@rbatelaan2

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

152 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 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

Why is Unity freezing with this code? 1 Answer

IEnumerator manipulates variable in android build 1 Answer

StopAllCoroutines() stops coroutines in other scripts? -- Other behaviours ?? 1 Answer

StopAllCoroutines not working 0 Answers

How to make the Colour parameter in coroutine work with any anything with a colour variable. 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