using UnityEngine; using System; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; public class GameFun{ // - 文字分割 public static string[] SpiltString(ref string strText, string strKey){ string[] stringArray = null; stringArray = strText.Split( new string[] {strKey}, StringSplitOptions.None); return stringArray; } // - 文字擷取 public static string CutStrBeforeKey(ref string strText, string strKey){ string strReturn = ""; int iKeyIdx = strText.IndexOf(strKey); if (iKeyIdx < 0) { // 沒找到關鍵字,則回傳全部 strReturn = strText; strText = ""; } // end if else { strReturn = strText.Substring(0, iKeyIdx); // 取得"關鍵字"之前的字串 strText = strText.Remove(0, iKeyIdx + strKey.Length); // 刪除包含關鍵字之前的字串 } // end else return strReturn; } // - 文字擷取 public static string CutStrAfterKey(ref string strText, string strKey){ int iKeyIdx = strText.IndexOf(strKey); if (iKeyIdx < 0){ return ""; }else{ int iKeyLen = strKey.Length; int iTextLen = strText.Length; // 取得"關鍵字"之後的字串 string strReturn = strText.Substring( iKeyIdx+iKeyLen , iTextLen-iKeyIdx-iKeyLen); // 原字串刪除關鍵字與關鍵字之後的字串 strText = strText.Remove(iKeyIdx, iTextLen-iKeyIdx); return strReturn; } // end else } // - 氣泡排序法 public static void BubbleSortArray(ref int[] _Array, int _First, int _Kind){ bool _Bool = false; for (int _i = _Array.Length; _i > _First; _i--){ _Bool = false; for (int _j = _First; _j < _i; _j++){ switch(_Kind){ case 1: // - 小 -> 大 if (_Array[_j] > _Array[_j + 1]){ SwapArray(ref _Array, _j, _j + 1); _Bool = true; } break; case 2: // - 大 -> 小 if (_Array[_j] < _Array[_j + 1]){ SwapArray(ref _Array, _j, _j + 1); _Bool = true; } break; } } if (!_Bool) break; } } // - 快速排序 public static void QuickSortArray(ref int[] _Array, int _First = 0){ QuickSort(ref _Array, _First, _Array.Length - 1); } private static void QuickSort(ref int[] _Array, int _left, int _right){ if (_left < _right) { int middle = _Array[(_left + _right) / 2]; int i = _left - 1; int j = _right + 1; while (true) { while (_Array[++i] < middle) ; while (_Array[--j] > middle) ; if (i >= j) break; SwapArray(ref _Array, i, j); } QuickSort(ref _Array, _left, i - 1); QuickSort(ref _Array, j + 1, _right); } } // - public static void SwapArray(ref int[] _Array, int _A, int _B){ int _Temp = 0; _Temp = _Array[_A]; _Array[_A] = _Array[_B]; _Array[_B] = _Temp; } // - 網路找到文字換行 public static string WarpWord(string originalWord,int lineWidth){ StringBuilder sb = new StringBuilder(); Regex punctuationRegex = new Regex(@"[,。;?~!:‘“”’【】()]"); int tempNum = 0; char[] c = originalWord.ToCharArray(); for (int i = 0; i < c.Length;i++){ if (c[i] >= 0x4e00 && c[i] <= 0x9fa5){ tempNum += 2; if(tempNum>lineWidth){ i--; sb.Append("\n"); tempNum = 0; }else{ sb.Append(c[i]); } }else{ if (punctuationRegex.IsMatch(c[i].ToString())){ tempNum += 2; if(tempNum>lineWidth){ i--; sb.Append("\n"); tempNum = 0; }else{ sb.Append(c[i]); } }else{ tempNum++; if(tempNum>lineWidth){ i--; sb.Append("\n"); tempNum = 0; }else{ sb.Append(c[i]); } } } } return sb.ToString(); } // - 用來接全部的Debug.log之後只要關這個地方就好了 public static void ShowDebugLog(string _Msg){ Debug.Log(_Msg); } }