質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

2065閲覧

Unity PUNを使ってテクスチャの送受信

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/01/19 01:27

前提・実現したいこと

はじめまして.

UnityでPUNを使用して,ユーザ間でテクスチャの送受信を行う機能を作ろうとしています.

PUN2を使用して,やりたいことに近いことをしているサイト(https://nabla-tech-lab.hatenablog.com/entry/2019/05/15/180000)を見つけたのですが,私はPUN2ではなくPUNで実装したいと思っています.

そのため,PUNで使用できるようにプログラムを書き換えました.
しかし,別で起動しているプロジェクト側でうまくテクスチャを受信できずに悩んでいます.
エラーがでているわけではないのですが,実行してもテクスチャが共有されません.
Warningがでており,eventが呼び出されていないようなのですが,どのように修正したら良いかわからず,質問させていただきました.

どなたかご存知の方がいらっしゃったらご助力いただきたいです.
よろしくお願いします.

発生している問題・エラーメッセージ

イメージ説明

該当のソースコード

C#

1using UnityEngine; 2using Photon.Realtime; 3using ExitGames.Client.Photon; 4using UniRx; 5using UniRxExtension; 6 7namespace TextureSharing 8{ 9 public enum StreamingBytesEventCode 10 { 11 BeginStream = 10, 12 Streaming = 11, 13 } 14 15 public class TextureSharingComponent : MonoBehaviour 16 { 17 18 19 20 [SerializeField] 21 22 private PhotonView photonView; //追記した部分 23 24 int messagePerSecond = 100; // 100 Messages / Second 25 26 int bytePerMessage = 1000; // 1KBytes / Message 27 28 Texture2D texture; // ★ Readable texture ★ 29 30 bool isReceiving; 31 byte[] receiveBuffer; 32 int totalDataSize; 33 int currentReceivedDataSize; 34 int receivedMessageCount; 35 36 void Awake()  //追記した部分 37 { 38 photonView = GetComponent<PhotonView>(); 39 } 40 41 void Start() 42 { 43 //現在のテクスチャを取得 44 texture = (Texture2D)GetComponent<Renderer>().material.mainTexture; 45 try 46 { 47 texture.GetPixels32(); 48 } 49 catch (UnityException e) 50 { 51 Debug.LogError("!! This texture is not readable !!"); 52 } 53 } 54 55 public void GetRawTextureDataFromMasterClient() 56 { 57 photonView.RPC("GetRawTextureDataRPC", PhotonTargets.All); 58 } 59 60 //************************************************************************** 61 // Client -> MasterClient (These methods are executed by the master client) 62 //************************************************************************** 63 [PunRPC] 64 public void GetRawTextureDataRPC(PhotonMessageInfo info) 65 { 66 byte[] rawTextureData = texture.EncodeToPNG(); 67 68 int width = texture.width; 69 int height = texture.height; 70 int dataSize = rawTextureData.Length; 71 int viewId = this.photonView.viewID; 72 73 74 Debug.Log("*************************"); 75 Debug.Log(" GetRawTextureDataRPC"); 76 Debug.Log(" RPC sender: " + info.sender); 77 Debug.Log(" Texture size: " + width + "x" + height + " = " + width * height + "px"); 78 Debug.Log(" RawTextureData: " + rawTextureData.Length + "bytes"); 79 Debug.Log("*************************"); 80 81 StreamTextureDataToRequestSender(rawTextureData, width, height, dataSize, viewId, info.sender); 82 } 83 84 void StreamTextureDataToRequestSender(byte[] rawTextureData, int width, int height, int dataSize, int viewId, PhotonPlayer requestSender) //Player requestSenderとなっていた引数をPhotonPlayer requestSenderbに変更 85 { 86 87 bool reliable = true; //追記 88 89 90 Debug.Log("***********************************"); 91 Debug.Log(" StreamTextureDataToRequestSender "); 92 Debug.Log("***********************************"); 93 94 RaiseEventOptions raiseEventOptions = new RaiseEventOptions 95 { 96 CachingOption = EventCaching.DoNotCache, 97 Receivers = ReceiverGroup.All, 98 TargetActors = new int[] { requestSender.ID }, 99 }; 100 101 SendOptions sendOptions = new ExitGames.Client.Photon.SendOptions 102 { 103 Reliability = true, 104 }; 105 106 // Send info 107 int[] textureInfo = new int[4]; 108 textureInfo[0] = viewId; 109 textureInfo[1] = width; 110 textureInfo[2] = height; 111 textureInfo[3] = dataSize; 112 //PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.BeginStream, textureInfo, raiseEventOptions, sendOptions); //コメントアウト 113 PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.BeginStream, textureInfo, reliable, raiseEventOptions); //追記 114 115 116 // Send raw data 117 // The SlowDown operator is not necessary if you ignore the limit on the number of messages per second of Photon Cloud. 118 rawTextureData.ToObservable() 119 .Buffer(bytePerMessage) 120 // .SlowDown(1.0f/messagePerSecond) 121 .Subscribe(byteSubList => 122 { 123 byte[] sendData = new byte[byteSubList.Count]; 124 byteSubList.CopyTo(sendData, 0); 125 //PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.Streaming, sendData, raiseEventOptions, sendOptions); //コメントアウト 126 PhotonNetwork.RaiseEvent((byte)StreamingBytesEventCode.Streaming, sendData, reliable, raiseEventOptions); //追記 127 128 }); 129 } 130 131 //*************************************************************************** 132 // MasterClient -> Client (These methods are executed by the master client) 133 //*************************************************************************** 134 public void OnEvent(ExitGames.Client.Photon.EventData photonEvent) 135 { 136 if (photonEvent.Code == (byte)StreamingBytesEventCode.BeginStream) 137 { 138 int[] data = (int[])photonEvent.Parameters[ParameterCode.Data]; 139 OnReceivedTextureInfo(data); 140 } 141 if (photonEvent.Code == (byte)StreamingBytesEventCode.Streaming) 142 { 143 byte[] data = (byte[])photonEvent.Parameters[ParameterCode.Data]; 144 OnReceivedRawTextureDataStream(data); 145 } 146 } 147 148 void OnReceivedTextureInfo(int[] data) 149 { 150 int viewId = data[0]; 151 if (viewId != this.photonView.viewID) 152 { 153 this.isReceiving = false; 154 this.totalDataSize = 0; 155 this.currentReceivedDataSize = 0; 156 this.receivedMessageCount = 0; 157 return; 158 } 159 160 this.isReceiving = true; 161 this.currentReceivedDataSize = 0; 162 this.receivedMessageCount = 0; 163 164 int width = data[1]; 165 int height = data[2]; 166 int dataSize = data[3]; 167 this.totalDataSize = dataSize; 168 this.receiveBuffer = new byte[dataSize]; 169 170 Debug.Log("*************************"); 171 Debug.Log(" OnReceivedTextureInfo"); 172 Debug.Log(" Texture size: " + width + "x" + height + "px"); 173 Debug.Log(" RawTextureDataSize: " + dataSize); 174 Debug.Log("*************************"); 175 } 176 177 void OnReceivedRawTextureDataStream(byte[] data) 178 { 179 if (this.isReceiving) 180 { 181 data.CopyTo(this.receiveBuffer, this.currentReceivedDataSize); 182 this.currentReceivedDataSize += data.Length; 183 this.receivedMessageCount++; 184 185 if (this.currentReceivedDataSize >= (this.totalDataSize)) 186 { 187 this.isReceiving = false; 188 this.currentReceivedDataSize = 0; 189 this.receivedMessageCount = 0; 190 191 OnReceivedRawTextureData(); 192 } 193 } 194 } 195 196 void OnReceivedRawTextureData() 197 { 198 Debug.Log("********************************"); 199 Debug.Log(" OnReceivedRawTextureData "); 200 Debug.Log("********************************"); 201 202 texture.LoadImage(this.receiveBuffer); 203 texture.Apply(); 204 GetComponent<Renderer>().material.mainTexture = texture; 205 } 206 } 207}

補足情報(FW/ツールのバージョンなど)

Unity 2019.2.13f1
PUN Classic

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

izmktr

2020/01/20 10:11

多分、このエラー内容は送信側でデータを送ったけど対応するものが受信側にない、ってエラーだと思います テクスチャのシリアライズは忘れて、ストリームとしてバイナリデータを送るという部分で見直したほうがいいかと思います (ストリームは書いたことないのであくまで予想です)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問