[AI相关]非常遗憾地告诉大家,以后可能真的没有程序员这个职业了,用DeepSeek写出Unity导出AssetBundle的实现代码

AI写的代码,虽然我没有完全验证下面的代码,但是已经得到我想要的关键字(并且它之前给出的几段代码,经我审查,是高度可用的代码)

而且整个编码是一个相当“恐怖”的过程

DeepSeek写代码已经具备以下能力:

日常,我可能也会去评定一些程序员,

如果让我从仅有的几个维度去评价DeepSeek,它已经是一个“高潜”的程序员,“完美无缺”,二十四小时,“任劳任怨”的程序员,完全不会失去专注力

还是免费的 

DeepSeek现能力:

  1. 能够迭代的写代码,(是真的告诉他这个不对,需要往哪个方向写代码)
  2. 能够完全按另一个逻辑,另一个流程重写代码
  3. 能理解你的需求(哪怕是中英混合,例如:“需要每个子Asset的代码”)
  4. 能分析到整段代码每个段落,并部分加强
  5. 到最后,后面输出(用什么格式,什么类型的代码,简直都不算是个事了)
  6. 而且完全没有语法错误(有自检机制的,大语言模型本身最重要的机制就是“训练评分”)

我之前写的文章,部分小黑子说理解不了,不知道说的什么,我给AI-DeepSeek同样的句子,同样的词语,DeepSeek理解了99.9%,这么多年,我并没有进步,是你的理解不如AI

好的,我明白你的需求了!你希望从 AssetBundle 中提取(Extract)每个子资源(Sub-Asset),例如从图集中提取每张图片,或者从复合资源中提取每个独立的资源。

Unity 的 AssetBundle 中可能包含复合资源(例如 Sprite Atlas 图集),这些资源在 AssetBundle 中是以子资源(Sub-Asset)的形式存在的。为了提取这些子资源,我们需要使用 `AssetBundle.LoadAssetWithSubAssets` 方法。

以下是完整的代码,展示如何从 AssetBundle 中提取每个子资源并保存到 Unity 项目中:

---
 

//### 代码:提取 AssetBundle 中的子资源
//```csharp
using UnityEditor;
using UnityEngine;
using System.IO;

public class ExtractAssetBundleSubAssets : MonoBehaviour
{
    [MenuItem("Tools/Extract AssetBundle Sub-Assets")]
    public static void ExtractSubAssets()
    {
        // 选择要加载的 AssetBundle 文件
        string assetBundlePath = EditorUtility.OpenFilePanel("Select AssetBundle", "", "assetbundle");
        if (string.IsNullOrEmpty(assetBundlePath))
        {
            Debug.LogError("未选择 AssetBundle 文件");
            return;
        }

        // 加载 AssetBundle
        AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlePath);
        if (assetBundle == null)
        {
            Debug.LogError("加载 AssetBundle 失败");
            return;
        }

        // 获取 AssetBundle 中的所有资源名称
        string[] assetNames = assetBundle.GetAllAssetNames();
        if (assetNames.Length == 0)
        {
            Debug.LogError("AssetBundle 中没有资源");
            assetBundle.Unload(false);
            return;
        }

        // 选择保存资源的文件夹
        string saveFolder = EditorUtility.SaveFolderPanel("Save Sub-Assets to Folder", "Assets", "");
        if (string.IsNullOrEmpty(saveFolder))
        {
            Debug.LogError("未选择保存文件夹");
            assetBundle.Unload(false);
            return;
        }

        // 将保存路径转换为相对于项目的路径
        string relativeSaveFolder = "Assets" + saveFolder.Substring(Application.dataPath.Length);

