Serialization
How do I use Serialization
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonViewInspector.cs
Copy
21 public override void OnInspectorGUI()
22 {
23 #if UNITY_3_5
24 EditorGUIUtility.LookLikeInspector();
25 #endif
26 //EditorGUI.indentLevel = 1;
27
28 m_Target = (PhotonView)this.target;
29 bool isProjectPrefab = EditorUtility.IsPersistent(m_Target.gameObject);
30
31 if( m_Target.ObservedComponents == null )
32 {
33 m_Target.ObservedComponents = new System.Collections.Generic.List
34 }
35
36 if( m_Target.ObservedComponents.Count == 0 )
37 {
38 m_Target.ObservedComponents.Add( null );
39 }
40
41 EditorGUILayout.BeginHorizontal();
42 // Owner
43 if (isProjectPrefab)
44 {
45 EditorGUILayout.LabelField("Owner:", "Set at runtime");
46 }
47 else if (m_Target.isSceneView)
48 {
49 EditorGUILayout.LabelField("Owner", "Scene");
50 }
51 else
52 {
53 PhotonPlayer owner = m_Target.owner;
54 string ownerInfo = (owner != null) ? owner.name : "
55
56 if (string.IsNullOrEmpty(ownerInfo))
57 {
58 ownerInfo = "
59 }
60
61 EditorGUILayout.LabelField("Owner", "[" + m_Target.ownerId + "] " + ownerInfo);
62 }
63
64 // ownership requests
65 EditorGUI.BeginDisabledGroup(Application.isPlaying);
66 m_Target.ownershipTransfer = (OwnershipOption)EditorGUILayout.EnumPopup(m_Target.ownershipTransfer, GUILayout.Width(100));
67 EditorGUI.EndDisabledGroup();
68
69 EditorGUILayout.EndHorizontal();
70
71
72 // View ID
73 if (isProjectPrefab)
74 {
75 EditorGUILayout.LabelField("View ID", "Set at runtime");
76 }
77 else if (EditorApplication.isPlaying)
78 {
79 EditorGUILayout.LabelField("View ID", m_Target.viewID.ToString());
80 }
81 else
82 {
83 int idValue = EditorGUILayout.IntField("View ID [1.."+(PhotonNetwork.MAX_VIEW_IDS-1)+"]", m_Target.viewID);
84 m_Target.viewID = idValue;
85 }
86
87
88
89 // Locally Controlled
90 if (EditorApplication.isPlaying)
91 {
92 string masterClientHint = PhotonNetwork.isMasterClient ? "(master)" : "";
93 EditorGUILayout.Toggle("Controlled locally: " + masterClientHint, m_Target.isMine);
94 }
95
96
97
98 //DrawOldObservedItem();
99 ConvertOldObservedItemToObservedList();
100
101
102 // ViewSynchronization (reliability)
103 if (m_Target.synchronization == ViewSynchronization.Off)
104 {
105 GUI.color = Color.grey;
106 }
107
108 EditorGUILayout.PropertyField( serializedObject.FindProperty( "synchronization" ), new GUIContent( "Observe option:" ) );
109
110 if( m_Target.synchronization != ViewSynchronization.Off &&
111 m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0 )
112 {
113 GUILayout.BeginVertical( GUI.skin.box );
114 GUILayout.Label( "Warning", EditorStyles.boldLabel );
115 GUILayout.Label( "Setting the synchronization option only makes sense if you observe something." );
116 GUILayout.EndVertical();
117 }
118
119 /*ViewSynchronization vsValue = (ViewSynchronization)EditorGUILayout.EnumPopup("Observe option:", m_Target.synchronization);
120 if (vsValue != m_Target.synchronization)
121 {
122 m_Target.synchronization = vsValue;
123 if (m_Target.synchronization != ViewSynchronization.Off && m_Target.observed == null)
124 {
125 EditorUtility.DisplayDialog("Warning", "Setting the synchronization option only makes sense if you observe something.", "OK, I will fix it.");
126 }
127 }*/
128
129 DrawSpecificTypeSerializationOptions();
130
131 GUI.color = Color.white;
132 DrawObservedComponentsList();
133
134 // Cleanup: save and fix look
135 if (GUI.changed)
136 {
137 EditorUtility.SetDirty(m_Target);
138 PhotonViewHandler.HierarchyChange(); // TODO: check if needed
139 }
140
141 GUI.color = Color.white;
142 EditorGUIUtility.LookLikeControls();
143 }
File name: PhotonViewInspector.cs
Copy
145 void DrawSpecificTypeSerializationOptions()
146 {
147 if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Transform ) ).Count > 0 ||
148 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Transform ) ) )
149 {
150 m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup( "Transform Serialization:", m_Target.onSerializeTransformOption );
151
152 }
153 else if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Rigidbody ) ).Count > 0 ||
154 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Rigidbody ) ) ||
155 m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Rigidbody2D ) ).Count > 0 ||
156 ( m_Target.observed != null && m_Target.observed.GetType() == typeof( Rigidbody2D ) ) )
157 {
158 m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup( "Rigidbody Serialization:", m_Target.onSerializeRigidBodyOption );
159 }
160 }
File name: PhotonViewInspector.cs
Copy
162 void DrawSpecificTypeOptions()
163 {
164 if( m_Target.observed != null )
165 {
166 Type type = m_Target.observed.GetType();
167 if( type == typeof( Transform ) )
168 {
169 m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup( "Serialization:", m_Target.onSerializeTransformOption );
170
171 }
172 else if( type == typeof( Rigidbody ) )
173 {
174 m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup( "Serialization:", m_Target.onSerializeRigidBodyOption );
175
176 }
177 }
178 }
File name: NetworkingPeer.cs
Copy
3339 private void OnSerializeRead(Hashtable data, PhotonPlayer sender, int networkTime, short correctPrefix)
3340 {
3341 // read view ID from key (byte)0: a int-array (PUN 1.17++)
3342 int viewID = (int)data[(byte)0];
3343
3344
3345 PhotonView view = this.GetPhotonView(viewID);
3346 if (view == null)
3347 {
3348 Debug.LogWarning("Received OnSerialization for view ID " + viewID + ". We have no such PhotonView! Ignored this if you're leaving a room. State: " + this.State);
3349 return;
3350 }
3351
3352 if (view.prefix > 0 && correctPrefix != view.prefix)
3353 {
3354 Debug.LogError("Received OnSerialization for view ID " + viewID + " with prefix " + correctPrefix + ". Our prefix is " + view.prefix);
3355 return;
3356 }
3357
3358 // SetReceiving filtering
3359 if (view.group != 0 && !this.allowedReceivingGroups.Contains(view.group))
3360 {
3361 return; // Ignore group
3362 }
3363
3364
3365 if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3366 {
3367 if (!this.DeltaCompressionRead(view, data))
3368 {
3369 // Skip this packet as we haven't got received complete-copy of this view yet.
3370 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
3371 Debug.Log("Skipping packet for " + view.name + " [" + view.viewID + "] as we haven't received a full packet for delta compression yet. This is OK if it happens for the first few frames after joining a game.");
3372 return;
3373 }
3374
3375 // store last received for delta-compression usage
3376 view.lastOnSerializeDataReceived = data[(byte)1] as object[];
3377 }
3378
3379 if (sender.ID != view.ownerId)
3380 {
3381 if (!view.isSceneView || !sender.isMasterClient)
3382 {
3383 // obviously the owner changed and we didn't yet notice.
3384 Debug.Log("Adjusting owner to sender of updates. From: " + view.ownerId + " to: " + sender.ID);
3385 view.ownerId = sender.ID;
3386 }
3387 }
3388
3389 object[] contents = data[(byte)1] as object[];
3390 PhotonStream pStream = new PhotonStream(false, contents);
3391 PhotonMessageInfo info = new PhotonMessageInfo(sender, networkTime, view);
3392
3393 view.DeserializeView( pStream, info );
3394 }
Serialization 135 lượt xem
Gõ tìm kiếm nhanh...