/// <summary> /// Rename the file associated with this data. /// </summary> /// <param name="newFilename">New name of the file.</param> /// <returns>true if successful, false otherwise.</returns> public bool RenameAsset(string newFilename) { var filenameComponents = new FilenameComponents(newFilename); Debug.Assert(filenameComponents.directory == Path.GetDirectoryName(filename)); // If the target file exists, delete it. if (AssetImporter.GetAtPath(newFilename) != null) { if (!AssetDatabase.MoveAssetToTrash(newFilename)) { UnityEngine.Debug.LogError( "Failed to move asset to trash: " + filename); return false; } } string error = AssetDatabase.RenameAsset( filename, filenameComponents.basenameNoExtension); if (!String.IsNullOrEmpty(error)) { UnityEngine.Debug.LogError( "Failed to rename asset " + filename + " to " + newFilename + " (" + error + ")"); return false; } AssetDatabase.ImportAsset(newFilename); filename = newFilename; UpdateAssetLabels(); return true; }
/// <summary> /// Parse metadata from filename and store in this class. /// </summary> /// <param name="filename">Name of the file to parse.</param> public FileMetadata(string filename) { this.filename = filename; filenameCanonical = filename; var filenameComponents = new FilenameComponents(filename); // Parse metadata from the filename. string[] tokens = filenameComponents.basenameNoExtension.Split( FILENAME_TOKEN_SEPARATOR); if (tokens.Length > 1) { filenameComponents.basenameNoExtension = tokens[0]; for (int i = 1; i < tokens.Length; ++i) { string token = tokens[i]; if (token == FILENAME_TOKEN_MANIFEST) { isManifest = true; } else if (token.StartsWith(TOKEN_TARGETS)) { targets = ParseTargets(token); } else if (token.StartsWith(TOKEN_VERSION)) { versionString = ParseVersion(token); } } } // Parse metadata from asset labels if it hasn't been specified in // the filename. AssetImporter importer = GetAssetImporter(); if (importer != null) { foreach (string label in AssetDatabase.GetLabels(importer)) { // Labels are converted to title case in the asset database // so convert to lower case before parsing. string lowerLabel = label.ToLower(); if (lowerLabel.StartsWith(LABEL_PREFIX)) { string token = lowerLabel.Substring(LABEL_PREFIX.Length); if (token.StartsWith(TOKEN_TARGETS)) { if (targets == null) { targets = ParseTargets(token); } } else if (token.StartsWith(TOKEN_VERSION)) { if (String.IsNullOrEmpty(versionString)) { versionString = ParseVersion(token); } } else if (token.Equals(FILENAME_TOKEN_MANIFEST)) { isManifest = true; } } } } // On Windows the AssetDatabase converts native path separators // used by the .NET framework '\' to *nix style '/' such that // System.IO.Path generated paths will not match those looked up // in the asset database. So we convert the output of Path.Combine // here to use *nix style paths so that it's possible to perform // simple string comparisons to check for path equality. filenameCanonical = Path.Combine( filenameComponents.directory, filenameComponents.basenameNoExtension + filenameComponents.extension).Replace('\\', '/'); UpdateAssetLabels(); }