using System;
using System.IO;
using System.Net;
using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;
public static class ProjectConfig
{
public static readonly string Path = Application.streamingAssetsPath + "/ProjectConfig/";
public static void WriteConfig<T>(T temp) where T : class
{
string filePath = Path + typeof(T).Name;
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
using (StreamWriter streamWriter = File.CreateText(filePath))
{
string json = JsonConvert.SerializeObject(temp);
streamWriter.WriteLine(json);
streamWriter.Close();
}
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
public static T ReadJson<T>() where T : class
{
string filePath = Path + typeof(T).Name;
if (!File.Exists(filePath))
{
return null;
}
using (StreamReader streamReader = File.OpenText(filePath))
{
string json = streamReader.ReadToEnd();
T temp = JsonConvert.DeserializeObject<T>(json);
streamReader.Close();
return temp;
}
}
//序列化
public static byte[] Serialize(object data)
{
using (MemoryStream rems = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(rems, data);
data = null;
return rems.GetBuffer();
}
}
//反序列化
public static object Deserialize(byte[] data)
{
using (MemoryStream rems = new MemoryStream(data))
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter.Deserialize(rems);
}
}
public static string SetUserID()
{
string _userid = GetMacAddress() + SystemInfo.deviceUniqueIdentifier + SystemInfo.deviceName + string.Format("{0:yyyyMMddhhmmss}", DateTime.Now);
return _userid;
}
/// <summary>
/// 获取本机IP地址
/// </summary>
/// <returns></returns>
public static System.Net.IPAddress GetHostIp()
{
string hostName = System.Net.Dns.GetHostName();
UnityEngine.Debug.Log($"Host Name:{hostName}");
System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
foreach (System.Net.IPAddress ip in ipEntry.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
UnityEngine.Debug.Log($"<<InterNetwork IP:{ip.ToString()} >>");
return ip;
}
}
return null;
}
public static string GetLocalIp()
{
try
{
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily == AddressFamily.InterNetwork)
{
return _IPAddress.ToString();
}
}
return "127.0.0.1";
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string GetIP()
{
using (WebClient webClient = new WebClient())
{
try
{
string temp = webClient.DownloadString("https://www.csdn.net/?spm=1001.2101.3001.5359");
string ip = Regex.Match(temp, @"\[(?<ip>\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
return !string.IsNullOrEmpty(ip) ? ip : null;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
public static string GetMacAddress()
{
string physicalAddress = "";
NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adaper in nice)
{
if (adaper.Description == "en0")
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
break;
}
else
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
if (physicalAddress != "")
{
break;
};
}
}
return physicalAddress;
}
private static void AddFireWallPort(int port)
{
string argsDll = String.Format(@"firewall set portopening TCP {0} ENABLE", port);
ProcessStartInfo psi = new ProcessStartInfo("netsh", argsDll);
psi.Verb = "runas";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process.Start(psi).WaitForExit();
}
}
[Serializable]
public class NetInfo
{
public string IpAddress;
public int Import;
}
Unity开发_序列化本地属性
于 2021-06-02 17:48:51 首次发布