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 /
  • Help Room /
avatar image
1
Question by Natrem · Nov 05, 2015 at 10:21 PM · networkconvert

How to convert floats/doubles to raw bytes without generating garbage?

Hi!

I have a network solution using UNET LLAPI. I use my own class to send and receive bytes, and I currently use BinaryReader and BinaryWriter with a MemoryStream. The problem is, BinaryWriter.Write(float) generates garbage (44 Bytes for each float) and I don't want any garbage collection. This problem also occurs with double, but not with BinaryReader and/or integer, byte, char etc. BinaryFormatter is worse in every way (garbage + bytes used).

I looked around, it seems that BinaryWriter.Write(float) uses BitConverter.GetBytes(), which creates an array each time it is called. Also I believe that it's only the Unity .NET version that does this issue.

I don't want to use my own function (which is the same than BinaryWriter.Write(float) inside .NET v4 library) because it uses unsafe code and I want to be able to target the WebPlayer platform.

I also want a conversion without any loss of accuracy, and that doesn't use more than 4 bytes per float once it's converted. The speed of conversion also matters but less than accuracy and space.

Thank you.

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
2
Best Answer

Answer by ttRevan · Nov 06, 2015 at 08:07 PM

You can use UNET NetworkWriter that is used in HLAPI, it doesn't generate garbage and uses something like C union imitation for float-to-int conversion.

BTW, thanks for figuring out BinaryWriter generate garbage, I am using it by myself :)

Comment
Add comment · Show 1 · 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 Natrem · Nov 06, 2015 at 08:51 PM 0
Share

I don't see any hint in your link that indicates that I could use NetworkWriter to convert a float into byte[]. Is it only through a GameObject? EDIT: Okay the documentation is incomplete. It seems to do what I was looking for, thanks!

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

34 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

Related Questions

NetworkIdentity.netID to int 3 Answers

Timespan to int total seconds 0 Answers

[UNET 5.2] Multiple local authority for 1 object? 0 Answers

Connecting SQL with Unity 0 Answers

What Version of SSL Protocol does unity supports? 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