- Home /
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?
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
Your answer
Follow this Question
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