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 · Jul 30, 2020 at 08:37 PM · coroutinereading data

Why is Unity freezing with this code?

I have clones of a cube at every position in a box of 10x10x10 with a step of 0.25 (40 cubes in the x direction, 40 cubes in the y direction, and 40 cubes in the z direction). I am trying to change the transparency of the cubes based on a text file, which has 64000 lines (with the first column corresponding to x values, the second column to y values, the third column to z values, and the fourth column to transparency values). I put this script on the cube, which is then cloned, so every cube has that script attached to it.

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using System.IO;
 using System;
 using System.Runtime.InteropServices;
 using System.Security.Cryptography;
 
 public class FileManager : MonoBehaviour
 {
     public Renderer rend;
     public Color color;
     string filePath, fileName;
 
     void Update()
     {
         if (Input.GetKeyDown("space"))
         {
             StartCoroutine(Delay());
         }
     }
 
     void LateUpdate()
     {
         Color color = this.GetComponent<MeshRenderer>().material.color;
         rend = GetComponent<Renderer>();
         if (color.a == 0)
         {
             rend.enabled = false;
         }
     }
 
     public IEnumerator Delay()
     {
         for (int i = 3; i <= 3; i++)
         {
             rend.enabled = false;
             fileName = "Wave_Time_" + i + ".txt";
             filePath = Application.dataPath + "/" + fileName;
             ReadFromTheFile();
             yield return null;
         }
     }
 
     public class Point
     {
         public float x { get; set; }
         public float y { get; set; }
         public float z { get; set; }
         public float p { get; set; }
     }
 
     public void ReadFromTheFile()
     {
         List<Point> Points = new List<Point>();
         List<string> lines = File.ReadAllLines(filePath).ToList();
         foreach (var line in lines)
         {
             string[] entries = line.Split(',');
 
             Point newPoint = new Point();
 
             newPoint.x = float.Parse(entries[0]);
             newPoint.y = float.Parse(entries[1]);
             newPoint.z = float.Parse(entries[2]);
             newPoint.p = float.Parse(entries[3]);
 
             Points.Add(newPoint);
         }
 
         Color color = this.GetComponent<MeshRenderer>().material.color;
         rend = GetComponent<Renderer>();
         float index = (4 * transform.position.x) * 1600 + (4 * transform.position.y) * 40 + (4 * transform.position.z);
         color.a = Points[(int) index].p;
         this.GetComponent<MeshRenderer>().material.color = color;
     }
 }
 
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
Best Answer

Answer by Bunny83 · Jul 30, 2020 at 11:06 PM

Ok, just to summarize:

You created 64000 cube gameobjects in your scene and attached this script to every one of those cubes. Each of those scripts (when space is pressed) will read a file with 64k lines of text, splits each line, parses each string fragment, creates a list of the parsed data and then looks up a single value in that list based on the index....


And now you're wondering why your application hangs? Are you serious? ^^ You essentially parse over 4 BILLION lines of text. You create tons of garbage on the way, which alone would probably take minutes if not hours.


There are many things wrong with your approach. First of all creating 64k gameobject and giving each object a unique material is already the first issue. None of your objects could batch that way. However depending on the effect you're after you might not have many alternatives since batching transparent objects is quite tricky.


Next thing is none of your cubes should have a script attached unless you really need one for other purposes. For what you have shown there's absolutely no reason to have a script on the cubes. You should have a single script instance on a seperate object (maybe a parent object of the cubes) and only read and parse your file once.


Next strange thing is you said that you have the x, y and z coordinates in the file which you completely ignore. You just calculate the index based on the flattened 3d array. So since you don't use any coordinates you would only need a single value per line.


You're doing a lot of unnecessary things which just create extra garbage. Like the ToList after "ReadAllLines". All you do is iterating through that list of lines. So just use the returned array. You duplicate the whole data for no reason. I don't see any reason why "Point" is a class and not a struct. Just for storing 4 float values you could simply use Unity's Vector4 struct.

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

141 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

Related Questions

How can I read text files to determine transparency. 2 Answers

If statement inside Coroutine firing without meeting the conditions 2 Answers

iOS Unity randomly hangs on yield return www 0 Answers

Download multiple textures 0 Answers

Make label disappear after 1 second? 3 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