Unity开发_序列化本地属性

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值