Sample
How do I use Sample
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: CubeLerp.cs
Copy
35 public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36 {
37 if (stream.isWriting)
38 {
39 Vector3 pos = transform.localPosition;
40 Quaternion rot = transform.localRotation;
41 stream.Serialize(ref pos);
42 stream.Serialize(ref rot);
43 }
44 else
45 {
46 // Receive latest state information
47 Vector3 pos = Vector3.zero;
48 Quaternion rot = Quaternion.identity;
49
50 stream.Serialize(ref pos);
51 stream.Serialize(ref rot);
52
53 latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
54 onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
55 fraction = 0; // reset the fraction we alreay moved. see Update()
56
57 transform.localRotation = rot; // this sample doesn't smooth rotation
58 }
59 }
File name: PhotonHandler.cs
Copy
59 protected void Update()
60 {
61 if (PhotonNetwork.networkingPeer == null)
62 {
63 Debug.LogError("NetworkPeer broke!");
64 return;
65 }
66
67 if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated || PhotonNetwork.connectionStateDetailed == PeerState.Disconnected || PhotonNetwork.offlineMode)
68 {
69 return;
70 }
71
72 // the messageQueue might be paused. in that case a thread will send acknowledgements only. nothing else to do here.
73 if (!PhotonNetwork.isMessageQueueRunning)
74 {
75 return;
76 }
77
78 bool doDispatch = true;
79 while (PhotonNetwork.isMessageQueueRunning && doDispatch)
80 {
81 // DispatchIncomingCommands() returns true of it found any command to dispatch (event, result or state change)
82 UnityEngine.Profiling.Profiler.BeginSample("DispatchIncomingCommands");
83 doDispatch = PhotonNetwork.networkingPeer.DispatchIncomingCommands();
84 UnityEngine.Profiling.Profiler.EndSample();
85 }
86
87 int currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000); // avoiding Environment.TickCount, which could be negative on long-running platforms
88 if (PhotonNetwork.isMessageQueueRunning && currentMsSinceStart > this.nextSendTickCountOnSerialize)
89 {
90 PhotonNetwork.networkingPeer.RunViewUpdate();
91 this.nextSendTickCountOnSerialize = currentMsSinceStart + this.updateIntervalOnSerialize;
92 this.nextSendTickCount = 0; // immediately send when synchronization code was running
93 }
94
95 currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000);
96 if (currentMsSinceStart > this.nextSendTickCount)
97 {
98 bool doSend = true;
99 while (PhotonNetwork.isMessageQueueRunning && doSend)
100 {
101 // Send all outgoing commands
102 UnityEngine.Profiling.Profiler.BeginSample("SendOutgoingCommands");
103 doSend = PhotonNetwork.networkingPeer.SendOutgoingCommands();
104 UnityEngine.Profiling.Profiler.EndSample();
105 }
106
107 this.nextSendTickCount = currentMsSinceStart + this.updateInterval;
108 }
109 }
File name: PhotonStreamQueue.cs
Copy
32 public PhotonStreamQueue( int sampleRate )
33 {
34 m_SampleRate = sampleRate;
35 }
File name: PhotonStreamQueue.cs
Copy
37 void BeginWritePackage()
38 {
39 //If not enough time has passed since the last sample, we don't want to write anything
40 if( Time.realtimeSinceStartup < m_LastSampleTime + 1f / m_SampleRate )
41 {
42 m_IsWriting = false;
43 return;
44 }
45
46 if( m_SampleCount == 1 )
47 {
48 m_ObjectsPerSample = m_Objects.Count;
49 //Debug.Log( "Setting m_ObjectsPerSample to " + m_ObjectsPerSample );
50 }
51 else if( m_SampleCount > 1 )
52 {
53 if( m_Objects.Count / m_SampleCount != m_ObjectsPerSample )
54 {
55 Debug.LogWarning( "The number of objects sent via a PhotonStreamQueue has to be the same each frame" );
56 Debug.LogWarning( "Objects in List: " + m_Objects.Count + " / Sample Count: " + m_SampleCount + " = " + ( m_Objects.Count / m_SampleCount ) + " != " + m_ObjectsPerSample );
57 }
58 }
59
60 /*if( m_SampleCount > 1 )
61 {
62 Debug.Log( "Check: " + m_Objects.Count + " / " + m_SampleCount + " = " + ( m_Objects.Count / m_SampleCount ) + " = " + m_ObjectsPerSample );
63 }*/
64
65 m_IsWriting = true;
66 m_SampleCount++;
67 m_LastSampleTime = Time.realtimeSinceStartup;
68
69 }
File name: PhotonStreamQueue.cs
Copy
74 public void Reset()
75 {
76 m_SampleCount = 0;
77 m_ObjectsPerSample = -1;
78
79 m_LastSampleTime = -Mathf.Infinity;
80 m_LastFrameCount = -1;
81
82 m_Objects.Clear();
83 }
File name: PhotonStreamQueue.cs
Copy
118 public object ReceiveNext()
119 {
120 if( m_NextObjectIndex == -1 )
121 {
122 return null;
123 }
124
125 if( m_NextObjectIndex >= m_Objects.Count )
126 {
127 m_NextObjectIndex -= m_ObjectsPerSample;
128 }
129
130 return m_Objects[ m_NextObjectIndex++ ];
131 }
File name: PhotonStreamQueue.cs
Copy
137 public void Serialize( PhotonStream stream )
138 {
139 stream.SendNext( m_SampleCount );
140 stream.SendNext( m_ObjectsPerSample );
141
142 for( int i = 0; i < m_Objects.Count; ++i )
143 {
144 stream.SendNext( m_Objects[ i ] );
145 }
146
147 //Debug.Log( "Serialize " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " / " + ( m_SampleCount * m_ObjectsPerSample ) );
148
149 m_Objects.Clear();
150 m_SampleCount = 0;
151 }
File name: PhotonStreamQueue.cs
Copy
157 public void Deserialize( PhotonStream stream )
158 {
159 m_Objects.Clear();
160
161 m_SampleCount = (int)stream.ReceiveNext();
162 m_ObjectsPerSample = (int)stream.ReceiveNext();
163
164 for( int i = 0; i < m_SampleCount * m_ObjectsPerSample; ++i )
165 {
166 m_Objects.Add( stream.ReceiveNext() );
167 }
168
169 if( m_Objects.Count > 0 )
170 {
171 m_NextObjectIndex = 0;
172 }
173 else
174 {
175 m_NextObjectIndex = -1;
176 }
177
178 //Debug.Log( "Deserialized " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " / " + ( m_SampleCount * m_ObjectsPerSample ) );
179 }
File name: frmLoginDetails.cs
Copy
29 public DataView GetData()
30 {
31 dynamic SelectQry = "SELECT RTRIM(Username) as [User Name],RTRIM(Password) as [Password] FROM Registration ";
32 DataSet SampleSource = new DataSet();
33 DataView TableView = null;
34 try
35 {
36 SqlCommand SampleCommand = new SqlCommand();
37 dynamic SampleDataAdapter = new SqlDataAdapter();
38 SampleCommand.CommandText = SelectQry;
39 SampleCommand.Connection = Connection;
40 SampleDataAdapter.SelectCommand = SampleCommand;
41 SampleDataAdapter.Fill(SampleSource);
42 TableView = SampleSource.Tables[0].DefaultView;
43 }
44 catch (Exception ex)
45 {
46 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
47 }
48 return TableView;
49 }
File name: frmRegisteredUsersDetails.cs
Copy
29 public DataView GetData()
30 {
31 dynamic SelectQry = "SELECT RTRIM(Username) as [User Name],RTRIM(Password) as [Password],RTRIM(Name) as [Name],RTRIM(ContactNo) as [Contact No.],RTRIM(Email) as [Email ID],RTRIM(joiningdate) as [Date Of Joining] FROM registration";
32 DataSet SampleSource = new DataSet();
33 DataView TableView = null;
34 try
35 {
36 SqlCommand SampleCommand = new SqlCommand();
37 dynamic SampleDataAdapter = new SqlDataAdapter();
38 SampleCommand.CommandText = SelectQry;
39 SampleCommand.Connection = Connection;
40 SampleDataAdapter.SelectCommand = SampleCommand;
41 SampleDataAdapter.Fill(SampleSource);
42 TableView = SampleSource.Tables[0].DefaultView;
43 }
44 catch (Exception ex)
45 {
46 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
47 }
48 return TableView;
49 }
Download file with original file name:Sample
Sample 209 lượt xem
Gõ tìm kiếm nhanh...