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 Chesley · Jun 19, 2012 at 03:12 PM · networkingframestreamreader

StreamReader & Unity still does weird things

yesterday i posted a question on how i could use StreamReader WITHOUT letting unity crash.... i taken out the loop BUT theres still some weird stuff

when i start the server (a java server) en connect to it in unity, the game not freezes but it WAITS for a newline (because i use ReadLine()), wether i use ReadLine(); or Read(); it doesn't matter, it always freezes.

i just discoverd that when i send something to unity using the server, unity rendered 1 frame then freezes again. unity waits until it receives data before moving on with the other things. i tried the things told i must do to make it work but, either i didn't do what was said, or it didn't work.

i hope to finally hear a solution to this, because im already searched for this 3 days in a row now and i can't move on before i found an awnser

Script:

 void Read() {
     NetworkStream STREAM = connection.GetStream();
     string line = null;
     StreamReader IN = new StreamReader(STREAM);
     String returndata = IN.ReadLine();
     string[] returndata2 = returndata.Split(" "[0]);
     string code = returndata2[0];
     string text = returndata.Replace(code, "");
     if(code == "#ChatMessage") {
         BroadcastMessage("AddChatMessage", text);
     }
 }
 void Update () {
     Read();
 }
Comment
Add comment · Show 5
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 Chesley · Jun 19, 2012 at 05:13 PM 0
Share

Bounce it up to the top

avatar image whydoidoit · Jun 19, 2012 at 05:22 PM 0
Share

It looks to me like you've got a design problem there. You are calling a Synchronous function many times per second. It just isn't the right way to do this stuff. If you want that code to work you will have to either:

  • Use Asynchronous IO and handle the resulting data

  • Use that code on a second thread and send messages to the main Unity thread when information arrives

avatar image whydoidoit · Jun 19, 2012 at 05:30 PM 0
Share

You could also possibly use my code from this post to start the second thread and send information back to the main thread.

avatar image Chesley · Jun 20, 2012 at 12:45 PM 0
Share

Ive got this almost working, but do you have any idea on how to send the messages to the main Unity thread??? Broadcast$$anonymous$$essage doesn't work because i can't call unity function inside a non-main thread

avatar image CHPedersen · Jun 20, 2012 at 01:40 PM 0
Share

You have to set some non-Unity-API-variables which Unity reacts to next time it renders a frame. This looks like you're implementing a chat window, so I imagine you want the chatmessage to be added to a list of messages in a little scroll window?

Just add the chatmessage to a List (remember to synchronize properly using the lock keyword). Then, in OnGUI somewhere else, make a scrollview that loops through all strings in that List and draws a Label with each message.

1 Reply

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

Answer by Chesley · Jun 25, 2012 at 09:50 AM

i already solved the problem a few days ago, but forgot to write a message for the right solution. basicly Christian H Pedersen was right!

Christian H Pedersen: You have to set some non-Unity-API-variables which Unity reacts to next time it renders a frame.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

other players and the server does not receive networkView.RPC 1 Answer

A node in a childnode? 1 Answer

How to upload a local file via the web player? 1 Answer

Unity networking tutorial? 6 Answers

How to make a game updater/launcher? 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