Version: 2023.2
public static void SaveAssets ();

描述

将所有未保存的资源更改写入磁盘。

EditorApplication.SaveAssets 相同,但不允许在序列化期间调用。

调用此函数时,会调用 AssetModificationProcessor.OnWillSaveAssets,从而允许覆盖保存到磁盘的文件。

EditorApplication.SaveAssets 将在未来版本中弃用。请使用 SaveAssets 来保持未来的兼容性。

using UnityEngine;
using UnityEditor;
using System.Text;

public class SaveAssetsExample : MonoBehaviour { [MenuItem("APIExamples/SaveAssets")] static void AssetsToBeSaved() { Color[] colorList = new[] { Color.red, Color.green, Color.blue, Color.black, Color.gray };

for (int i = 0; i < colorList.Length; ++i) { Material material = new Material(Shader.Find("Specular")); var materialName = "material_" + i + ".mat"; AssetDatabase.CreateAsset(material, "Assets/Artifacts/" + materialName);

material.SetColor("_Color", colorList[i]); }

AssetDatabase.SaveAssets(); } }

public class OutputListOfFilesToSave : UnityEditor.AssetModificationProcessor { //Will be invoked once for each call to CreateAsset() //and once for calling AssetDatabase.SaveAssets() static string[] OnWillSaveAssets(string[] paths) { Debug.Log("OnWillSaveAssets invoked");

StringBuilder assetsToBeSaved = new StringBuilder(); assetsToBeSaved.AppendLine();

foreach (string path in paths) { assetsToBeSaved.Append(path); assetsToBeSaved.AppendLine(); }

Debug.Log("Assets to be saved:" + assetsToBeSaved.ToString());

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