- Home /
Get playerPrefs of connections?
I am trying to make a networking game and i am making a lobby right now. I am trying to make a list of all the connected players names. I saved their names in the playerPrefs and now i am trying to get their names to print. Why cant i do this? :
for (int x=0; x<Network.connections.length; x++)
{
GUI.Button(new Rect(25, 50 + 40 * x, rightWindowRect.width - 50, 30), Network.connections[x].PlayerPrefs.GetString("PlayerName"));
}
How can i get the playerPrefs for all the connections?
Answer by iwaldrop · Dec 29, 2014 at 11:43 PM
PlayerPrefs is not an ideal solution to short-term data storage. I would suggest using a custom model to store your data. What follows is a simple primer on using a custom class for storing and accessing your data. This kind of data is not persistent, but shouldn't be anyway; it's collected from the network environment at runtime.
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkPlayerInfoDemo : MonoBehaviour
{
void Awake()
{
infos = new List<NetworkPlayerInfo>()
{
new NetworkPlayerInfo("Network Player A"),
new NetworkPlayerInfo("Network Player B"),
new NetworkPlayerInfo("Network Player C"),
new NetworkPlayerInfo("Network Player D")
};
}
void OnGUI()
{
foreach (NetworkPlayerInfo info in infos)
GUILayout.Button(info.name);
}
private List<NetworkPlayerInfo> infos;
}
public class NetworkPlayerInfo
{
public readonly string name;
public readonly DateTime connectionStartTime;
public NetworkPlayerInfo(string name)
{
this.name = name;
connectionStartTime = DateTime.Now;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Differences between mobile save/load data 1 Answer
How to save different values with same script? 1 Answer
remove spawned object from my networkView when timeScale = 0 0 Answers