public void AssetPathTOSystemPathConvert() { string systemPath = Application.dataPath + "/UnityIO/Editor/Unit Tests/Loading Assets/Misc Prefab.prefab"; string assetPath = "Assets/UnityIO/Editor/Unit Tests/Loading Assets/Misc Prefab.prefab"; string result = IO.AssetPathToSystemPath(assetPath); Assert.AreEqual(systemPath, result, "The path did not convert correctly"); }
/// <summary> /// Move an asset file from one folder to another. /// </summary> /// <param name="oldPath">The path where the asset currently resides.</param> /// <param name="newPath">The path which the asset should be moved to.</param> /// <returns></returns> public static string MoveAsset(string oldPath, string newPath) { #if UNITY_EDITOR return(uAssetDatabase.MoveAsset(oldPath, newPath)); #else oldPath = IO.AssetPathToSystemPath(oldPath); newPath = IO.AssetPathToSystemPath(newPath); File.Move(oldPath, newPath); return(null); #endif }
/// <summary> /// returns true if it exists, false otherwise false. /// </summary> public static bool IsValidFolder(string assetPath) { #if UNITY_EDITOR return(uAssetDatabase.IsValidFolder(assetPath)); #else // Convert our path to a system path string sourceFilePath = IO.AssetPathToSystemPath(assetPath); // Return if it exists or not. return(Directory.Exists(sourceFilePath)); #endif }
/// <summary> /// Duplicates the asset at path and stores it at newPath. /// </summary> /// <returns>Returns true if the copy was successful.</returns> public static bool CopyAsset(string path, string newPath) { #if UNITY_EDITOR return(uAssetDatabase.CopyAsset(path, newPath)); #else // Convert our path to a system path string sourceFilePath = IO.AssetPathToSystemPath(path); // Get a unique one for our detestation path. string destSystemPath = GenerateUniqueAssetPath(newPath); // Copy the file. File.Copy(sourceFilePath, destSystemPath, false); // Return our result return(true); #endif }
/// <summary> /// Creates a new unique path for an asset. /// </summary> public static string GenerateUniqueAssetPath(string assetPath) { // Get our system path since we need the full one. string systemPath = IO.AssetPathToSystemPath(assetPath); // Create a holder for a unique one. string uniquePath = systemPath; // Loop till max int (We should never have that many folder but we don't want to loop forever). for (int i = 0; i < int.MaxValue; i++) { // If the file does not exist we can break. if (!File.Exists(uniquePath)) { break; } // One with that name already exists so we append our number to the file name. uniquePath = IO.AppendName(systemPath, " " + i.ToString()); } return(uniquePath); }
/// <summary> /// Deletes the asset file at path. // Returns true if the asset has been successfully deleted, false if it doesn't exit or couldn't be removed. /// </summary> public static bool DeleteAsset(string assetPath) { #if UNITY_EDITOR return(uAssetDatabase.DeleteAsset(assetPath)); #else // Convert to system path string systemPath = IO.AssetPathToSystemPath(assetPath); // Check if it exists if (File.Exists(systemPath)) { // Delete it File.Delete(systemPath); // Return true return(true); } else { return(false); } #endif }
/// <summary> /// Checks if an asset file can be moved from one folder to another. (Without actually moving the file). /// </summary> /// <param name="oldPath">The path where the asset currently resides.</param> /// <param name="newPath">The path which the asset should be moved to.</param> /// <returns>An empty string if the asset can be moved, otherwise an error message.</returns> public static string ValidateMoveAsset(string oldPath, string newPath) { #if UNITY_EDITOR return(uAssetDatabase.ValidateMoveAsset(oldPath, newPath)); #else oldPath = IO.AssetPathToSystemPath(oldPath); newPath = IO.AssetPathToSystemPath(newPath); if (File.Exists(newPath)) { return("File Already Exists"); } if (AssetDatabase.IsValidFileName(newPath)) { return("Not valid file name"); } return(string.Empty); #endif }