Save
How do I use Save
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: 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: 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: PhotonEditor.cs
Copy
649 protected virtual void OnGuiSetupCloudAppId()
650 {
651 GUILayout.Label(CurrentLang.AppIdLabel);
652
653 GUILayout.BeginHorizontal();
654 this.cloudAppId = EditorGUILayout.TextField(this.cloudAppId);
655
656 open = GUILayout.Toggle(open, PhotonGUI.HelpIcon, GUIStyle.none, GUILayout.ExpandWidth(false));
657
658 GUILayout.EndHorizontal();
659
660 if (open) GUILayout.Label(CurrentLang.AppIdInfoLabel);
661
662
663
664 EditorGUILayout.Separator();
665
666 GUILayout.Label(CurrentLang.CloudRegionLabel);
667
668 GUILayout.BeginHorizontal();
669 int toolbarValue = GUILayout.Toolbar((int)selectedRegion, CloudServerRegionNames); // the enum CloudRegionCode is converted into a string[] in init (toolbar can't use enum)
670 helpRegion = GUILayout.Toggle( helpRegion, PhotonGUI.HelpIcon, GUIStyle.none, GUILayout.ExpandWidth( false ) );
671 GUILayout.EndHorizontal();
672
673
674 if (helpRegion) GUILayout.Label(CurrentLang.RegionalServersInfo);
675 PhotonEditor.selectedRegion = (CloudRegionCode)toolbarValue;
676
677 EditorGUILayout.Separator();
678
679 GUILayout.BeginHorizontal();
680 if (GUILayout.Button(CurrentLang.CancelButton))
681 {
682 GUIUtility.keyboardControl = 0;
683 this.ReApplySettingsToWindow();
684 }
685
686
687
688 if (GUILayout.Button(CurrentLang.SaveButton))
689 {
690 GUIUtility.keyboardControl = 0;
691 this.cloudAppId = this.cloudAppId.Trim();
692 PhotonEditor.Current.UseCloud(this.cloudAppId);
693
694 PhotonEditor.Current.PreferredRegion = PhotonEditor.selectedRegion;
695 PhotonEditor.Current.HostType = (PhotonEditor.Current.PreferredRegion == CloudRegionCode.none)
696 ? ServerSettings.HostingOption.BestRegion
697 : ServerSettings.HostingOption.PhotonCloud;
698 PhotonEditor.Save();
699
700 Inspect();
701 EditorUtility.DisplayDialog(CurrentLang.SettingsSavedTitle, CurrentLang.SettingsSavedMessage, CurrentLang.OkButton);
702 }
703
704 GUILayout.EndHorizontal();
705
706
707
708 GUILayout.Space(20);
709
710 GUILayout.Label(CurrentLang.SetupOwnServerLabel);
711
712 if (GUILayout.Button(CurrentLang.SelfHostSettingsButton))
713 {
714 //this.photonAddress = ServerSettings.DefaultServerAddress;
715 //this.photonPort = ServerSettings.DefaultMasterPort;
716 this.photonSetupState = PhotonSetupStates.SetupSelfHosted;
717 }
718
719 EditorGUILayout.Separator();
720 GUILayout.Label(CurrentLang.OwnHostCloudCompareLabel);
721 if (GUILayout.Button(CurrentLang.ComparisonPageButton))
722 {
723 Application.OpenURL(UrlCompare);
724 }
725 }
File name: PhotonEditor.cs
Copy
727 protected virtual void OnGuiSetupSelfhosting()
728 {
729 GUILayout.Label(CurrentLang.YourPhotonServerLabel);
730
731 this.photonAddress = EditorGUILayout.TextField(CurrentLang.AddressIPLabel, this.photonAddress);
732 this.photonPort = EditorGUILayout.IntField(CurrentLang.PortLabel, this.photonPort);
733 this.photonProtocol = (ConnectionProtocol)EditorGUILayout.EnumPopup("Protocol", this.photonProtocol);
734 EditorGUILayout.Separator();
735
736 GUILayout.BeginHorizontal();
737 if (GUILayout.Button(CurrentLang.CancelButton))
738 {
739 GUIUtility.keyboardControl = 0;
740 this.ReApplySettingsToWindow();
741 }
742
743 if (GUILayout.Button(CurrentLang.SaveButton))
744 {
745 GUIUtility.keyboardControl = 0;
746
747 PhotonEditor.Current.UseMyServer(this.photonAddress, this.photonPort, null);
748 PhotonEditor.Current.Protocol = this.photonProtocol;
749 PhotonEditor.Save();
750
751 Inspect();
752 EditorUtility.DisplayDialog(CurrentLang.SettingsSavedTitle, CurrentLang.SettingsSavedMessage, CurrentLang.OkButton);
753 }
754
755 GUILayout.EndHorizontal();
756
757
758 GUILayout.Space(20);
759
760
761 // license
762 GUILayout.BeginHorizontal();
763 GUILayout.Label(CurrentLang.LicensesLabel, EditorStyles.boldLabel, GUILayout.Width(100));
764
765 if (GUILayout.Button(new GUIContent(CurrentLang.LicenseDownloadText, CurrentLang.LicenseDownloadTooltip)))
766 {
767 EditorUtility.OpenWithDefaultApp(UrlFreeLicense);
768 }
769
770 GUILayout.EndHorizontal();
771
772
773 GUILayout.Space(20);
774
775
776 GUILayout.Label(CurrentLang.TryPhotonAppLabel);
777
778 if (GUILayout.Button(CurrentLang.GetCloudAppButton))
779 {
780 this.cloudAppId = string.Empty;
781 this.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
782 }
783
784 EditorGUILayout.Separator();
785 GUILayout.Label(CurrentLang.OwnHostCloudCompareLabel);
786 if (GUILayout.Button(CurrentLang.ComparisonPageButton))
787 {
788 Application.OpenURL(UrlCompare);
789 }
790 }
File name: PhotonEditor.cs
Copy
792 protected virtual void RegisterWithEmail(string email)
793 {
794 EditorUtility.DisplayProgressBar(CurrentLang.ConnectionTitle, CurrentLang.ConnectionInfo, 0.5f);
795 var client = new AccountService();
796 client.RegisterByEmail(email, RegisterOrigin); // this is the synchronous variant using the static RegisterOrigin. "result" is in the client
797
798 EditorUtility.ClearProgressBar();
799 if (client.ReturnCode == 0)
800 {
801 PhotonEditor.Current.UseCloud(client.AppId, 0);
802 PhotonEditor.Save();
803 this.ReApplySettingsToWindow();
804 this.photonSetupState = PhotonSetupStates.SetupPhotonCloud;
805 }
806 else
807 {
808 if (client.Message.Contains(CurrentLang.EmailInUseLabel))
809 {
810 this.photonSetupState = PhotonSetupStates.EmailAlreadyRegistered;
811 }
812 else
813 {
814 EditorUtility.DisplayDialog(CurrentLang.ErrorTextTitle, client.Message, CurrentLang.OkButton);
815 // Debug.Log(client.Exception);
816 this.photonSetupState = PhotonSetupStates.RegisterForPhotonCloud;
817 }
818 }
819 }
File name: PhotonEditor.cs
Copy
881 public static void Save()
882 {
883 EditorUtility.SetDirty(PhotonEditor.Current);
884 }
File name: PhotonViewHandler.cs
Copy
173 //TODO: check if this can be internal protected (as source in editor AND as dll)
174 public static void LoadAllScenesToFix()
175 {
176 string[] scenes = System.IO.Directory.GetFiles(".", "*.unity", SearchOption.AllDirectories);
177
178 foreach (string scene in scenes)
179 {
180 EditorApplication.OpenScene(scene);
181
182 PhotonViewHandler.HierarchyChange();//TODO: most likely on load also triggers a hierarchy change
183
184 EditorApplication.SaveScene();
185 }
186
187 Debug.Log("Corrected scene views where needed.");
188 }
File name: ReorderableListResources.cs
Copy
147 public static Texture2D CreatePixelTexture( string name, Color color )
148 {
149 var tex = new Texture2D( 1, 1, TextureFormat.ARGB32, false, true );
150 tex.name = name;
151 tex.hideFlags = HideFlags.HideAndDontSave;
152 tex.filterMode = FilterMode.Point;
153 tex.SetPixel( 0, 0, color );
154 tex.Apply();
155 return tex;
156 }
File name: ReorderableListResources.cs
Copy
168 private static void LoadResourceAssets()
169 {
170 var skin = EditorGUIUtility.isProSkin ? s_DarkSkin : s_LightSkin;
171 s_Cached = new Texture2D[ skin.Length ];
172
173 for( int i = 0; i < s_Cached.Length; ++i )
174 {
175 // Get image data (PNG) from base64 encoded strings.
176 byte[] imageData = Convert.FromBase64String( skin[ i ] );
177
178 // Gather image size from image data.
179 int texWidth, texHeight;
180 GetImageSize( imageData, out texWidth, out texHeight );
181
182 // Generate texture asset.
183 var tex = new Texture2D( texWidth, texHeight, TextureFormat.ARGB32, false, true );
184 tex.hideFlags = HideFlags.HideAndDontSave;
185 tex.name = "(Generated) ReorderableList:" + i;
186 tex.filterMode = FilterMode.Point;
187 tex.LoadImage( imageData );
188
189 s_Cached[ i ] = tex;
190 }
191
192 s_LightSkin = null;
193 s_DarkSkin = null;
194 }
Download file with original file name:Save
Save 127 lượt xem
Gõ tìm kiếm nhanh...