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