美文网首页
util代码

util代码

作者: 此年此景 | 来源:发表于2017-02-06 11:48 被阅读0次
using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.GZip;
using System.Reflection;

public static class Util 
{
    public static string TheWebConfigURL;
    public static int Int(object o) {
        return Convert.ToInt32(o);
    }

    public static float Float(object o) {
        return (float)Math.Round(Convert.ToSingle(o), 2);
    }

    public static long Long(object o) {
        return Convert.ToInt64(o);
    }

    public static int Random(int min, int max) {
        return UnityEngine.Random.Range(min, max);
    }

    public static float Random(float min, float max) {
        return UnityEngine.Random.Range(min, max);
    }

    public static string Uid(string uid) {
        int position = uid.LastIndexOf('_');
        return uid.Remove(0, position + 1);
    }

    public static long GetTime() { 
        TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0).Ticks);
        return (long)ts.TotalMilliseconds;
    }

    /// <summary>
    /// 搜索子物体组件-GameObject版
    /// </summary>
    public static T Get<T>(GameObject go, string subnode) where T : Component {
        if (go != null) {
            Transform sub = go.transform.FindChild(subnode);
            if (sub != null) return sub.GetComponent<T>();
        }
        return null;
    }

    /// <summary>
    /// 搜索子物体组件-Transform版
    /// </summary>
    public static T Get<T>(Transform go, string subnode) where T : Component {
        if (go != null) {
            Transform sub = go.FindChild(subnode);
            if (sub != null) return sub.GetComponent<T>();
        }
        return null;
    }

    /// <summary>
    /// 搜索子物体组件-Component版
    /// </summary>
    public static T Get<T>(Component go, string subnode) where T : Component {
        return go.transform.FindChild(subnode).GetComponent<T>();
    }

    /// <summary>
    /// 添加组件
    /// </summary>
    public static T Add<T>(GameObject go) where T : Component {
        if (go != null) {
            T[] ts = go.GetComponents<T>();
            for (int i = 0; i < ts.Length; i++ ) {
                if (ts[i] != null) GameObject.Destroy(ts[i]);
            }
            return go.gameObject.AddComponent<T>();
        }
        return null;
    }

    /// <summary>
    /// 添加组件
    /// </summary>
    public static T Add<T>(Transform go) where T : Component {
        return Add<T>(go.gameObject);
    }

    /// <summary>
    /// 查找子对象
    /// </summary>
    public static GameObject Child(GameObject go, string subnode) {
        return Child(go.transform, subnode);
    }

    /// <summary>
    /// 查找子对象
    /// </summary>
    public static GameObject Child(Transform go, string subnode) {
        Transform tran = go.FindChild(subnode);
        if (tran == null) return null;
        return tran.gameObject;
    }

    /// <summary>
    /// 取平级对象
    /// </summary>
    public static GameObject Peer(GameObject go, string subnode) {
        return Peer(go.transform, subnode);
    }

    /// <summary>
    /// 取平级对象
    /// </summary>
    public static GameObject Peer(Transform go, string subnode) {
        Transform tran = go.parent.FindChild(subnode);
        if (tran == null) return null;
        return tran.gameObject;
    }

    /// <summary>
    /// 手机震动
    /// </summary>
    public static void Vibrate() {
        //int canVibrate = PlayerPrefs.GetInt(Const.AppPrefix + "Vibrate", 1);
        //if (canVibrate == 1) iPhoneUtils.Vibrate();
    }



    /// <summary>
    /// Base64编码
    /// </summary>
    public static string EncodeBase64(string message) {
        byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(message);
        return Convert.ToBase64String(bytes);
    }

    /// <summary>
    /// Base64解码
    /// </summary>
    public static string DecodeBase64(string message) {
        byte[] bytes = Convert.FromBase64String(message);
        return Encoding.GetEncoding("utf-8").GetString(bytes);
    }

    /// <summary>
    /// 判断数字
    /// </summary>
    public static bool IsNumeric(string str) {
        if (str == null || str.Length == 0) return false;
        for (int i = 0; i < str.Length; i++ ) {
            if (!Char.IsNumber(str[i])) { return false; }
        }
        return true;
    }

    public static string IntToStringWithW(int num)
    {
        if (num >= 10000)
            return (num / 10000).ToString() + "万";
        if(num >= 100000000)
            return (num / 100000000).ToString() + "亿";
        return num.ToString();

    }

    /// <summary>
    /// HashToMD5Hex
    /// </summary>
    public static string HashToMD5Hex(string sourceStr) {
        byte[] Bytes = Encoding.UTF8.GetBytes(sourceStr);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) {
            byte[] result = md5.ComputeHash(Bytes);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
                builder.Append(result[i].ToString("x2"));
            return builder.ToString();
        }
    }

    /// <summary>
    /// 计算字符串的MD5值
    /// </summary>
    public static string md5(string source) {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.UTF8.GetBytes(source);
        byte[] md5Data = md5.ComputeHash(data, 0, data.Length);
        md5.Clear();

        string destString = "";
        for (int i = 0; i < md5Data.Length; i++) {
            destString += System.Convert.ToString(md5Data[i], 16).PadLeft(2, '0');
        }
        destString = destString.PadLeft(32, '0');
        return destString;
    }

        /// <summary>
    /// 计算文件的MD5值
    /// </summary>
    public static string md5file(string file) {
        try {
            FileStream fs = new FileStream(file, FileMode.Open,FileAccess.Read, FileShare.Read);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(fs);
            fs.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++) {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        } catch (Exception ex) {
            throw new Exception("md5file() fail, error:" + ex.Message);
        }
    }

    /// <summary>
    /// 功能:压缩字符串
    /// </summary>
    /// <param name="infile">被压缩的文件路径</param>
    /// <param name="outfile">生成压缩文件的路径</param>
    public static void CompressFile(string infile, string outfile) {
        Stream gs = new GZipOutputStream(File.Create(outfile));
        FileStream fs = File.OpenRead(infile);
        byte[] writeData = new byte[fs.Length];
        fs.Read(writeData, 0, (int)fs.Length);
        gs.Write(writeData, 0, writeData.Length);
        gs.Close(); fs.Close();
    }

    /// <summary>
    /// 功能:输入文件路径,返回解压后的字符串
    /// </summary>
    public static string DecompressFile(string infile) {
        string result = string.Empty;
        Stream gs = new GZipInputStream(File.OpenRead(infile));
        MemoryStream ms = new MemoryStream();
        int size = 2048;
        byte[] writeData = new byte[size]; 
        while (true) {
            size = gs.Read(writeData, 0, size); 
            if (size > 0) {
                ms.Write(writeData, 0, size); 
            } else {
                break; 
            }
        }
        result = new UTF8Encoding().GetString(ms.ToArray());
        gs.Close(); ms.Close();
        return result;
    }

    /// <summary>
    /// 压缩字符串
    /// </summary>
    public static string Compress(string source) {
        byte[] data = Encoding.UTF8.GetBytes(source);
        MemoryStream ms = null;
        using (ms = new MemoryStream()) {
            using (Stream stream = new GZipOutputStream(ms)) {
                try {
                    stream.Write(data, 0, data.Length);
                } finally {
                    stream.Close();
                    ms.Close();
                }
            }
        }
        return Convert.ToBase64String(ms.ToArray());
    }

    /// <summary>
    /// 解压字符串
    /// </summary>
    public static string Decompress(string source) {
        string result = string.Empty;
        byte[] buffer = null;
        try {
            buffer = Convert.FromBase64String(source);
        } catch {
            Debug.LogError("Decompress---->>>>" + source);
        }
        using (MemoryStream ms = new MemoryStream(buffer)) {
            using (Stream sm = new GZipInputStream(ms)) {
                StreamReader reader = new StreamReader(sm, Encoding.UTF8);
                try {
                    result = reader.ReadToEnd();
                } finally {
                    sm.Close();
                    ms.Close();
                }
            }
        }
        return result;
    }

    /// <summary>
    /// 清除所有子节点
    /// </summary>
    public static void ClearChild(Transform go) {
        if (go == null) return;
        for (int i = go.childCount - 1; i >= 0; i--) {
            GameObject.Destroy(go.GetChild(i).gameObject);
        }
    }

    /// <summary>
    /// 生成一个Key名
    /// </summary>
    public static string GetKey(string key) {
        return Const.AppPrefix + Const.UserId + "_" + key; 
    }

    /// <summary>
    /// 取得整型
    /// </summary>
    public static int GetInt(string key) {
        string name = GetKey(key);
        return PlayerPrefs.GetInt(name);
    }

    /// <summary>
    /// 有没有值
    /// </summary>
    public static bool HasKey(string key) {
        string name = GetKey(key);
        return PlayerPrefs.HasKey(name);
    }

    /// <summary>
    /// 保存整型
    /// </summary>
    public static void SetInt(string key, int value) {
        string name = GetKey(key);
        PlayerPrefs.DeleteKey(name);
        PlayerPrefs.SetInt(name, value);
    }

    /// <summary>
    /// 取得数据
    /// </summary>
    public static string GetString(string key) {
        string name = GetKey(key);
        return PlayerPrefs.GetString(name);
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    public static void SetString(string key, string value) {
        string name = GetKey(key);
        PlayerPrefs.DeleteKey(name);
        PlayerPrefs.SetString(name, value);
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    public static void RemoveData(string key) {
        string name = GetKey(key);
        PlayerPrefs.DeleteKey(name);
    }

    /// <summary>
    /// 清理内存
    /// </summary>
    public static void ClearMemory() {
        //GC.Collect(); 
        Resources.UnloadUnusedAssets();
    }

    /// <summary>
    /// 是否为数字
    /// </summary>
    public static bool IsNumber(string strNumber) {
        Regex regex = new Regex("[^0-9]");
        return !regex.IsMatch(strNumber);
    }

    /// <summary>
    /// 取得数据存放目录
    /// </summary>
    public static string DataPath {
        get {

            
            //string game = Const.AppName.ToLower();
            //if (Application.platform == RuntimePlatform.IPhonePlayer || 
            //    Application.platform == RuntimePlatform.Android ||
            //    Application.platform == RuntimePlatform.WP8Player) {
            //    return Application.persistentDataPath + "/" + game + "/";
            //}
            //if (Application.platform == RuntimePlatform.WindowsWebPlayer) {
            //    return "web/";
            //}
            //#if UNITY_WEBPLAYER
            //if(!String.IsNullOrEmpty(TheWebConfigURL))
            //{
            //    return  TheWebConfigURL + "/StreamingAssets/WebPlayer/";
            //}
            //return  "file://" + Application.dataPath + "/StreamingAssets/WebPlayer/";
            //#endif
            //if (Const.DebugMode) {
            //    string target = string.Empty;
            //    if (Application.platform == RuntimePlatform.OSXEditor ||
            //        Application.platform == RuntimePlatform.IPhonePlayer ||
            //        Application.platform == RuntimePlatform.OSXEditor) {
            //        target = "ios";
            //        }
            //    else if (Application.platform == RuntimePlatform.Android)
            //    {
            //        target = "android";
            //    }
            //    else { 
                    
            //    }
            //    return Application.dataPath + "/StreamingAssets/" + target + "/";
            //}
            //return Application.dataPath + "/persistentDataPath/";
            return Util.AppContentPath();
        }
    }

#if !UNITY_WEBPLAYER
    /// <summary>
    /// 取得行文本
    /// </summary>
    public static string GetFileText(string path) {
        return File.ReadAllText(path);
        //return "";
    }
#endif
    /// <summary>
    /// 网络可用
    /// </summary>
    public static bool NetAvailable {
        get {
            return Application.internetReachability != NetworkReachability.NotReachable;
        }
    }

    /// <summary>
    /// 是否是无线
    /// </summary>
    public static bool IsWifi {
        get {
            return Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork;
        }
    }

    /// <summary>
    /// 应用程序内容路径
    /// </summary>
    public static string AppContentPath() {

        return GetAppContentPath(Application.platform);
    }

    public static string GetAppContentPath(RuntimePlatform platform) {

        string path = string.Empty;


#if UNITY_WEBPLAYER && UNITY_EDITOR
    path = "file://" + Application.dataPath + "/streamingassets/webplayer/"; 
#endif

        
#if UNITY_WEBPLAYER && !UNITY_EDITOR
path = Application.dataPath + "/streamingassets/webplayer/"; 
#endif


#if (UNITY_STANDALONE || UNITY_STANDALONE_WIN) && !UNITY_EDITOR 
//path = "file://" + Application.dataPath + "/streamingassets/windows32/";
        path = PathSetting.resourcePath;//"http://cdn1.app99.wsdo.xnhdgame.com/dance/streamingassets/windows32/windows32/";
#endif

#if (UNITY_STANDALONE || UNITY_STANDALONE_WIN) && UNITY_EDITOR 
        path = "file://" + Application.dataPath + "/streamingassets/windows32/";
#endif

/*
#if UNITY_WEBPLAYER
#if UNITY_EDITOR
    
        if(!String.IsNullOrEmpty(TheWebConfigURL))
        {
            return  TheWebConfigURL + "/streamingassets/webplayer/";
        }
        else
        {
            path = "file://" + Application.dataPath + "/streamingassets/webplayer/"; 
        }
#else
        path = Application.dataPath + "/streamingassets/webplayer/";
#endif

#else
switch (Application.platform) {
            case RuntimePlatform.Android:
                path = "jar:file://" + Application.dataPath + "!/assets/Android/";
            break;
            case RuntimePlatform.IPhonePlayer:

                path = Application.dataPath + "/Raw/IOS/";
            break;
        case RuntimePlatform.OSXPlayer:
        case RuntimePlatform.OSXEditor:
            path = Application.dataPath + "/StreamingAssets/Mac/";
            break;
        case RuntimePlatform.WindowsEditor:
        case RuntimePlatform.WindowsPlayer:
            path = "file://" + Application.dataPath + "/StreamingAssets/Windows32/";
            break;
        case RuntimePlatform.WindowsWebPlayer:
            path = Application.dataPath + "/streamingassets/webplayer/";
            break;
        default:
            path = Application.dataPath + "/StreamingAssets/Android/";
            break;

        }
#endif
*/


        return path;
    }



    /// <summary>
    /// 添加lua单机事件
    /// </summary>
    public static void AddClick(GameObject go, System.Object luafuc) {
        UIEventListener.Get(go).onClick += delegate(GameObject o) {
//            LuaInterface.LuaFunction func = (LuaInterface.LuaFunction)luafuc;
    //        func.Call();
        };
    }

    public static string LuaPath() {
        if (Const.DebugMode) {
            return Application.dataPath + "/lua/";
        }
        return DataPath + "st/"; 
    }

    /// <summary>
    /// 取得Lua路径
    /// </summary>
    public static string LuaPath(string name) {
        string lowerName = name.ToLower();
        string path = LuaPath();
        if (lowerName.EndsWith(".ab")) {
            return path+ name;
        }
        return path + name + ".ab";
    }
     
   

    public static GameObject LoadAsset(AssetBundle bundle, string name) {
#if UNITY_5
        return bundle.LoadAsset(name, typeof(GameObject)) as GameObject;
#else
        return bundle.Load(name, typeof(GameObject)) as GameObject;
#endif
    }

    public static Component AddComponent(GameObject go, string assembly, string classname) {
        Assembly asmb = Assembly.Load(assembly);
        Type t = asmb.GetType(assembly + "." + classname);
        Component com = go.GetComponent(t);
        if (com == null)
        {
            return go.AddComponent(t);
        }
        return com;
    }

    public static bool TryAddValue<T, K>(this Dictionary<T, K> dic, T key, K value) {
        if (dic.ContainsKey(key)) {
            dic[key] = value;
            return false;
        }
        else {
            dic.Add(key, value);
            return true;
        }
    }


    public static int GetStringByteCount2(string str)
    {
        if (str.Equals(string.Empty))
            return 0;

        int strlen = 0;

        byte[] strBytes = System.Text.Encoding.Unicode.GetBytes(str);
        for (int i = 0; i <= strBytes.Length - 1; i++)
        {
            if (strBytes[i] != 0)
                strlen++;
        }

        return strlen;
    }

    public static int GetStringByteCount(string str)
    {

        if (str.Equals(string.Empty))
            return 0;

        int strlen = 0;

        foreach (char c in str.ToCharArray())
        {
            if (Regex.IsMatch(c.ToString(), @"[\u4e00-\u9fa5]"))
            {
                strlen += 2;
            }
            else
            {
                strlen++;
            }
        }

        return strlen;
    }

    public static bool isWebPlayer
    {
        get
        {
            return Application.platform == RuntimePlatform.WindowsWebPlayer ||
                    Application.platform == RuntimePlatform.OSXWebPlayer;
        }

    }

    public static bool isEditor
    {
        get
        {
#if UNITY_EDITOR
            return true;
#endif
            return false;
        }
    }
}

相关文章

网友评论

      本文标题:util代码

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