Creates a new unique path for an asset.
When you call this method, Unity checks to see whether an asset already exists with the matching path and filename you supply. If it does not exist, Unity returns the same string you supplied. If there is already an existing asset with the matching path and filename, Unity appends the number 1 to the filename and checks again. It continues incrementing this number and checking again until it finds a filename that does not currently exists, and returns the path with that new unique filename.
All paths generated are relative to the project folder, for example: "Assets/MyTextures/hello.png".
using UnityEditor; using UnityEngine;
public class GenerateUniqueAssetPathExample : MonoBehaviour { [MenuItem("APIExamples/GenerateUniqueAssetPath")] static void GenerateUniqueAssetPathForFilesWithSameName() { for (int i = 0; i < 5; ++i) { //The file names that this should create are: // Assets/Artifacts/material.mat // Assets/Artifacts/material 1.mat // Assets/Artifacts/material 2.mat // Assets/Artifacts/material 3.mat // Assets/Artifacts/material 4.mat var uniqueFileName = AssetDatabase.GenerateUniqueAssetPath("Assets/Artifacts/material.mat");
Material material = new Material(Shader.Find("Specular")); AssetDatabase.CreateAsset(material, uniqueFileName); } } }