版本:Unity 6 (6000.0)
语言英语
  • C#

AssetDatabase.RemoveObjectFromAsset

建议更改

成功!

感谢您帮助我们改进 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每条建议更改,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,无法提交您建议的更改。请在几分钟后重试。感谢您花时间帮助我们改进 Unity 文档的质量。

关闭

取消

声明

public static void RemoveObjectFromAsset(Object objectToRemove);

描述

从其资源中移除对象(其他资源:AssetDatabase.AddObjectToAsset)。

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Scriptable : ScriptableObject { } public class AssetDatabaseExamples : MonoBehaviour { [MenuItem("AssetDatabase/Remove Object From Asset Example")] public static void RemoveObjectFromAssetExample() { var scriptableObjectList = new List<Scriptable>();

//Create Scriptable Objects and store them in a List for (var i = 0; i < 10; i++) { scriptableObjectList.Add(ScriptableObject.CreateInstance<Scriptable>()); AssetDatabase.CreateAsset(scriptableObjectList[i], $"Assets/ScriptableObjects/SO{i}.asset"); }

//Add Materials as Sub Assets to the Scriptable Objects foreach (var so in scriptableObjectList) { AssetDatabase.AddObjectToAsset(new Material(Shader.Find("Standard")), so); } AssetDatabase.SaveAssets();

//Remove Materials from their Scriptable Objects foreach (var so in scriptableObjectList) { var material = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(so), typeof(Material)); AssetDatabase.RemoveObjectFromAsset(material); } AssetDatabase.SaveAssets(); } }