ObservedComponents
How do I use Observed Components
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
180 void ConvertOldObservedItemToObservedList()
181 {
182 if( m_Target.observed != null )
183 {
184 if( m_Target.ObservedComponents.Contains( m_Target.observed ) == false )
185 {
186 bool wasAdded = false;
187
188 for( int i = 0; i < m_Target.ObservedComponents.Count; ++i )
189 {
190 if( m_Target.ObservedComponents[ i ] == null )
191 {
192 m_Target.ObservedComponents[ i ] = m_Target.observed;
193 wasAdded = true;
194 }
195 }
196
197 if( wasAdded == false )
198 {
199 m_Target.ObservedComponents.Add( m_Target.observed );
200 }
201 }
202
203 m_Target.observed = null;
204 EditorUtility.SetDirty( m_Target );
205 }
206 }
File name: PhotonViewInspector.cs
Copy
243 int GetObservedComponentsCount()
244 {
245 int count = 0;
246
247 for( int i = 0; i < m_Target.ObservedComponents.Count; ++i )
248 {
249 if( m_Target.ObservedComponents[ i ] != null )
250 {
251 count++;
252 }
253 }
254
255 return count;
256 }
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: PhotonView.cs
Copy
328 public void SerializeView( PhotonStream stream, PhotonMessageInfo info )
329 {
330 SerializeComponent( observed, stream, info );
331
332 for( int i = 0; i < ObservedComponents.Count; ++i )
333 {
334 SerializeComponent( ObservedComponents[ i ], stream, info );
335 }
336 }
File name: PhotonView.cs
Copy
338 public void DeserializeView( PhotonStream stream, PhotonMessageInfo info )
339 {
340 DeserializeComponent( observed, stream, info );
341
342 for( int i = 0; i < ObservedComponents.Count; ++i )
343 {
344 DeserializeComponent( ObservedComponents[ i ], stream, info );
345 }
346 }
ObservedComponents 117 lượt xem
Gõ tìm kiếm nhanh...