        // 遍历所有资源并提取子资源
        foreach (string assetName in assetNames)
        {
            // 加载主资源及其子资源
            Object[] subAssets = assetBundle.LoadAssetWithSubAssets(assetName);
            if (subAssets == null || subAssets.Length == 0)
            {
                Debug.LogWarning($"资源 {assetName} 没有子资源");
                continue;
            }

            // 保存每个子资源
            foreach (Object subAsset in subAssets)
            {
                if (subAsset == null)
                    continue;

                // 生成保存路径
                string assetPath = Path.Combine(relativeSaveFolder, $"{subAsset.name}{GetAssetExtension(subAsset)}");
                assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath); // 确保路径唯一

                // 保存资源
                if (subAsset is GameObject)
                {
                    // 如果是 GameObject,保存为预制体
                    PrefabUtility.SaveAsPrefabAsset((GameObject)subAsset, assetPath);
                }
                else
                {
                    // 其他资源(如 Texture2D、Sprite、Material 等)
                    AssetDatabase.CreateAsset(subAsset, assetPath);
                }

                Debug.Log($"子资源已保存: {assetPath}");
            }
        }

        // 刷新 AssetDatabase
        AssetDatabase.Refresh();

        // 卸载 AssetBundle
        assetBundle.Unload(false);
    }

    // 获取资源的文件扩展名
    private static string GetAssetExtension(Object asset)
    {
        if (asset is GameObject)
            return ".prefab";
        if (asset is Texture2D)
            return ".png";
        if (asset is Sprite)
            return ".asset"; // Sprite 是 Texture2D 的子资源,保存为 .asset
        if (asset is Material)
            return ".mat";
        if (asset is AudioClip)
            return ".wav";
        return ".asset";
    }
}

---

### 代码说明
1. **加载 AssetBundle**:
   - 使用 `AssetBundle.LoadFromFile` 加载指定的 AssetBundle 文件。

2. **获取所有资源名称**:
   - 使用 `AssetBundle.GetAllAssetNames` 获取 AssetBundle 中所有资源的路径。

3. **加载子资源**:
   - 使用 `AssetBundle.LoadAssetWithSubAssets` 加载每个资源及其子资源。

4. **保存子资源**:
   - 遍历每个子资源,根据资源类型生成保存路径。
   - 使用 `PrefabUtility.SaveAsPrefabAsset` 保存 GameObject 为预制体。
   - 使用 `AssetDatabase.CreateAsset` 保存其他资源(如 Texture2D、Sprite、Material 等)。

5. **处理文件扩展名**:
   - 根据资源类型生成正确的文件扩展名(如 `.prefab`、`.png`、`.mat` 等)。

6. **刷新 AssetDatabase**:
   - 使用 `AssetDatabase.Refresh` 刷新 Unity 项目,确保保存的资源立即显示在项目中。

7. **卸载 AssetBundle**:
   - 使用 `assetBundle.Unload(false)` 卸载 AssetBundle,释放内存。

---

### 示例:提取子资源
假设 AssetBundle 中包含以下资源:
- 一个 Sprite Atlas 图集(`MyAtlas.spriteatlas`),其中包含多个 Sprite 子资源。
- 一个复合材质(`MyMaterial.mat`),其中包含多个贴图子资源。

运行上述代码后,所有子资源会被提取并保存到指定的文件夹中:
```
Assets/SavedAssets/
    - Sprite1.asset
    - Sprite2.asset
    - Texture1.png
    - Texture2.png
```

---

### 注意事项
1. **子资源类型**:
   - 子资源可能是 Sprite、Texture2D、Material 等,需要根据类型正确处理。

2. **资源唯一性**:
   - 使用 `AssetDatabase.GenerateUniqueAssetPath` 确保保存的资源路径唯一,避免覆盖现有资源。

3. **目标平台**:
   - 加载 AssetBundle 时,确保 AssetBundle 是为当前平台构建的。

---

### 总结
通过上述代码,你可以从 AssetBundle 中提取每个子资源(Sub-Asset),并将它们保存到 Unity 项目中。这种方法特别适用于处理复合资源(如 Sprite Atlas 图集)的场景。

有一点点理解了柯洁当年面对Alpha-Go是何等的绝望

 当年柯洁面对的还只是通用AI,只能处理特定问题。

而今天所有人都不得不面对的是:通用大模型

已经过了为智驾冲动消费的年纪也没有买电车,但是哪些车企CEO,高管口中说的:比老司机还会开车,应该是真的!!当然,你首先得先成为一个老司机才能体会个中乐趣!!

加油!!成为一个更好的自己而努力!!总归是没有错的 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值