PlayerPrefs
How do I use Player Prefs
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: NamePickGui.cs
Copy
15 public void Awake()
16 {
17 this.guiCenteredRect = new Rect(Screen.width/2-GuiSize.x/2, Screen.height/2-GuiSize.y/4, GuiSize.x, GuiSize.y);
18 this.chatComponent = this.GetComponent
19
20 if (this.chatComponent != null && chatComponent.enabled)
21 {
22 Debug.LogWarning("When using the NamePickGui, ChatGui should be disabled initially.");
23
24 if (this.chatComponent.chatClient != null)
25 {
26 this.chatComponent.chatClient.Disconnect();
27 }
28 this.chatComponent.enabled = false;
29 }
30
31 string prefsName = PlayerPrefs.GetString(NamePickGui.UserNamePlayerPref);
32 if (!string.IsNullOrEmpty(prefsName))
33 {
34 this.InputLine = prefsName;
35 }
36 }
File name: NamePickGui.cs
Copy
83 private void StartChat()
84 {
85 this.chatComponent.UserName = this.InputLine;
86 this.chatComponent.enabled = true;
87 this.enabled = false;
88
89 PlayerPrefs.SetString(NamePickGui.UserNamePlayerPref, this.InputLine);
90 }
File name: WorkerMenu.cs
Copy
65 public void OnGUI()
66 {
67 if (this.Skin != null)
68 {
69 GUI.skin = this.Skin;
70 }
71
72 if (!PhotonNetwork.connected)
73 {
74 if (PhotonNetwork.connecting)
75 {
76 GUILayout.Label("Connecting to: " + PhotonNetwork.ServerAddress);
77 }
78 else
79 {
80 GUILayout.Label("Not connected. Check console output. Detailed connection state: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.ServerAddress);
81 }
82
83 if (this.connectFailed)
84 {
85 GUILayout.Label("Connection failed. Check setup and use Setup Wizard to fix configuration.");
86 GUILayout.Label(String.Format("Server: {0}", new object[] {PhotonNetwork.ServerAddress}));
87 GUILayout.Label("AppId: " + PhotonNetwork.PhotonServerSettings.AppID);
88
89 if (GUILayout.Button("Try Again", GUILayout.Width(100)))
90 {
91 this.connectFailed = false;
92 PhotonNetwork.ConnectUsingSettings("0.9");
93 }
94 }
95
96 return;
97 }
98
99 Rect content = new Rect((Screen.width - WidthAndHeight.x)/2, (Screen.height - WidthAndHeight.y)/2, WidthAndHeight.x, WidthAndHeight.y);
100 GUI.Box(content,"Join or Create Room");
101 GUILayout.BeginArea(content);
102
103 GUILayout.Space(40);
104
105 // Player name
106 GUILayout.BeginHorizontal();
107 GUILayout.Label("Player name:", GUILayout.Width(150));
108 PhotonNetwork.playerName = GUILayout.TextField(PhotonNetwork.playerName);
109 GUILayout.Space(158);
110 if (GUI.changed)
111 {
112 // Save name
113 PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
114 }
115 GUILayout.EndHorizontal();
116
117 GUILayout.Space(15);
118
119 // Join room by title
120 GUILayout.BeginHorizontal();
121 GUILayout.Label("Roomname:", GUILayout.Width(150));
122 this.roomName = GUILayout.TextField(this.roomName);
123
124 if (GUILayout.Button("Create Room", GUILayout.Width(150)))
125 {
126 PhotonNetwork.CreateRoom(this.roomName, new RoomOptions() { maxPlayers = 10 }, null);
127 }
128
129 GUILayout.EndHorizontal();
130
131 // Create a room (fails if exist!)
132 GUILayout.BeginHorizontal();
133 GUILayout.FlexibleSpace();
134 //this.roomName = GUILayout.TextField(this.roomName);
135 if (GUILayout.Button("Join Room", GUILayout.Width(150)))
136 {
137 PhotonNetwork.JoinRoom(this.roomName);
138 }
139
140 GUILayout.EndHorizontal();
141
142
143 if (!string.IsNullOrEmpty(this.ErrorDialog))
144 {
145 GUILayout.Label(this.ErrorDialog);
146
147 if (timeToClearDialog < Time.time)
148 {
149 timeToClearDialog = 0;
150 this.ErrorDialog = "";
151 }
152 }
153
154 GUILayout.Space(15);
155
156 // Join random room
157 GUILayout.BeginHorizontal();
158
159 GUILayout.Label(PhotonNetwork.countOfPlayers + " users are online in " + PhotonNetwork.countOfRooms + " rooms.");
160 GUILayout.FlexibleSpace();
161 if (GUILayout.Button("Join Random", GUILayout.Width(150)))
162 {
163 PhotonNetwork.JoinRandomRoom();
164 }
165
166
167 GUILayout.EndHorizontal();
168
169 GUILayout.Space(15);
170 if (PhotonNetwork.GetRoomList().Length == 0)
171 {
172 GUILayout.Label("Currently no games are available.");
173 GUILayout.Label("Rooms will be listed here, when they become available.");
174 }
175 else
176 {
177 GUILayout.Label(PhotonNetwork.GetRoomList().Length + " rooms available:");
178
179 // Room listing: simply call GetRoomList: no need to fetch/poll whatever!
180 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
181 foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList())
182 {
183 GUILayout.BeginHorizontal();
184 GUILayout.Label(roomInfo.name + " " + roomInfo.playerCount + "/" + roomInfo.maxPlayers);
185 if (GUILayout.Button("Join", GUILayout.Width(150)))
186 {
187 PhotonNetwork.JoinRoom(roomInfo.name);
188 }
189
190 GUILayout.EndHorizontal();
191 }
192
193 GUILayout.EndScrollView();
194 }
195
196 GUILayout.EndArea();
197 }
File name: PhotonHandler.cs
Copy
199 {
200 get
201 {
202 string prefsRegionCode = PlayerPrefs.GetString(PlayerPrefsKey, "");
203 if (!string.IsNullOrEmpty(prefsRegionCode))
204 {
205 CloudRegionCode loadedRegion = Region.Parse(prefsRegionCode);
206 return loadedRegion;
207 }
208
209 return CloudRegionCode.none;
210 }
211 set
212 {
213 if (value == CloudRegionCode.none)
214 {
215 PlayerPrefs.DeleteKey(PlayerPrefsKey);
216 }
217 else
218 {
219 PlayerPrefs.SetString(PlayerPrefsKey, value.ToString());
220 }
221 }
222 }
File name: PhotonNetwork.cs
Copy
1213 /// The ping result can be overridden via PhotonNetwork.OverrideBestCloudServer(..)
1221 /// - Invalid AppId (calls: OnFailedToConnectToPhoton(). check exact AppId value)
1222 /// - Network issues (calls: OnFailedToConnectToPhoton())
1223 /// - Invalid region (calls: OnConnectionFail() with DisconnectCause.InvalidRegion)
1224 /// - Subscription CCU limit reached (calls: OnConnectionFail() with DisconnectCause.MaxCcuReached. also calls: OnPhotonMaxCccuReached())
1231 public static bool ConnectToBestCloudServer(string gameVersion)
1232 {
1233 if (PhotonServerSettings == null)
1234 {
1235 Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: " + PhotonNetwork.serverSettingsAssetFile);
1236 return false;
1237 }
1238
1239 if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
1240 {
1241 return PhotonNetwork.ConnectUsingSettings(gameVersion);
1242 }
1243
1244 networkingPeer.IsInitialConnect = true;
1245 networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);
1246
1247 CloudRegionCode bestFromPrefs = PhotonHandler.BestRegionCodeInPreferences;
1248 if (bestFromPrefs != CloudRegionCode.none)
1249 {
1250 Debug.Log("Best region found in PlayerPrefs. Connecting to: " + bestFromPrefs);
1251 return networkingPeer.ConnectToRegionMaster(bestFromPrefs);
1252 }
1253
1254 bool couldConnect = PhotonNetwork.networkingPeer.ConnectToNameServer();
1255 return couldConnect;
1256 }
File name: NetworkAdapter.cs
Copy
26 {
27 get
28 {
29 if (PhotonNetwork.connected)
30 {
31 return PhotonNetwork.playerName;
32 }
33 return PlayerPrefs.GetString("playerName", "Player" + Random.Range(1, 9999));
34 }
35 set
36 {
37 PhotonNetwork.playerName = value;
38 PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
39 }
40 }
File name: SwitchAngle.cs
Copy
62 void Start () {
63 int cameraView = PlayerPrefs.GetInt(GameManager.CAMERA_VIEW,0);
64 angleType = (cameraView == 1) ? AngleType._3D : AngleType._2D;
65 rotateCamera = GetComponent
66 LoadAngleType();
67 }
File name: GameManager.cs
Copy
136 void SetTimers() {
137 float timer = PlayerPrefs.GetInt(PLAYER_TIMER, DEFAULT_PLAYER_TIMER_MIN); //get in minutes
138 timer *= 60f; //convert to seconds
139 whiteTimer = blackTimer = timer;
140 UpdateWhiteTimer();
141 UpdateBlackTimer();
142 }
File name: GameManager.cs
Copy
311 public void AddGame() {
312 int newGame = PlayerPrefs.GetInt(GAME_CURRENT,0) + 1;
313 PlayerPrefs.SetInt(GAME_CURRENT,newGame);
314
315 int maxGame = PlayerPrefs.GetInt(GAME_MAX, 0);
316 if (newGame >= maxGame) {
317 End();
318 }
319 }
File name: GameManager.cs
Copy
321 public void AddScore(string playerString) {
322 int newScore = PlayerPrefs.GetInt(playerString,0) + 1;
323 PlayerPrefs.SetInt(playerString,newScore);
324
325 int maxScore = PlayerPrefs.GetInt(SCORE_MAX,0);
326 if (newScore >= maxScore) {
327 End();
328 }
329 }
PlayerPrefs 134 lượt xem
Gõ tìm kiếm nhanh...