木骰

一个Util工具类

平时用到的一个工具类。目前包含对streamingAssets路径和沙盒路径的读取,还有一些单位转换,日期转换,计算等方法。之后会不断增加。


using System;
using UnityEngine;
public class Util
{
    //模拟移动平台
    public static bool simulationMobile = false;
    public static string DataPath
    {
        get
        {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
            if (simulationMobile)//模拟移动平台的沙盒路径
                return Application.dataPath + "/persistentDataPath";
            return Application.streamingAssetsPath;
#elif UNITY_ANDROID
            return Application.persistentDataPath;
#elif UNITY_IPHONE
            return Application.persistentDataPath;
#endif
        }
    }
    public static string StreamingAssetPath
    {
        get
        {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
            return "file://" + Application.streamingAssetsPath;
#elif UNITY_ANDROID
            return "jar:file://" + Application.dataPath + "!/assets";
#elif UNITY_IPHONE
            return "file://"+Application.streamingAssetsPath;
#endif
        }
    }
    public static DateTime CovertIntDateTime(double unixTime)
    {
        DateTime time = DateTime.MinValue;
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
        time = startTime.AddMilliseconds(unixTime);
        return time;
    }
    public static long CovertDateTimeInt(DateTime time)
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
        long unixTime = (time.Ticks - startTime.Ticks) / 10000;
        return unixTime;
    }
    /// 
    /// 计算两个时间戳之间的天数
    /// 
    /// 
    /// 
    /// 
    public static int DateDiff(double dateStart,double dateEnd)
    {
        return DateDiff(CovertIntDateTime(dateStart),CovertIntDateTime(dateEnd));
    }
    /// 
    /// 计算两个日期之间的天数
    /// 
    /// 
    /// 
    /// 
    public static int DateDiff(DateTime dateStart, DateTime dateEnd)
    {
        DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
        Debug.Log(start);
        DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
        Debug.Log(end);
        TimeSpan sp = end.Subtract(start);
        return sp.Days;
    }
    public static string TranslateNumber(double number)
    {
        string str = string.Empty;
        if (number < 99999)
            str = string.Format("{0:0.00}", number);
        else if (number < 99999999)
            str = string.Format("{0:0.00}万", number / 10000);
        else
            str = string.Format("{0:0.00}亿", number / 100000000);
        return str;
    }
}

— 于 共写了1861个字
— 文内使用到的标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*