AssetDatabase.AddObjectToAsset

Unity · Версия 2020.3 LTS · Scripting · AssetDatabase

AssetDatabase.AddObjectToAsset

Объявление

public static void AddObjectToAsset(Object objectToAdd, string path);

Параметры

objectToAdd Объект для добавления к существующему активу.
path Путь файловой системы к ресурсу.

Описание

Добавляет objectToAdd к существующему ресурсу по path.

Обратите внимание, что вы должны добавлять активы только в активы '.asset', например, импортированные модели или текстурные активы потеряют свои данные.

Все пути указаны относительно папки проекта, например: "Assets/MyTextures/hello.png".

Примечание.

Вы не можете добавлять игровые объекты; вместо этого используйте PrefabUtility.

using UnityEngine;
using UnityEditor;

public class CreateMaterialExample : MonoBehaviour
{
    [MenuItem("GameObject/Create Material")]
    static void CreateMaterial()
    {
        // Create a simple material asset

Material material = new Material(Shader.Find("Specular"));
        AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");

        // Add an animation clip to it
        AnimationClip animationClip = new AnimationClip();
        animationClip.name = "My Clip";
        AssetDatabase.AddObjectToAsset(animationClip, material);

        // Reimport the asset after adding an object.
        // Otherwise the change only shows up when saving the project
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));

        // Print the path of the created asset
        Debug.Log(AssetDatabase.GetAssetPath(material));
    }
}

Объявление

public static void AddObjectToAsset(Object objectToAdd, Object assetObject);

Описание

Adds objectToAdd to an existing asset identified by assetObject.

Note: You can not add GameObjects; use PrefabUtility instead.

using UnityEngine;
using UnityEditor;

public class CreateMaterialExample : MonoBehaviour
{
    [MenuItem("GameObject/Create Material")]
    static void CreateMaterial()
    {
        // Create a simple material asset

Material material = new Material(Shader.Find("Specular"));
        AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat");

        // Add an animation clip to it
        AnimationClip animationClip = new AnimationClip();
        animationClip.name = "My Clip";
        AssetDatabase.AddObjectToAsset(animationClip, material);

        // Reimport the asset after adding an object.
        // Otherwise the change only shows up when saving the project
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animationClip));

        // Print the path of the created asset
        Debug.Log(AssetDatabase.GetAssetPath(material));
    }
}

Обновлено 17 ноября 2026 г.