using System; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.EventSystems; namespace Funique { /// /// Extensions: String
/// ------------------------------------------------
/// 擴充: 字串 ///
public static class StringExtensions { public static string FilterVideoName(this string videoSourceName) { string filterVideoName = Path.GetFileNameWithoutExtension(videoSourceName); // fileName_format_time string videoFileType = Path.GetExtension(videoSourceName); //meida format (.mp4 .mov) string[] sLocation = filterVideoName.Split('_'); // fileName + format + time try { // To detect the videoSourceName have time or not. if not will stop filter TranslateTime(sLocation[sLocation.Length - 1].ToLower()); filterVideoName = filterVideoName.Replace("_" + sLocation[sLocation.Length - 1], ""); filterVideoName = filterVideoName + videoFileType; } catch (Exception e) { filterVideoName = videoSourceName; } return filterVideoName; } public static float FilterVideoTime(this string VideoSourceName) { double totalSecTime = 0; string sendVideoName = Path.GetFileNameWithoutExtension(VideoSourceName); //fileName_formate_time string[] sLocation = sendVideoName.Split('_'); // fileName + format + time try { totalSecTime = TranslateTime(sLocation[sLocation.Length - 1].ToLower()); } catch (Exception e) { return 0; } return (float)totalSecTime; } public static double TranslateTime(this string timeString) { string[] hourString = timeString.Split('h'); int h = 0; if (hourString.Length == 2) { h = int.Parse(hourString[0]); } string[] minString = hourString[hourString.Length - 1].Split('m'); int m = 0; if (minString.Length == 2) { m = int.Parse(minString[0]); } string[] secString = minString[minString.Length - 1].Split('s'); int s = 0; if (secString.Length == 2) { s = int.Parse(secString[0]); } double totalSecTime = new TimeSpan(h, m, s).TotalSeconds; if (totalSecTime == 0) { throw new ArgumentOutOfRangeException(); } return totalSecTime; } public static string FilterBtnName(this string VideoSourceName) { string btnName = ""; if (!string.IsNullOrEmpty(VideoSourceName)) { try { string videoName = VideoSourceName.Substring(0, VideoSourceName.LastIndexOf(".")); string[] sLocation = videoName.Split('_'); // fileName + format + time bool bhavetime = false; string[] videoformat = StereoModeUtility.AllStereoType; foreach (var go in videoformat) { if (sLocation[sLocation.Length - 2] == go) { bhavetime = true; break; } } string _btn_name = null; int videoLength = sLocation.Length; // have time if (bhavetime) { videoLength = sLocation.Length - 1; // doesn't have time } for (int j = 0; j < videoLength; j++) { if (j == videoLength - 1) { _btn_name += sLocation[j]; } else { _btn_name += sLocation[j] + "_"; } } if (!string.IsNullOrEmpty(_btn_name)) { btnName = _btn_name; } else { btnName = "FileName Bug > " + VideoSourceName; } } catch (Exception e) { btnName = "FileName Bug"; } } return btnName; } public static string MergeTime(this string name, double time) { TimeSpan NowTime = TimeSpan.FromSeconds(time); string VideoName = Path.GetFileNameWithoutExtension(name); if (NowTime.Hours > 0 || NowTime.Minutes > 0 || NowTime.Seconds > 0) VideoName += "_"; if (NowTime.Hours > 0) VideoName += NowTime.Hours + "h"; if (NowTime.Minutes > 0) VideoName += NowTime.Minutes + "m"; if (NowTime.Seconds > 0) VideoName += NowTime.Seconds + "s"; VideoName += Path.GetExtension(name); return VideoName; } public static string DetectStereoMode(this string videoFilterName) { string stereoMode = StereoModeUtility.StereoModeDict[StereoMode.Unknown]; // Default string[] sLocation = videoFilterName.Split('.'); if (sLocation.Length < 2) return stereoMode; try { sLocation = sLocation[sLocation.Length - 2].Split('_'); stereoMode = sLocation[sLocation.Length - 1]; if (StereoModeUtility.CheckinType(stereoMode)) { stereoMode = StereoModeUtility.StereoModeDict[StereoModeUtility.ReverseSearch(stereoMode)]; } } catch (InvalidCastException e) { FuniqueLogger.Log("Invalid stereo mode", "Tool"); } return stereoMode; } public static bool CheckRaycast(this string tag) { PointerEventData pointer = new PointerEventData(EventSystem.current); pointer.position = Input.mousePosition; List raycastResults = new List(); EventSystem.current.RaycastAll(pointer, raycastResults); if (raycastResults.Count > 0) { foreach (var go in raycastResults) { if (go.gameObject.tag == tag) { return true; } } } return false; } } }