美文网首页
unity常用的工具方法

unity常用的工具方法

作者: 安宇辛 | 来源:发表于2021-03-19 09:57 被阅读0次

1——获取时间相关

  public static int GetTimeStamp()
        {
            long time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
            return (int)time;
        }
  private static DateTime GetDateTime(int timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = ((long)timeStamp * 10000000);
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(toNow);
            return targetDt;
        }
   public static string TimeToDate(int timeStamp)
        {
            DateTime dt = GetDateTime(timeStamp);
            string time = string.Format("{0}", dt.ToString("yyyy.MM.dd hh:mm:ss"));
            return time;
        }

2——秒转年月日

public static string TimeConversion(int second)
        {
            string time;
            int hour = 0;
            int minute = 0;

            if (second > 60)
            {
                minute = second / 60;
                second = second % 60;
            }
            if (minute > 60)
            {
                hour = minute / 60;
                minute = minute % 60;
            }
            time = string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);

            return time;
        }

3——Json保存和加载

public class DataToJson
    {
       //保存json   name是文件名   param是json的结构体
        public static void SaveJson(string name, object param)
        {
            string path = Application.persistentDataPath +string.Format("/{0}.json",name);
            var content = JsonConvert.SerializeObject(param);
            File.WriteAllText(path, content);
        }
       //读取json  MyLife是转出的结构体
        public static MyLife LoadJson(string name)
        {
            string path = Application.persistentDataPath + string.Format("/{0}.json", name);
            if (File.Exists(path))
            {
                var content = File.ReadAllText(path);
                var playerData = JsonConvert.DeserializeObject<MyLife>(content);
                return playerData;
            }
            else
            {
                Debug.LogError("Save file not found in  " + path);
                return null;
            }
        }


    }

4——文本的正则检测

//正则字符串
public static string restrict = @"^[a-zA-Z0-9\s~`.\-_'|!@#\$%\^&\*\(\)\+=\|\\\}\]\{\[:;<,>\?\/""]*$";

  //检测是不是有相应的正则内容  返回true 没有返回false
  public static bool FnIsDigitOrLetter2(string strMessage)
        {
            bool bResult = false;

            string pattern = GameData.restrict;

            if (System.Text.RegularExpressions.Regex.IsMatch(strMessage, pattern))
            {
                  bResult = true;
            }
            else
            {
                  bResult = false;
            }
       

            return bResult;
        }
   //删除没有正则字符的内容
   public static string Change(string str)
        {
            str = str.Trim();//去掉首尾空格
            bool pop = false;
            if (str.Length > 0)
            {
                for (int i = str.Length - 1; i >= 0; i--)
                {

                    string newStr = str[i].ToString();
                    if (!FnIsDigitOrLetter2(newStr))
                    {
                        str = str.Remove(i, 1);
                        pop = true;
                    }
                }
            }
            if (pop)
            {
                //提示有无效字符
                GamePopUpLabel.Show("412", true);

            }
            return str;

        }

5——256加密

        //256加密
        public static string Sha256(string data)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            byte[] hash = SHA256.Create().ComputeHash(bytes);

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("X2"));
            }

            return builder.ToString();

        }

相关文章

  • unity常用的工具方法

    1——获取时间相关 2——秒转年月日 3——Json保存和加载 4——文本的正则检测 5——256加密

  • UniRx日常笔记

    Unity中常用方法 使用观察者模式对Unity UI组件进行扩展 位于UniRx public static c...

  • Unity基础(11)-Input类

    01-Unity下的Input类属性 常用属性 02-Unity下的Input类方法 03-Input虚拟轴的设置...

  • Unity简易事件触发器

    事件触发器作为unity常用的模块解耦工具,其主要功能有三点: 订阅事件 移除事件 事件触发,并传给监听的回调方法...

  • [工具]Unity3D 常用方法封装

    public static GameObjectGet2DTouchObject() { GameObjectto...

  • VRTK案例001~037

    VRTK这个工具包提供了许多unity引擎中VR常用的功能,诸如(但不局限于): 用常用别名封装的控制器按钮事件 ...

  • unity 常用工具

    一:模型下载1.https://www.3d66.com[https://www.3d66.com]2.https...

  • 常用的工具方法

    日期类 附件类 Json类 加解密 Http调用 Xml转换

  • unity & C#相关

    事件和委托 C#事件和Unity3d - CSDN博客 Unity常用的函数和遇到的C#的知识整理 Unity常用...

  • Unity 2019 破解方法

    Unity 2019.1.0 - Unity 2019.2.21 工具名称:UniPatcher2019_v3工具...

网友评论

      本文标题:unity常用的工具方法

      本文链接:https://www.haomeiwen.com/subject/vzxvcltx.html