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 KOCGI · Feb 02 at 02:04 AM · photonpun

Photon Engine - Streaming texture with CPU peaks and low performance. Any ideas?

Hi,

I want to stream a sprite texture to all players in the room. I use invoke repeat in start to call the streaming method in a loop (currently 1x per sec). It's working, but there is a huge problem: The performance.

As you can see, I have those nasty peaks. They are coming only from the script attached. Is there any way to get it more CPU friendly?

alt text

Code:

 using UnityEngine;
 using Photon.Pun;
 using Photon.Realtime;
 using ExitGames.Client.Photon;
 using UniRx;
 using UniRxExtension;
 
 namespace TexStreaming
 {
     public class TextureBroadcastComponent : MonoBehaviourPunCallbacks, IOnEventCallback
     {
         int bytePerMessage = 1000; 
         public GameObject textureG;     
         public Texture2D texture2d;
 
         bool isReceiving;
         byte[] receiveBuffer;
         int totalDataSize;
         int currentReceivedDataSize;
         int receivedMessageCount;
 
         void Start()
         {
             texture2d = textureG.GetComponent<SpriteRenderer>().sprite.texture;
             texture2d.GetPixels32();
 
             // Run update 2x per second
             InvokeRepeating(nameof(BroadcastTexture), 0f, 1.0f);
         }
 
         public void BroadcastTexture()
         {
             byte[] rawTextureData = texture2d.EncodeToPNG();
 
             int width = texture2d.width;
             int height = texture2d.height;
             int dataSize = rawTextureData.Length;
             int viewId = this.photonView.ViewID;
 
             //Debug.Log("--TEXTURE BROADCAST--");
             StreamTextureDataToOtherClients(rawTextureData, width, height, dataSize, viewId);
         }
 
         void StreamTextureDataToOtherClients(byte[] rawTextureData, int width, int height, int dataSize, int viewId)
         {
            // Debug.Log("*--STREAMING--*");
             
             RaiseEventOptions raiseEventOptions = new RaiseEventOptions
             {
                 CachingOption = EventCaching.DoNotCache,
                 Receivers = ReceiverGroup.Others,
             };
 
             SendOptions sendOptions = new ExitGames.Client.Photon.SendOptions
             {
                 Reliability = true,
             };
 
             // The actual info we send
             int[] textureInfo = new int[4];
             textureInfo[0] = viewId;
             textureInfo[1] = width;
             textureInfo[2] = height;
             textureInfo[3] = dataSize;
 
             // Send the event:
             PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.BeginStream, textureInfo, raiseEventOptions, sendOptions);
 
             // Send data
             rawTextureData.ToObservable()
                 .Buffer(bytePerMessage)
                 .Subscribe(byteSubList =>
                 {
                     byte[] sendData = new byte[byteSubList.Count];
                     byteSubList.CopyTo(sendData, 0);
                     PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.Streaming, sendData, raiseEventOptions, sendOptions);
                 });
         }
 
 
         public void OnEvent(ExitGames.Client.Photon.EventData photonEvent)
         {
             if(photonEvent.Code == (byte)StreamingBytesEventCode.BeginStream)
             {
                 int[] data = (int[])photonEvent.Parameters[ParameterCode.Data];
                 OnReceivedTextureInfo(data);
             }
             if(photonEvent.Code == (byte)StreamingBytesEventCode.Streaming)
             {
                 byte[] data = (byte[])photonEvent.Parameters[ParameterCode.Data];
                 OnReceivedRawTextureDataStream(data);
             }
         }
 
         void OnReceivedTextureInfo(int[] data)
         {
             int viewId = data[0];
             if (viewId != this.photonView.ViewID)
             {
                 this.isReceiving = false;
                 this.totalDataSize = 0;
                 this.currentReceivedDataSize = 0;
                 this.receivedMessageCount = 0;
                 return;
             }
 
             this.isReceiving = true;
             this.currentReceivedDataSize = 0;
             this.receivedMessageCount = 0;
 
             int width = data[1];
             int height = data[2];
             int dataSize = data[3];
             this.totalDataSize = dataSize;
             this.receiveBuffer = new byte[dataSize];
             // Debug.Log("*--TEXTURE RECEIVE--*");
         }
 
         void OnReceivedRawTextureDataStream(byte[] data)
         {
             if (this.isReceiving)
             {
                 data.CopyTo(this.receiveBuffer, this.currentReceivedDataSize);
                 this.currentReceivedDataSize += data.Length;
                 this.receivedMessageCount++;
 
                 if (this.currentReceivedDataSize >= (this.totalDataSize))
                 {
                     this.isReceiving = false;
                     this.currentReceivedDataSize = 0;
                     this.receivedMessageCount = 0;
                     OnReceivedRawTextureData();
                 }
             }
         }
 
         void OnReceivedRawTextureData()
         {
             // Debug.Log(" **** Received Raw Texture Data **** ");
             texture2d.LoadImage(this.receiveBuffer);
             texture2d.Apply();
             GetComponent<Renderer>().material.mainTexture = texture2d;
         }
     }
 }

performancesere.jpg (62.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

0 Replies

· Add your reply
  • Sort: 

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

138 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

Related Questions

How much players are in scene 1 Answer

How Could I sync a value of varibale in both device? 2 Answers

Spawn point for specific player.Photon Pun2 0 Answers

Multiplayer , account to log in with and saved inventory after a game 0 Answers

OnRoomsListUpdate() is not being called.,OnRoomsListUpdate not being called 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