UserHelper代码

36 篇文章 1 订阅
    /// <summary>
    /// 截取字符串(中英文)
    /// </summary>
    public static string CutString(string text, int length, string replacetxt = "")
    {
        int strLength = 0;
        StringBuilder strb = new StringBuilder();
        char[] Temp = text.ToCharArray();
        for (int i = 0; i != Temp.Length; i++)
        {
            if (strLength >= length) //
            {
                strb.Append(replacetxt);
                break;
            }
            else
            {
                if (((int)Temp[i]) < 255) //大于255的都是汉字或者特殊字符
                {
                    strLength++;
                }
                else
                {
                    strLength = strLength + 2;
                }
                strb.Append(Temp[i]);
            }
        }
        return strb.ToString();
    }

    #region 播放音频 PlaySound

    public static IEnumerator PlaySound(string soundName, Action endSound = null, string GoName = "soundGameObject", Transform _parent = null)
    {
        AudioClip ac = (AudioClip)Resources.Load("Audio/" + soundName);
        GameObject go;
        if (GameObject.Find(GoName))
            go = GameObject.Find(GoName);
        else
            go = new GameObject(GoName);
        AudioSource aSource;
        go.transform.SetParent(_parent);
        if (go.GetComponent<AudioSource>())
            aSource = go.GetComponent<AudioSource>();
        else
            aSource = go.AddComponent<AudioSource>();
        aSource.clip = ac;
        aSource.Play();
        if (endSound != null)
        {
            while (true)
            {
                if (aSource)
                {
                    if (!aSource.isPlaying)
                    {
                        endSound.Invoke();
                        MonoBehaviour.Destroy(go);
                        break;
                    }
                }
                else
                {
                    break;
                }
                yield return new WaitForFixedUpdate();
            }
        }
    }

    #endregion

    #region 获取每页字符
    public static string[] getPageInfo(string str, out int pageNum, int _max_char_for_a_line = 19, int _max_line_for_a_page = 10)
    {
        string[] intro = new string[200]; //每行字符存储的数组  
        string[] pageInfo = new string[30]; //每页信息的数组,默认不超过30页。  
        int max_char_for_a_line = _max_char_for_a_line; //每行最多存储的字符个数  
        int max_line_for_a_page = _max_line_for_a_page; //每页最多存储的行数  

        int length = str.Length; //文件里字符串总长度      
        int correntLine = 0; //当前行数  
        int tempChar = 0; //当前行字符个数  
        for (int i = 0; i < length; i++)
        {
            if ((str[i] != '\n') && (str[i] != '\r'))
            {
                if (tempChar == max_char_for_a_line) //如果已经存了15个字符就换行  
                {
                    correntLine++;
                    tempChar = 0;
                }
                tempChar++;
                intro[correntLine] += str[i];
            }
            else if (str[i] == '\n')
            {
                tempChar = 0;
                correntLine++;
            }
        }
        int totalLine = correntLine + 1;
        //现在intro[]为分行数组,totalLine为总行数  
        int correntPage = 0; //当前第几页  
        int correntPageLine = 0; //当前页到几行  
        for (int i = 0; i < totalLine; i++)
        {
            if (correntPageLine == max_line_for_a_page) //到了最大行就换一页  
            {
                correntPage++;
                correntPageLine = 0;
            }
            pageInfo[correntPage] = pageInfo[correntPage] + intro[i] + "\n";
            correntPageLine++;
        }

        pageNum = correntPage + 1; //out出总页数  
        return pageInfo; //返回每页信息的数组  
    }
    #endregion

    #region 去重
    /// <summary>
    /// 去重
    /// </summary>
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
    #endregion

    ///<summary>
    /// GPS经纬度换算成x,y坐标
    ///</summary>
    ///<param name="l">精度</param>
    ///<param name="B">纬度</param>
    public static Vector3 GetMCT84Bl2xy(double l, double B)
    {
        double xc; double yc;
        try
        {
            l = l * Math.PI / 180;
            B = B * Math.PI / 180;

            double B0 = 30 * Math.PI / 180;

            double N = 0, e = 0, a = 0, b = 0, e2 = 0, K = 0;
            a = 6378137;
            b = 6356752.3142;
            e = Math.Sqrt(1 - (b / a) * (b / a));
            e2 = Math.Sqrt((a / b) * (a / b) - 1);
            double CosB0 = Math.Cos(B0);
            N = (a * a / b) / Math.Sqrt(1 + e2 * e2 * CosB0 * CosB0);
            K = N * CosB0;

            double Pi = Math.PI;
            double SinB = Math.Sin(B);

            double tan = Math.Tan(Pi / 4 + B / 2);
            double E2 = Math.Pow((1 - e * SinB) / (1 + e * SinB), e / 2);
            double xx = tan * E2;

            xc = K * Math.Log(xx);
            yc = K * l;
        }
        catch (Exception ErrInfo)
        {
            xc = -1;
            yc = -1;
        }


        return new Vector3((float)xc, 0f, (float)yc);
    }

    public static void ClearChildens(Transform parent)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            Object.Destroy(parent.GetChild(i).gameObject);
        }
    }
    public static void ActiveChildens(Transform parent, bool isActive = true)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            parent.GetChild(i).gameObject.SetActive(isActive);
        }
    }

        #region 获取磁盘空间
        //单位MB
        public static long GetHardDiskSpace(string str_HardDiskName)
        {
            long totalSize = 0;
            str_HardDiskName = str_HardDiskName + ":\\";
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    totalSize = drive.TotalFreeSpace;
                }
            }
            return totalSize;
        }
        #endregion

        #region 执行CMD命令

        //dosCommand Dos命令语句  
        public static string Execute(string dosCommand)
        {
            return Execute(dosCommand, 10);
        }
        /// <summary>  
        /// 执行DOS命令,返回DOS命令的输出  
        /// </summary>  
        /// <param name="dosCommand">dos命令</param>  
        /// <param name="milliseconds">等待命令执行的时间(单位:毫秒),  
        /// 如果设定为0,则无限等待</param>  
        /// <returns>返回DOS命令的输出</returns>  
        public static string Execute(string command, int seconds)
        {
            string output = ""; //输出字符串  
            if (command != null && !command.Equals(""))
            {
                Process process = new Process();//创建进程对象  
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";//设定需要执行的命令  
                startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出  
                startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                startInfo.RedirectStandardInput = false;//不重定向输入  
                startInfo.RedirectStandardOutput = true; //重定向输出  
                startInfo.CreateNoWindow = true;//不创建窗口  
                process.StartInfo = startInfo;
                try
                {
                    if (process.Start())//开始进程  
                    {
                        if (seconds == 0)
                        {
                            process.WaitForExit();//这里无限等待进程结束  
                        }
                        else
                        {
                            process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒  
                        }
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出  
                    }
                }
                catch
                {
                    Debug("Execute错误!");
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
            return output;
        }

        #endregion

        #region 关闭进程
        /// <summary>
        /// 关闭进程
        /// </summary>
        /// <param name="processName">进程名</param>
        public static void KillProcess(string processName)
        {
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName == processName)
                {
                    item.Kill();
                }
            }
        }
        #endregion

        #region 是否有网络
        private const int INTERNET_CONNECTION_MODEM = 1;
        private const int INTERNET_CONNECTION_LAN = 2;
        [DllImport("winInet.dll ")]
        private static extern bool InternetGetConnectedState(
        ref int dwFlag,
        int dwReserved
        );

        public static bool GetIsInternet()
        {
            int dwFlag = new int();
            if (!InternetGetConnectedState(ref dwFlag, 0))
                return false;
            return true;
        }
        #endregion

    #region 自动添加换行
    /// <summary>
     /// 获取拆分后的字符串列表。默认20位
     /// </summary>
     /// <param name="mOrigianlString"></param>
     /// <param name="subStringCharNumber"></param>
     /// <returns></returns>
    public static string GetSeparateSubString(string mOrigianlString, int subStringCharNumber=40)
    {
        string str = "";
        string tempStr = mOrigianlString;
        int charNumber = subStringCharNumber;
        int totalCount = 0;
        string mSubStr = "";
        for (int i = 0; i < tempStr.Length; i++)
        {
            string mChar = tempStr.Substring(i, 1);
            int byteCount = Encoding.Default.GetByteCount(mChar);//关键点判断字符所占的字节数

            if (byteCount == 1)
            {
                totalCount++;
                mSubStr += mChar;
                if (totalCount == charNumber || i == tempStr.Length - 1)
                {
                    str += mSubStr + "\n";
                    totalCount = 0;
                    mSubStr = "";
                }
            }
            else if (byteCount > 1)
            {

                totalCount += 2;
                if (totalCount > charNumber)
                {
                    str += mSubStr + "\n";
                    if (i == tempStr.Length - 1)
                    {
                        mSubStr = mChar;
                        str += mSubStr + "\n";
                    }
                    else
                    {
                        totalCount = 2;
                        mSubStr = mChar;
                    }
                }
                else if (totalCount == charNumber)
                {
                    mSubStr += mChar;
                    str += mSubStr + "\n";
                    totalCount = 0;
                    mSubStr = "";
                }
                else if (i == tempStr.Length - 1)
                {
                    mSubStr += mChar;
                    str += mSubStr + "\n";
                }
                else
                {
                    mSubStr += mChar;
                }
            }
        }
        return str.Replace("|","");
    }
    #endregion

       /// <summary>
        /// 获取拆分后的字符串列表。
        /// </summary>
        /// <param name="mOrigianlString"></param>
        /// <param name="subStringCharNumber"></param>
        /// <returns></returns>        
        private ArrayList GetSeparateSubString(string mOrigianlString, int subStringCharNumber)
        {
            ArrayList resultList = new ArrayList();
            string tempStr = mOrigianlString;
            int charNumber = subStringCharNumber;
            int totalCount = 0;
            string mSubStr = "";
            for (int i = 0; i < tempStr.Length; i++)
            {
                string mChar = tempStr.Substring(i, 1);
                int byteCount = Encoding.Default.GetByteCount(mChar);//关键点判断字符所占的字节数

                if (byteCount == 1)
                {
                    totalCount++;
                    mSubStr += mChar;
                    if (totalCount == charNumber || i == tempStr.Length - 1)
                    {
                        resultList.Add(mSubStr);
                        totalCount = 0;
                        mSubStr = "";
                    }
                }
                else if (byteCount > 1)
                {

                    totalCount += 2;
                    if (totalCount > charNumber )
                    {
                        resultList.Add(mSubStr);
                        if (i == tempStr.Length - 1)
                        {
                            mSubStr = mChar;
                            resultList.Add(mSubStr);
                        }
                        else
                        {
                            totalCount = 2;
                            mSubStr = mChar;
                        }
                    }
                    else if (totalCount == charNumber)
                    {
                        mSubStr += mChar;
                        resultList.Add(mSubStr);
                        totalCount = 0;
                        mSubStr = "";
                    }
                    else if (i == tempStr.Length - 1)
                    {
                        mSubStr += mChar;
                        resultList.Add(mSubStr);
                    }
                    else
                    {
                        mSubStr += mChar;
                    }
                }
            }
            return resultList;
        }

    public Bounds GetBounds(GameObject target, bool include_children = true)
    {

        Renderer[] mrs = target.gameObject.GetComponentsInChildren<Renderer>();
        Vector3 center = target.transform.position;
        Bounds bounds = new Bounds(center, Vector3.zero);
        if (include_children)
        {
            if (mrs.Length != 0)
            {
                foreach (Renderer mr in mrs)
                {
                    bounds.Encapsulate(mr.bounds);
                }
            }
        }
        else
        {
            Renderer rend = target.GetComponentInChildren<Renderer>();
            if (rend)
            {
                bounds = rend.bounds;
            }
        }
        return bounds;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呼呼突突

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值