UnreliableOnChange
How do I use Unreliable On Change
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonViewInspector.cs
Copy
209 void DrawOldObservedItem()
210 {
211 EditorGUILayout.BeginHorizontal();
212
213 // Using a lower version then 3.4? Remove the TRUE in the next line to fix an compile error
214 string typeOfObserved = string.Empty;
215 if( m_Target.observed != null )
216 {
217 int firstBracketPos = m_Target.observed.ToString().LastIndexOf( '(' );
218 if( firstBracketPos > 0 )
219 {
220 typeOfObserved = m_Target.observed.ToString().Substring( firstBracketPos );
221 }
222 }
223
224
225 Component componenValue = (Component)EditorGUILayout.ObjectField( "Observe: " + typeOfObserved, m_Target.observed, typeof( Component ), true );
226 if( m_Target.observed != componenValue )
227 {
228 if( m_Target.observed == null )
229 {
230 m_Target.synchronization = ViewSynchronization.UnreliableOnChange; // if we didn't observe anything yet. use unreliable on change as default
231 }
232 if( componenValue == null )
233 {
234 m_Target.synchronization = ViewSynchronization.Off;
235 }
236
237 m_Target.observed = componenValue;
238 }
239
240 EditorGUILayout.EndHorizontal();
241 }
File name: PhotonViewInspector.cs
Copy
258 void DrawObservedComponentsList()
259 {
260 GUILayout.Space( 5 );
261 SerializedProperty listProperty = serializedObject.FindProperty( "ObservedComponents" );
262
263 if( listProperty == null )
264 {
265 return;
266 }
267
268 float containerElementHeight = 22;
269 float containerHeight = listProperty.arraySize * containerElementHeight;
270
271 bool isOpen = PhotonGUI.ContainerHeaderFoldout( "Observed Components (" + GetObservedComponentsCount() + ")", serializedObject.FindProperty( "ObservedComponentsFoldoutOpen" ).boolValue );
272 serializedObject.FindProperty( "ObservedComponentsFoldoutOpen" ).boolValue = isOpen;
273
274 if( isOpen == false )
275 {
276 containerHeight = 0;
277 }
278
279 //Texture2D statsIcon = AssetDatabase.LoadAssetAtPath( "Assets/Photon Unity Networking/Editor/PhotonNetwork/PhotonViewStats.png", typeof( Texture2D ) ) as Texture2D;
280
281 Rect containerRect = PhotonGUI.ContainerBody( containerHeight );
282 bool wasObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;
283 if( isOpen == true )
284 {
285 for( int i = 0; i < listProperty.arraySize; ++i )
286 {
287 Rect elementRect = new Rect( containerRect.xMin, containerRect.yMin + containerElementHeight * i, containerRect.width, containerElementHeight );
288 {
289 Rect texturePosition = new Rect( elementRect.xMin + 6, elementRect.yMin + elementRect.height / 2f - 1, 9, 5 );
290 ReorderableListResources.DrawTexture( texturePosition, ReorderableListResources.texGrabHandle );
291
292 Rect propertyPosition = new Rect( elementRect.xMin + 20, elementRect.yMin + 3, elementRect.width - 45, 16 );
293 EditorGUI.PropertyField( propertyPosition, listProperty.GetArrayElementAtIndex( i ), new GUIContent() );
294
295 //Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );
296 //Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );
297 //ReorderableListResources.DrawTexture( statsPosition, statsIcon );
298
299 Rect removeButtonRect = new Rect( elementRect.xMax - PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
300 elementRect.yMin + 2,
301 PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
302 PhotonGUI.DefaultRemoveButtonStyle.fixedHeight );
303
304 GUI.enabled = listProperty.arraySize > 1;
305 if( GUI.Button( removeButtonRect, new GUIContent( ReorderableListResources.texRemoveButton ), PhotonGUI.DefaultRemoveButtonStyle ) )
306 {
307 listProperty.DeleteArrayElementAtIndex( i );
308 }
309 GUI.enabled = true;
310
311 if( i < listProperty.arraySize - 1 )
312 {
313 texturePosition = new Rect( elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4, 1 );
314 PhotonGUI.DrawSplitter( texturePosition );
315 }
316 }
317 }
318 }
319
320 if( PhotonGUI.AddButton() )
321 {
322 listProperty.InsertArrayElementAtIndex( Mathf.Max( 0, listProperty.arraySize - 1 ) );
323 }
324
325 serializedObject.ApplyModifiedProperties();
326
327 bool isObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;
328
329 if( wasObservedComponentsEmpty == true && isObservedComponentsEmpty == false && m_Target.synchronization == ViewSynchronization.Off )
330 {
331 m_Target.synchronization = ViewSynchronization.UnreliableOnChange;
332 EditorUtility.SetDirty( m_Target );
333 serializedObject.Update();
334 }
335
336 if( wasObservedComponentsEmpty == false && isObservedComponentsEmpty == true )
337 {
338 m_Target.synchronization = ViewSynchronization.Off;
339 EditorUtility.SetDirty( m_Target );
340 serializedObject.Update();
341 }
342
343 }
File name: NetworkingPeer.cs
Copy
3271 // calls OnPhotonSerializeView (through ExecuteOnSerialize)
3273 private Hashtable OnSerializeWrite(PhotonView view)
3274 {
3275 PhotonStream pStream = new PhotonStream( true, null );
3276 PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );
3277
3278 // each view creates a list of values that should be sent
3279 view.SerializeView( pStream, info );
3280
3281 if( pStream.Count == 0 )
3282 {
3283 return null;
3284 }
3285
3286 object[] dataArray = pStream.data.ToArray();
3287
3288 if (view.synchronization == ViewSynchronization.UnreliableOnChange)
3289 {
3290 if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
3291 {
3292 if (view.mixedModeIsReliable)
3293 {
3294 return null;
3295 }
3296
3297 view.mixedModeIsReliable = true;
3298 view.lastOnSerializeDataSent = dataArray;
3299 }
3300 else
3301 {
3302 view.mixedModeIsReliable = false;
3303 view.lastOnSerializeDataSent = dataArray;
3304 }
3305 }
3306
3307 // EVDATA:
3308 // 0=View ID (an int, never compressed cause it's not in the data)
3309 // 1=data of observed type (different per type of observed object)
3310 // 2=compressed data (in this case, key 1 is empty)
3311 // 3=list of values that are actually null (if something was changed but actually IS null)
3312 Hashtable evData = new Hashtable();
3313 evData[(byte)0] = (int)view.viewID;
3314 evData[(byte)1] = dataArray; // this is the actual data (script or observed object)
3315
3316
3317 if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3318 {
3319 // compress content of data set (by comparing to view.lastOnSerializeDataSent)
3320 // the "original" dataArray is NOT modified by DeltaCompressionWrite
3321 // if something was compressed, the evData key 2 and 3 are used (see above)
3322 bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);
3323
3324 // buffer the full data set (for next compression)
3325 view.lastOnSerializeDataSent = dataArray;
3326
3327 if (!somethingLeftToSend)
3328 {
3329 return null;
3330 }
3331 }
3332
3333 return evData;
3334 }
UnreliableOnChange 118 lượt xem
Gõ tìm kiếm nhanh...