AssetDatabase.SetMainObject

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

AssetDatabase.SetMainObject

Объявление

public static void SetMainObject(Object mainObject, string assetPath);

Параметры

mainObject Объект, который должен стать главным объектом.
assetPath Путь к файлу актива.

Описание

Указывает, какой объект в файле актива должен стать основным объектом после следующего импорта.

Все остальные объекты актива становятся дочерними по отношению к основному объекту. ПРИМЕЧАНИЕ. Эта функция изменяет объект импортера, а не сам актив. Следующий импорт отражает изменение в импортированном объекте.

using UnityEditor;
using UnityEngine;

public class Scriptable : ScriptableObject
{
}
public class AssetDatabaseExamples : MonoBehaviour
{
    [MenuItem("AssetDatabase/Set Main Object Example")]
    public static void SetMainObjectExample()
    {
        //Create a Scriptable Object and a Material
        var materialAsset = new Material(Shader.Find("Standard"));
        var scriptableAssetPath = "Assets/ScriptableObjects/NewObject.asset";
        var mainAsset = ScriptableObject.CreateInstance();
        AssetDatabase.CreateAsset(mainAsset, scriptableAssetPath);

        //Add the Material to the Scriptable Object
        AssetDatabase.AddObjectToAsset(materialAsset, scriptableAssetPath);
        AssetDatabase.SaveAssets();

        //Set the Material to be the main Object and import it
        AssetDatabase.SetMainObject(materialAsset, scriptableAssetPath);
        AssetDatabase.ImportAsset(scriptableAssetPath);
    }
}

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