Skip to content

Instantly share code, notes, and snippets.

@jadepark-dev
Last active July 24, 2019 01:32
Show Gist options
  • Save jadepark-dev/85924e09447e46c38a5d30328c425bc7 to your computer and use it in GitHub Desktop.
Save jadepark-dev/85924e09447e46c38a5d30328c425bc7 to your computer and use it in GitHub Desktop.
[{"serverIP":"192.168.1.10","serverPort":"3000"}]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class NetworkSetting{
public string serverIP;
public string serverPort;
public NetworkSetting(string _serverIp, string _serverPort){
serverIP = _serverIp;
serverPort = _serverPort;
}
}
public class SocketSettings : MonoBehaviour
{
public string ipAddress;
public string port;
public List<NetworkSetting> networkSettingList = new List<NetworkSetting>();
private string networkSettingFile = "NetworkSetting.json";
#region Save / Load Settings.
public void LoadFromJson(){
string filePath = Path.Combine(Application.streamingAssetsPath, networkSettingFile);
string networkSettingJsonFile = File.ReadAllText(filePath);
JsonData networkSettingJson = JsonMapper.ToObject(networkSettingJsonFile);
// Assign them with class variables.
ipAddress = networkSettingJson[0]["serverIP"].ToString();
port = networkSettingJson[0]["serverPort"].ToString();
}
public void SaveToJson(){
// Put them in the list, and make it Json data.
networkSettingList.Add(new NetworkSetting(ipAddress,port));
JsonData networkSettingJson = JsonMapper.ToJson(networkSettingList);
networkSettingList.Clear();
// Save Json to File
string filePath = Path.Combine(Application.streamingAssetsPath, networkSettingFile);
File.WriteAllText(filePath,networkSettingJson.ToString());
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment