Version: 2021.1
언어: 한국어

AssetDatabase.GenerateUniqueAssetPath

매뉴얼로 전환
public static string GenerateUniqueAssetPath (string path);

설명

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); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961