OfflineMode
How do I use Offline Mode
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: ServerSettingsInspector.cs
Copy
17 public override void OnInspectorGUI()
18 {
19 ServerSettings settings = (ServerSettings)this.target;
20
21 #if UNITY_3_5
22 EditorGUIUtility.LookLikeInspector();
23 #endif
24
25
26 settings.HostType = (ServerSettings.HostingOption)EditorGUILayout.EnumPopup("Hosting", settings.HostType);
27 EditorGUI.indentLevel = 1;
28
29 switch (settings.HostType)
30 {
31 case ServerSettings.HostingOption.BestRegion:
32 case ServerSettings.HostingOption.PhotonCloud:
33 if (settings.HostType == ServerSettings.HostingOption.PhotonCloud)
34 settings.PreferredRegion = (CloudRegionCode)EditorGUILayout.EnumPopup("Region", settings.PreferredRegion);
35 settings.AppID = EditorGUILayout.TextField("AppId", settings.AppID);
36 settings.Protocol = (ConnectionProtocol)EditorGUILayout.EnumPopup("Protocol", settings.Protocol);
37
38 if (string.IsNullOrEmpty(settings.AppID) || settings.AppID.Equals("master"))
39 {
40 EditorGUILayout.HelpBox("The Photon Cloud needs an AppId (GUID) set.\nYou can find it online in your Dashboard.", MessageType.Warning);
41 }
42 break;
43
44 case ServerSettings.HostingOption.SelfHosted:
45 bool hidePort = false;
46 if (settings.Protocol == ConnectionProtocol.Udp && (settings.ServerPort == 4530 || settings.ServerPort == 0))
47 {
48 settings.ServerPort = 5055;
49 }
50 else if (settings.Protocol == ConnectionProtocol.Tcp && (settings.ServerPort == 5055 || settings.ServerPort == 0))
51 {
52 settings.ServerPort = 4530;
53 }
54 #if RHTTP
55 if (settings.Protocol == ConnectionProtocol.RHttp)
56 {
57 settings.ServerPort = 0;
58 hidePort = true;
59 }
60 #endif
61 settings.ServerAddress = EditorGUILayout.TextField("Server Address", settings.ServerAddress);
62 settings.ServerAddress = settings.ServerAddress.Trim();
63 if (!hidePort)
64 {
65 settings.ServerPort = EditorGUILayout.IntField("Server Port", settings.ServerPort);
66 }
67 settings.Protocol = (ConnectionProtocol)EditorGUILayout.EnumPopup("Protocol", settings.Protocol);
68 settings.AppID = EditorGUILayout.TextField("AppId", settings.AppID);
69 break;
70
71 case ServerSettings.HostingOption.OfflineMode:
72 EditorGUI.indentLevel = 0;
73 EditorGUILayout.HelpBox("In 'Offline Mode', the client does not communicate with a server.\nAll settings are hidden currently.", MessageType.Info);
74 break;
75
76 case ServerSettings.HostingOption.NotSet:
77 EditorGUI.indentLevel = 0;
78 EditorGUILayout.HelpBox("Hosting is 'Not Set'.\nConnectUsingSettings() will not be able to connect.\nSelect another option or run the PUN Wizard.", MessageType.Info);
79 break;
80
81 default:
82 DrawDefaultInspector();
83 break;
84 }
85
86 if (PhotonEditor.CheckPunPlus())
87 {
88 settings.Protocol = ConnectionProtocol.Udp;
89 EditorGUILayout.HelpBox("You seem to use PUN+.\nPUN+ only supports reliable UDP so the protocol is locked.", MessageType.Info);
90 }
91
92 settings.AppID = settings.AppID.Trim();
93
94 EditorGUI.indentLevel = 0;
95 SerializedObject sObj = new SerializedObject(this.target);
96 SerializedProperty sRpcs = sObj.FindProperty("RpcList");
97 EditorGUILayout.PropertyField(sRpcs, true);
98 sObj.ApplyModifiedProperties();
99
100 GUILayout.BeginHorizontal();
101 GUILayout.Space(20);
102 if (GUILayout.Button("Refresh RPCs"))
103 {
104 PhotonEditor.UpdateRpcList();
105 Repaint();
106 }
107 if (GUILayout.Button("Clear RPCs"))
108 {
109 PhotonEditor.ClearRpcList();
110 }
111 if (GUILayout.Button("Log HashCode"))
112 {
113 Debug.Log("RPC-List HashCode: " + RpcListHashCode() + ". Make sure clients that send each other RPCs have the same RPC-List.");
114 }
115 GUILayout.Space(20);
116 GUILayout.EndHorizontal();
117
118 //SerializedProperty sp = serializedObject.FindProperty("RpcList");
119 //EditorGUILayout.PropertyField(sp, true);
120
121 if (GUI.changed)
122 {
123 EditorUtility.SetDirty(target);
124 }
125 }
File name: NetworkingPeer.cs
Copy
997 public override bool OpRaiseEvent(byte eventCode, object customEventContent, bool sendReliable, RaiseEventOptions raiseEventOptions)
998 {
999 if (PhotonNetwork.offlineMode)
1000 {
1001 return false;
1002 }
1003
1004 return base.OpRaiseEvent(eventCode, customEventContent, sendReliable, raiseEventOptions);
1005 }
File name: NetworkingPeer.cs
Copy
2890 /// (byte)0 -> (int) ViewId (combined from actorNr and actor-unique-id)
2891 /// (byte)1 -> (short) prefix (level)
2895 /// (byte)5 -> (byte) method shortcut (alternative to name)
2899 internal void RPC(PhotonView view, string methodName, PhotonTargets target, bool encrypt, params object[] parameters)
2900 {
2901 if (this.blockSendingGroups.Contains(view.group))
2902 {
2903 return; // Block sending on this group
2904 }
2905
2906 if (view.viewID < 1)
2907 {
2908 Debug.LogError("Illegal view ID:" + view.viewID + " method: " + methodName + " GO:" + view.gameObject.name);
2909 }
2910
2911 if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2912 Debug.Log("Sending RPC \"" + methodName + "\" to " + target);
2913
2914
2915 //ts: changed RPCs to a one-level hashtable as described in internal.txt
2916 Hashtable rpcEvent = new Hashtable();
2917 rpcEvent[(byte)0] = (int)view.viewID; // LIMITS NETWORKVIEWS&PLAYERS
2918 if (view.prefix > 0)
2919 {
2920 rpcEvent[(byte)1] = (short)view.prefix;
2921 }
2922 rpcEvent[(byte)2] = this.ServerTimeInMilliSeconds;
2923
2924
2925 // send name or shortcut (if available)
2926 int shortcut = 0;
2927 if (rpcShortcuts.TryGetValue(methodName, out shortcut))
2928 {
2929 rpcEvent[(byte)5] = (byte)shortcut; // LIMITS RPC COUNT
2930 }
2931 else
2932 {
2933 rpcEvent[(byte)3] = methodName;
2934 }
2935
2936 if (parameters != null && parameters.Length > 0)
2937 {
2938 rpcEvent[(byte)4] = (object[])parameters;
2939 }
2940
2941 // Check scoping
2942 if (target == PhotonTargets.All)
2943 {
2944 RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Encrypt = encrypt };
2945 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2946
2947 // Execute local
2948 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2949 }
2950 else if (target == PhotonTargets.Others)
2951 {
2952 RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Encrypt = encrypt };
2953 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2954 }
2955 else if (target == PhotonTargets.AllBuffered)
2956 {
2957 RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2958 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2959
2960 // Execute local
2961 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2962 }
2963 else if (target == PhotonTargets.OthersBuffered)
2964 {
2965 RaiseEventOptions options = new RaiseEventOptions() { CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2966 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2967 }
2968 else if (target == PhotonTargets.MasterClient)
2969 {
2970 if (this.mMasterClient == this.mLocalActor)
2971 {
2972 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2973 }
2974 else
2975 {
2976 RaiseEventOptions options = new RaiseEventOptions() { Receivers = ReceiverGroup.MasterClient, Encrypt = encrypt };
2977 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2978 }
2979 }
2980 else if (target == PhotonTargets.AllViaServer)
2981 {
2982 RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Receivers = ReceiverGroup.All, Encrypt = encrypt };
2983 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2984 if (PhotonNetwork.offlineMode)
2985 {
2986 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2987 }
2988 }
2989 else if (target == PhotonTargets.AllBufferedViaServer)
2990 {
2991 RaiseEventOptions options = new RaiseEventOptions() { InterestGroup = (byte)view.group, Receivers = ReceiverGroup.All, CachingOption = EventCaching.AddToRoomCache, Encrypt = encrypt };
2992 this.OpRaiseEvent(PunEvent.RPC, rpcEvent, true, options);
2993 if (PhotonNetwork.offlineMode)
2994 {
2995 this.ExecuteRPC(rpcEvent, this.mLocalActor);
2996 }
2997 }
2998 else
2999 {
3000 Debug.LogError("Unsupported target enum: " + target);
3001 }
3002 }
File name: NetworkingPeer.cs
Copy
3147 public void RunViewUpdate()
3148 {
3149 if (!PhotonNetwork.connected || PhotonNetwork.offlineMode)
3150 {
3151 return;
3152 }
3153
3154 if (this.mActors == null ||
3155#if !PHOTON_DEVELOP
3156 this.mActors.Count <= 1
3157#endif
3158 )
3159 {
3160 return; // No need to send OnSerialize messages (these are never buffered anyway)
3161 }
3162
3163 dataPerGroupReliable.Clear();
3164 dataPerGroupUnreliable.Clear();
3165
3166 /* Format of the data hashtable:
3167 * Hasthable dataPergroup*
3168 * [(byte)0] = this.ServerTimeInMilliSeconds;
3169 * OPTIONAL: [(byte)1] = currentLevelPrefix;
3170 * + data
3171 */
3172
3173 foreach (KeyValuePair
3174 {
3175 PhotonView view = kvp.Value;
3176
3177 if (view.synchronization != ViewSynchronization.Off)
3178 {
3179 // Fetch all sending photonViews
3180 if (view.isMine)
3181 {
3182 #if UNITY_2_6_1 || UNITY_2_6 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
3183 if (!view.gameObject.active)
3184 {
3185 continue; // Only on actives
3186 }
3187 #else
3188 if (!view.gameObject.activeInHierarchy)
3189 {
3190 continue; // Only on actives
3191 }
3192 #endif
3193
3194 if (this.blockSendingGroups.Contains(view.group))
3195 {
3196 continue; // Block sending on this group
3197 }
3198
3199 // Run it trough its OnSerialize
3200 Hashtable evData = this.OnSerializeWrite(view);
3201 if (evData == null)
3202 {
3203 continue;
3204 }
3205
3206 if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed || view.mixedModeIsReliable)
3207 {
3208 if (!evData.ContainsKey((byte)1) && !evData.ContainsKey((byte)2))
3209 {
3210 // Everything has been removed by compression, nothing to send
3211 }
3212 else
3213 {
3214 if (!dataPerGroupReliable.ContainsKey(view.group))
3215 {
3216 dataPerGroupReliable[view.group] = new Hashtable();
3217 dataPerGroupReliable[view.group][(byte)0] = this.ServerTimeInMilliSeconds;
3218 if (currentLevelPrefix >= 0)
3219 {
3220 dataPerGroupReliable[view.group][(byte)1] = this.currentLevelPrefix;
3221 }
3222 }
3223 Hashtable groupHashtable = dataPerGroupReliable[view.group];
3224 groupHashtable.Add((short)groupHashtable.Count, evData);
3225 }
3226 }
3227 else
3228 {
3229 if (!dataPerGroupUnreliable.ContainsKey(view.group))
3230 {
3231 dataPerGroupUnreliable[view.group] = new Hashtable();
3232 dataPerGroupUnreliable[view.group][(byte)0] = this.ServerTimeInMilliSeconds;
3233 if (currentLevelPrefix >= 0)
3234 {
3235 dataPerGroupUnreliable[view.group][(byte)1] = this.currentLevelPrefix;
3236 }
3237 }
3238 Hashtable groupHashtable = dataPerGroupUnreliable[view.group];
3239 groupHashtable.Add((short)groupHashtable.Count, evData);
3240 }
3241 }
3242 else
3243 {
3244 // Debug.Log(" NO OBS on " + view.name + " " + view.owner);
3245 }
3246 }
3247 else
3248 {
3249 }
3250 }
3251
3252 //Send the messages: every group is send in it's own message and unreliable and reliable are split as well
3253 RaiseEventOptions options = new RaiseEventOptions();
3254
3255#if PHOTON_DEVELOP
3256 options.Receivers = ReceiverGroup.All;
3257#endif
3258
3259 foreach (KeyValuePair
3260 {
3261 options.InterestGroup = (byte)kvp.Key;
3262 this.OpRaiseEvent(PunEvent.SendSerializeReliable, kvp.Value, true, options);
3263 }
3264 foreach (KeyValuePair
3265 {
3266 options.InterestGroup = (byte)kvp.Key;
3267 this.OpRaiseEvent(PunEvent.SendSerialize, kvp.Value, false, options);
3268 }
3269 }
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: PhotonNetwork.cs
Copy
84 {
85 get
86 {
87 if (offlineMode)
88 {
89 return true;
90 }
91
92 if (networkingPeer == null)
93 {
94 return false;
95 }
96
97 return !networkingPeer.IsInitialConnect && networkingPeer.State != PeerState.PeerCreated && networkingPeer.State != PeerState.Disconnected && networkingPeer.State != PeerState.Disconnecting && networkingPeer.State != PeerState.ConnectingToNameServer;
98 }
99 }
File name: PhotonNetwork.cs
Copy
105 {
106 get { return networkingPeer.IsInitialConnect && !offlineMode; }
107 }
File name: PhotonNetwork.cs
Copy
113 {
114 get
115 {
116 // connected property will check offlineMode and networkingPeer being null
117 if (!connected)
118 {
119 return false;
120 }
121
122 if (offlineMode)
123 {
124 return true;
125 }
126
127 switch (connectionStateDetailed)
128 {
129 case PeerState.PeerCreated:
130 case PeerState.Disconnected:
131 case PeerState.Disconnecting:
132 case PeerState.Authenticating:
133 case PeerState.ConnectingToGameserver:
134 case PeerState.ConnectingToMasterserver:
135 case PeerState.ConnectingToNameServer:
136 case PeerState.Joining:
137 case PeerState.Leaving:
138 return false; // we are not ready to execute any operations
139 }
140
141 return true;
142 }
143 }
File name: PhotonNetwork.cs
Copy
149 {
150 get
151 {
152 if (offlineMode)
153 {
154 return ConnectionState.Connected;
155 }
156
157 if (networkingPeer == null)
158 {
159 return ConnectionState.Disconnected;
160 }
161
162 switch (networkingPeer.PeerState)
163 {
164 case PeerStateValue.Disconnected:
165 return ConnectionState.Disconnected;
166 case PeerStateValue.Connecting:
167 return ConnectionState.Connecting;
168 case PeerStateValue.Connected:
169 return ConnectionState.Connected;
170 case PeerStateValue.Disconnecting:
171 return ConnectionState.Disconnecting;
172 case PeerStateValue.InitializingApplication:
173 return ConnectionState.InitializingApplication;
174 }
175
176 return ConnectionState.Disconnected;
177 }
178 }
File name: PhotonNetwork.cs
Copy
187 {
188 get
189 {
190 if (offlineMode)
191 {
192 return (offlineModeRoom != null) ? PeerState.Joined : PeerState.ConnectedToMaster;
193 }
194
195 if (networkingPeer == null)
196 {
197 return PeerState.Disconnected;
198 }
199
200 return networkingPeer.State;
201 }
202 }
OfflineMode 155 lượt xem
Gõ tìm kiếm nhanh...