Observe









How do I use Observe
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PickupController.cs Copy
73     // Are we jumping? (Initiated with jump button and not grounded yet)
77     // Are we moving backwards (This locks the camera to not do a 180 degree spin)
81     // When did the user start walking (Used for going into trot after a while)
87     // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
101     void Awake()
102     {
103         // PUN: automatically determine isControllable, if this GO has a PhotonView
104         PhotonView pv = this.gameObject.GetComponent();
105         if (pv != null)
106         {
107             isControllable = pv.isMine;
108
109             // The pickup demo assigns this GameObject as the PhotonPlayer.TagObject. This way, we can access this character (controller, position, etc) easily
110             if (this.AssignAsTagObject)
111             {
112                 pv.owner.TagObject = this.gameObject;
113             }
114
115             // please note: we change this setting on ANY PickupController if "DoRotate" is off. not only locally when it's "our" GameObject!
116             if (pv.observed is Transform && !DoRotate)
117             {
118                 pv.onSerializeTransformOption = OnSerializeTransform.OnlyPosition;
119             }
120         }
121
122
123         moveDirection = transform.TransformDirection(Vector3.forward);
124
125         _animation = GetComponent();
126         if (!_animation)
127             Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");
128
129         if (!idleAnimation)
130         {
131             _animation = null;
132             Debug.Log("No idle animation found. Turning off animations.");
133         }
134         if (!walkAnimation)
135         {
136             _animation = null;
137             Debug.Log("No walk animation found. Turning off animations.");
138         }
139         if (!runAnimation)
140         {
141             _animation = null;
142             Debug.Log("No run animation found. Turning off animations.");
143         }
144         if (!jumpPoseAnimation && canJump)
145         {
146             _animation = null;
147             Debug.Log("No jump animation found and the character has canJump enabled. Turning off animations.");
148         }
149     }
File name: PhotonConverter.cs Copy
351     static int ConvertNetworkView(NetworkView[] netViews, bool isScene)
352     {
353         for (int i = netViews.Length - 1; i >= 0; i--)
354         {
355             NetworkView netView = netViews[i];
356             PhotonView view = netView.gameObject.AddComponent();
357             if (isScene)
358             {
359                 //Get scene ID
360                 string str = netView.viewID.ToString().Replace("SceneID: ", "");
361                 int firstSpace = str.IndexOf(" ");
362                 str = str.Substring(0, firstSpace);
363                 int oldViewID = int.Parse(str);
364
365                 view.viewID = oldViewID;
366                 EditorUtility.SetDirty(view);
367                 EditorUtility.SetDirty(view.gameObject);
368             }
369             view.observed = netView.observed;
370             if (netView.stateSynchronization == NetworkStateSynchronization.Unreliable)
371             {
372                 view.synchronization = ViewSynchronization.Unreliable;
373             }
374             else if (netView.stateSynchronization == NetworkStateSynchronization.ReliableDeltaCompressed)
375             {
376                 view.synchronization = ViewSynchronization.ReliableDeltaCompressed;
377             }
378             else
379             {
380                 view.synchronization = ViewSynchronization.Off;
381             }
382             DestroyImmediate(netView, true);
383         }
384         AssetDatabase.Refresh();
385         AssetDatabase.SaveAssets();
386
387         return netViews.Length;
388     }
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: 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
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
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: 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     }

Observe 140 lượt xem

Gõ tìm kiếm nhanh...