See
How do I use See
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: 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: PhotonConverter.cs
Copy
18 public static void RunConversion()
19 {
20 //Ask if user has made a backup.
21 int option = EditorUtility.DisplayDialogComplex("Conversion", "Attempt automatic conversion from Unity Networking to Photon Unity Networking \"PUN\"?", "Yes", "No!", "Pick Script Folder");
22 switch (option)
23 {
24 case 0:
25 break;
26 case 1:
27 return;
28 case 2:
29 PickFolderAndConvertScripts();
30 return;
31 default:
32 return;
33 }
34
35 //REAAAALY?
36 bool result = EditorUtility.DisplayDialog("Conversion", "Disclaimer: The code conversion feature is quite crude, but should do it's job well (see the sourcecode). A backup is therefore strongly recommended!", "Yes, I've made a backup: GO", "Abort");
37 if (!result)
38 {
39 return;
40 }
41 Output(EditorApplication.timeSinceStartup + " Started conversion of Unity networking -> Photon");
42
43 //Ask to save current scene (optional)
44 EditorApplication.SaveCurrentSceneIfUserWantsTo();
45
46 EditorUtility.DisplayProgressBar("Converting..", "Starting.", 0);
47
48 //Convert NetworkViews to PhotonViews in Project prefabs
49 //Ask the user if we can move all prefabs to a resources folder
50 bool movePrefabs = EditorUtility.DisplayDialog("Conversion", "Can all prefabs that use a PhotonView be moved to a Resources/ folder? You need this if you use Network.Instantiate.", "Yes", "No");
51
52
53 string[] prefabs = Directory.GetFiles("Assets/", "*.prefab", SearchOption.AllDirectories);
54 foreach (string prefab in prefabs)
55 {
56 EditorUtility.DisplayProgressBar("Converting..", "Object:" + prefab, 0.6f);
57
58 Object[] objs = (Object[])AssetDatabase.LoadAllAssetsAtPath(prefab);
59 int converted = 0;
60 foreach (Object obj in objs)
61 {
62 if (obj != null && obj.GetType() == typeof(GameObject))
63 converted += ConvertNetworkView(((GameObject)obj).GetComponents
64 }
65 if (movePrefabs && converted > 0)
66 {
67 //This prefab needs to be under the root of a Resources folder!
68 string path = prefab.Replace("\\", "/");
69 int lastSlash = path.LastIndexOf("/");
70 int resourcesIndex = path.LastIndexOf("/Resources/");
71 if (resourcesIndex != lastSlash - 10)
72 {
73 if (path.Contains("/Resources/"))
74 {
75 Debug.LogWarning("Warning, prefab [" + prefab + "] was already in a resources folder. But has been placed in the root of another one!");
76 }
77 //This prefab NEEDS to be placed under a resources folder
78 string resourcesFolder = path.Substring(0, lastSlash) + "/Resources/";
79 EnsureFolder(resourcesFolder);
80 string newPath = resourcesFolder + path.Substring(lastSlash + 1);
81 string error = AssetDatabase.MoveAsset(prefab, newPath);
82 if (error != "")
83 Debug.LogError(error);
84 Output("Fixed prefab [" + prefab + "] by moving it into a resources folder.");
85 }
86 }
87 }
88
89 //Convert NetworkViews to PhotonViews in scenes
90 string[] sceneFiles = Directory.GetFiles("Assets/", "*.unity", SearchOption.AllDirectories);
91 foreach (string sceneName in sceneFiles)
92 {
93 EditorApplication.OpenScene(sceneName);
94 EditorUtility.DisplayProgressBar("Converting..", "Scene:" + sceneName, 0.2f);
95
96 int converted2 = ConvertNetworkView((NetworkView[])GameObject.FindObjectsOfType(typeof(NetworkView)), true);
97 if (converted2 > 0)
98 {
99 //This will correct all prefabs: The prefabs have gotten new components, but the correct ID's were lost in this case
100 PhotonViewHandler.HierarchyChange(); //TODO: most likely this is triggered on change or on save
101
102 Output("Replaced " + converted2 + " NetworkViews with PhotonViews in scene: " + sceneName);
103 EditorApplication.SaveScene(EditorApplication.currentScene);
104 }
105
106 }
107
108 //Convert C#/JS scripts (API stuff)
109 List
110
111 EditorUtility.DisplayProgressBar("Converting..", "Scripts..", 0.9f);
112 ConvertScripts(scripts);
113
114 Output(EditorApplication.timeSinceStartup + " Completed conversion!");
115 EditorUtility.ClearProgressBar();
116
117 EditorUtility.DisplayDialog("Completed the conversion", "Don't forget to add \"PhotonNetwork.ConnectWithDefaultSettings();\" to connect to the Photon server before using any multiplayer functionality.", "OK");
118 }
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: PhotonEditor.cs
Copy
630 protected virtual void OnGuiCompareAndHelpOptions()
631 {
632 GUILayout.FlexibleSpace();
633
634 GUILayout.Label(CurrentLang.QuestionsLabel);
635 if (GUILayout.Button(CurrentLang.SeeForumButton))
636 {
637 Application.OpenURL(UrlForum);
638 }
639
640 if (photonSetupState != PhotonSetupStates.SetupSelfHosted)
641 {
642 if (GUILayout.Button(CurrentLang.OpenDashboardButton))
643 {
644 EditorUtility.OpenWithDefaultApp(UrlCloudDashboard + Uri.EscapeUriString(this.emailAddress));
645 }
646 }
647 }
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: LoadbalancingPeer.cs
Copy
29 private readonly Dictionary31 public LoadbalancingPeer(IPhotonPeerListener listener, ConnectionProtocol protocolType) : base(listener, protocolType)
32 {
33 }
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 }
File name: NetworkingPeer.cs
Copy
495 protected internal void LocalCleanupAnythingInstantiated(bool destroyInstantiatedGameObjects)
496 {
497 if (tempInstantiationData.Count > 0)
498 {
499 Debug.LogWarning("It seems some instantiation is not completed, as instantiation data is used. You should make sure instantiations are paused when calling this method. Cleaning now, despite this.");
500 }
501
502 // Destroy GO's (if we should)
503 if (destroyInstantiatedGameObjects)
504 {
505 // Fill list with Instantiated objects
506 HashSet
507 foreach (PhotonView view in this.photonViewList.Values)
508 {
509 if (view.isRuntimeInstantiated)
510 {
511 instantiatedGos.Add(view.gameObject); // HashSet keeps each object only once
512 }
513 }
514
515 foreach (GameObject go in instantiatedGos)
516 {
517 this.RemoveInstantiatedGO(go, true);
518 }
519 }
520
521 // photonViewList is cleared of anything instantiated (so scene items are left inside)
522 // any other lists can be
523 this.tempInstantiationData.Clear(); // should be empty but to be safe we clear (no new list needed)
524 PhotonNetwork.lastUsedViewSubId = 0;
525 PhotonNetwork.lastUsedViewSubIdStatic = 0;
526 }
File name: NetworkingPeer.cs
Copy
757 internal protected bool SetMasterClient(int playerId, bool sync)
758 {
759 bool masterReplaced = this.mMasterClient != null && this.mMasterClient.ID != playerId;
760 if (!masterReplaced || !this.mActors.ContainsKey(playerId))
761 {
762 return false;
763 }
764
765 if (sync)
766 {
767 bool sent = this.OpRaiseEvent(PunEvent.AssignMaster, new Hashtable() { { (byte)1, playerId } }, true, null);
768 if (!sent)
769 {
770 return false;
771 }
772 }
773
774 this.hasSwitchedMC = true;
775 this.mMasterClient = this.mActors[playerId];
776 SendMonoMessage(PhotonNetworkingMessage.OnMasterClientSwitched, this.mMasterClient); // we only callback when an actual change is done
777 return true;
778 }
Download file with original file name:See
See 122 lượt xem
Gõ tìm kiếm nhanh...