AssetDatabase.AddObjectToAsset
可以将一个Object作为子对象添加到一个Asset内部
一个Asset里面包含多个Object是很常见的(随便用文本模式打开一个prefab文件你就看到了),这么多Object中,可以单独被查看的Object就会被显示为嵌套的资源[待验证]
来自Unity文档
[MenuItem("GameObject/Create Material")] static void CreateMaterial() { // Create a simple material asset Material material = new Material(Shader.Find("Specular")); // 将Object保存为资源 AssetDatabase.CreateAsset(material, "Assets/MyMaterial.mat"); // Add an animation clip to it AnimationClip animationClip = new AnimationClip(); animationClip.name = "My Clip"; // 将一个Object添加到另一个Object之中,作为嵌套的子资源 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)); // 路径: // Assets/MyMaterial.mat // Assets/MyMaterial.mat[My Clip] // Print the path of the created asset Debug.Log(AssetDatabase.GetAssetPath(material)); }