Zhonghui

每个不曾起舞的日子,都是对生命的辜负

User Tools

Site Tools


软件:unity:嵌套资源

Unity嵌套资源

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));
}

/var/www/DokuWikiStick/dokuwiki/data/pages/软件/unity/嵌套资源.txt · Last modified: 2023/05/16 09:26 by zh