Web
How do I use Web
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: GUICustomAuth.cs
Copy
81 void OnGUI()
82 {
83 if (PhotonNetwork.connected)
84 {
85 GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
86 return;
87 }
88
89
90 GUILayout.BeginArea(GuiRect);
91 switch (guiState)
92 {
93 case GuiState.AuthFailed:
94 GUILayout.Label("Authentication Failed");
95
96 GUILayout.Space(10);
97
98 GUILayout.Label("Error message:\n'" + this.authDebugMessage + "'");
99
100 GUILayout.Space(10);
101
102 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
103 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
104 GUILayout.Label("The error message comes from that service and can be customized.");
105
106 GUILayout.Space(10);
107
108 GUILayout.BeginHorizontal();
109 if (GUILayout.Button("Back"))
110 {
111 SetStateAuthInput();
112 }
113 if (GUILayout.Button("Help"))
114 {
115 SetStateAuthHelp();
116 }
117 GUILayout.EndHorizontal();
118 break;
119
120 case GuiState.AuthHelp:
121
122 GUILayout.Label("By default, any player can connect to Photon.\n'Custom Authentication' can be enabled to reject players without valid user-account.");
123
124 GUILayout.Label("The actual authentication must be done by a web-service which you host and customize. Example sourcecode for these services is available on the docs page.");
125
126 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
127 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
128
129 GUILayout.Space(10);
130 if (GUILayout.Button("Configure Authentication (Dashboard)"))
131 {
132 Application.OpenURL("https://cloud.exitgames.com/dashboard");
133 }
134 if (GUILayout.Button("Authentication Docs"))
135 {
136 Application.OpenURL("https://doc.exitgames.com/en/pun/current/tutorials/pun-and-facebook-custom-authentication");
137 }
138
139
140 GUILayout.Space(10);
141 if (GUILayout.Button("Back to input"))
142 {
143 SetStateAuthInput();
144 }
145 break;
146
147 case GuiState.AuthInput:
148
149 GUILayout.Label("Authenticate yourself");
150
151 GUILayout.BeginHorizontal();
152 this.authName = GUILayout.TextField(this.authName, GUILayout.Width(Screen.width/4 - 5));
153 GUILayout.FlexibleSpace();
154 this.authToken = GUILayout.TextField(this.authToken, GUILayout.Width(Screen.width/4 - 5));
155 GUILayout.EndHorizontal();
156
157
158 if (GUILayout.Button("Authenticate"))
159 {
160 PhotonNetwork.AuthValues = new AuthenticationValues();
161 PhotonNetwork.AuthValues.SetAuthParameters(this.authName, this.authToken);
162 PhotonNetwork.ConnectUsingSettings("1.0");
163 }
164
165 GUILayout.Space(10);
166
167 if (GUILayout.Button("Help", GUILayout.Width(100)))
168 {
169 SetStateAuthHelp();
170 }
171
172 break;
173 }
174
175 GUILayout.EndArea();
176 }
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
File name: AccountService.cs
Copy
37 public enum Origin : byte { ServerWeb = 1, CloudWeb = 2, Pun = 3, Playmaker = 4 };
File name: AccountService.cs
Copy
42 public AccountService()
43 {
44 WebRequest.DefaultWebProxy = null;
45 ServicePointManager.ServerCertificateValidationCallback = Validator;
46 }
File name: AccountService.cs
Copy
59 public void RegisterByEmail(string email, Origin origin)
60 {
61 this.registrationCallback = null;
62 this.AppId = string.Empty;
63 this.Message = string.Empty;
64 this.ReturnCode = -1;
65
66 string result;
67 try
68 {
69 WebRequest req = HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
70 HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
71
72 // now read result
73 StreamReader reader = new StreamReader(resp.GetResponseStream());
74 result = reader.ReadToEnd();
75 }
76 catch (Exception ex)
77 {
78 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
79 this.Exception = ex;
80 return;
81 }
82
83 this.ParseResult(result);
84 }
File name: AccountService.cs
Copy
93 public void RegisterByEmailAsync(string email, Origin origin, Action94 {
95 this.registrationCallback = callback;
96 this.AppId = string.Empty;
97 this.Message = string.Empty;
98 this.ReturnCode = -1;
99
100 try
101 {
102 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
103 req.Timeout = 5000;
104 req.BeginGetResponse(this.OnRegisterByEmailCompleted, req);
105 }
106 catch (Exception ex)
107 {
108 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
109 this.Exception = ex;
110 if (this.registrationCallback != null)
111 {
112 this.registrationCallback(this);
113 }
114 }
115 }
File name: AccountService.cs
Copy
121 private void OnRegisterByEmailCompleted(IAsyncResult ar)
122 {
123 try
124 {
125 HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
126 HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
127
128 if (response != null && response.StatusCode == HttpStatusCode.OK)
129 {
130 // no error. use the result
131 StreamReader reader = new StreamReader(response.GetResponseStream());
132 string result = reader.ReadToEnd();
133
134 this.ParseResult(result);
135 }
136 else
137 {
138 // a response but some error on server. show message
139 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
140 }
141 }
142 catch (Exception ex)
143 {
144 // not even a response. show message
145 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
146 this.Exception = ex;
147 }
148
149 if (this.registrationCallback != null)
150 {
151 this.registrationCallback(this);
152 }
153 }
File name: AccountService.cs
Copy
173 private void ParseResult(string result)
174 {
175 if (string.IsNullOrEmpty(result))
176 {
177 this.Message = "Server's response was empty. Please register through account website during this service interruption.";
178 return;
179 }
180
181 Dictionary
182 if (values == null)
183 {
184 this.Message = "Service temporarily unavailable. Please register through account website.";
185 return;
186 }
187
188 int returnCodeInt = -1;
189 string returnCodeString = string.Empty;
190 string message;
191
192 values.TryGetValue("ReturnCode", out returnCodeString);
193 values.TryGetValue("Message", out message);
194 int.TryParse(returnCodeString, out returnCodeInt);
195
196 this.ReturnCode = returnCodeInt;
197 if (returnCodeInt == 0)
198 {
199 // returnCode == 0 means: all ok. message is new AppId
200 this.AppId = message;
201 }
202 else
203 {
204 // any error gives returnCode != 0
205 this.AppId = string.Empty;
206 this.Message = message;
207 }
208 }
File name: PhotonEditor.cs
Copy
420 protected virtual void OnGuiRegisterCloudApp()
421 {
422 GUI.skin.label.wordWrap = true;
423 if (!this.isSetupWizard)
424 {
425 GUILayout.BeginHorizontal();
426 GUILayout.FlexibleSpace();
427 if (GUILayout.Button(CurrentLang.MainMenuButton, GUILayout.ExpandWidth(false)))
428 {
429 this.SwitchMenuState(GUIState.Main);
430 }
431
432 GUILayout.EndHorizontal();
433
434 GUILayout.Space(15);
435 }
436
437 if (this.photonSetupState == PhotonSetupStates.RegisterForPhotonCloud)
438 {
439 GUI.skin.label.fontStyle = FontStyle.Bold;
440 GUILayout.Label(CurrentLang.ConnectButton);
441 EditorGUILayout.Separator();
442 GUI.skin.label.fontStyle = FontStyle.Normal;
443
444 GUILayout.Label(CurrentLang.UsePhotonLabel);
445 EditorGUILayout.Separator();
446 this.emailAddress = EditorGUILayout.TextField(CurrentLang.EmailLabel, this.emailAddress);
447
448 if (GUILayout.Button(CurrentLang.SendButton))
449 {
450 GUIUtility.keyboardControl = 0;
451 this.RegisterWithEmail(this.emailAddress);
452 }
453
454 GUILayout.Space(20);
455
456
457 GUILayout.Label(CurrentLang.SignedUpAlreadyLabel);
458 if (GUILayout.Button(CurrentLang.SetupButton))
459 {
460 this.photonSetupState = PhotonSetupStates.SetupPhotonCloud;
461 }
462 EditorGUILayout.Separator();
463
464
465 GUILayout.Label(CurrentLang.RegisterByWebsiteLabel);
466 if (GUILayout.Button(CurrentLang.AccountWebsiteButton))
467 {
468 EditorUtility.OpenWithDefaultApp(UrlAccountPage + Uri.EscapeUriString(this.emailAddress));
469 }
470
471 EditorGUILayout.Separator();
472
473 GUILayout.Label(CurrentLang.SelfHostLabel);
474
475 if (GUILayout.Button(CurrentLang.SelfHostSettingsButton))
476 {
477 this.photonSetupState = PhotonSetupStates.SetupSelfHosted;
478 }
479
480 GUILayout.FlexibleSpace();
481
482
483 if (!InternalEditorUtility.HasAdvancedLicenseOnBuildTarget(BuildTarget.Android) || !InternalEditorUtility.HasAdvancedLicenseOnBuildTarget(BuildTarget.iOS))
484 {
485 GUILayout.Label(CurrentLang.MobileExportNoteLabel);
486 }
487 EditorGUILayout.Separator();
488 }
489 else if (this.photonSetupState == PhotonSetupStates.EmailAlreadyRegistered)
490 {
491 GUI.skin.label.fontStyle = FontStyle.Bold;
492 GUILayout.Label(CurrentLang.OopsLabel);
493 GUI.skin.label.fontStyle = FontStyle.Normal;
494
495 GUILayout.Label(CurrentLang.EmailInUseLabel);
496
497 if (GUILayout.Button(CurrentLang.SeeMyAccountPageButton))
498 {
499 EditorUtility.OpenWithDefaultApp(UrlCloudDashboard + Uri.EscapeUriString(this.emailAddress));
500 }
501
502 EditorGUILayout.Separator();
503
504 GUILayout.Label(CurrentLang.KnownAppIdLabel);
505 GUILayout.BeginHorizontal();
506 if (GUILayout.Button(CurrentLang.CancelButton))
507 {
508 this.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
509 }
510
511 if (GUILayout.Button(CurrentLang.SetupButton))
512 {
513 this.photonSetupState = PhotonSetupStates.SetupPhotonCloud;
514 }
515
516 GUILayout.EndHorizontal();
517 }
518 else if (this.photonSetupState == PhotonSetupStates.SetupPhotonCloud)
519 {
520 // cloud setup
521 GUI.skin.label.fontStyle = FontStyle.Bold;
522 GUILayout.Label(CurrentLang.PhotonCloudConnect);
523 GUI.skin.label.fontStyle = FontStyle.Normal;
524
525 EditorGUILayout.Separator();
526 this.OnGuiSetupCloudAppId();
527 this.OnGuiCompareAndHelpOptions();
528 }
529 else if (this.photonSetupState == PhotonSetupStates.SetupSelfHosted)
530 {
531 // self-hosting setup
532 GUI.skin.label.fontStyle = FontStyle.Bold;
533 GUILayout.Label(CurrentLang.SetupOwnHostLabel);
534 GUI.skin.label.fontStyle = FontStyle.Normal;
535
536 EditorGUILayout.Separator();
537
538 this.OnGuiSetupSelfhosting();
539 this.OnGuiCompareAndHelpOptions();
540 }
541 }
File name: LoadbalancingPeer.cs
Copy
463 public virtual bool OpRaiseEvent(byte eventCode, object customEventContent, bool sendReliable, RaiseEventOptions raiseEventOptions)
464 {
465 opParameters.Clear(); // re-used private variable to avoid many new Dictionary() calls (garbage collection)
466 opParameters[(byte)LiteOpKey.Code] = (byte)eventCode;
467 if (customEventContent != null)
468 {
469 opParameters[(byte) LiteOpKey.Data] = customEventContent;
470 }
471
472 if (raiseEventOptions == null)
473 {
474 raiseEventOptions = RaiseEventOptions.Default;
475 }
476 else
477 {
478 if (raiseEventOptions.CachingOption != EventCaching.DoNotCache)
479 {
480 opParameters[(byte) LiteOpKey.Cache] = (byte) raiseEventOptions.CachingOption;
481 }
482 if (raiseEventOptions.Receivers != ReceiverGroup.Others)
483 {
484 opParameters[(byte) LiteOpKey.ReceiverGroup] = (byte) raiseEventOptions.Receivers;
485 }
486 if (raiseEventOptions.InterestGroup != 0)
487 {
488 opParameters[(byte) LiteOpKey.Group] = (byte) raiseEventOptions.InterestGroup;
489 }
490 if (raiseEventOptions.TargetActors != null)
491 {
492 opParameters[(byte) LiteOpKey.ActorList] = raiseEventOptions.TargetActors;
493 }
494 if (raiseEventOptions.ForwardToWebhook)
495 {
496 opParameters[(byte) ParameterCode.EventForward] = true; //TURNBASED
497 }
498 }
499
500 return this.OpCustom((byte)LiteOpCode.RaiseEvent, opParameters, sendReliable, raiseEventOptions.SequenceChannel, raiseEventOptions.Encrypt);
501 }
Download file with original file name:Web
Web 210 lượt xem
Gõ tìm kiếm nhanh...