예제 #1
0
        public static AudioClip Load(AudioKey InAudioKey)
        {
#if UNITY_EDITOR
            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (!AudioConfig.USE_ASSET_BUNDLE_IN_EDITOR_B)
            {
                if (_keyForFullname == null)
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo("Assets/Bundle/Audio");
                    FileInfo[]    files         = directoryInfo.GetFiles();
                    int           count         = files.Length;
                    _keyForFullname = new Dictionary <string, string>(count);
                    for (int i = 0; i < count; i++)
                    {
                        if (files[i].Extension == ".meta")
                        {
                            continue;
                        }

                        string fullname = files[i].Name;
                        _keyForFullname.Add(Path.GetFileNameWithoutExtension(fullname), fullname);
                    }
                }

                if (_keyForFullname.TryGetValue(InAudioKey.ToString(), out var filename))
                {
                    return(UnityEditor.AssetDatabase.LoadAssetAtPath <AudioClip>(Path.Combine("Assets/Bundle/Audio/",
                                                                                              filename)));
                }

                Debug.LogError($"Audio clip named '{InAudioKey}' not found in .");

                return(null);
            }
#endif
            string fileName   = InAudioKey.ToString();
            string bundleName = fileName.ToLower();

            string localPath =
                UnityPathTools.GetPersistentDataPath(
                    $"b22f0418e8ac915eb66f829d262d14a2/{MD5Tools.GetStringMd5(bundleName)}");

            string streamPath = UnityPathTools.GetStreamingAssetsPath($"Audio/{MD5Tools.GetStringMd5(bundleName)}");

            AssetBundle ab = AssetBundle.LoadFromFile(File.Exists(localPath) ? localPath : streamPath);

            if (ab == null)
            {
                return(null);
            }

            AudioClip ac = ab.LoadAsset <AudioClip>(fileName);

            ab.Unload(false);

            return(ac);
        }
예제 #2
0
        public void OnPreprocessBuild(BuildTarget InTarget, string InPath)
        {
            string streamingAssetsPath = Application.streamingAssetsPath;

            if (!Directory.Exists(streamingAssetsPath))
            {
                return;
            }
            List <FileInfo> fileInfos = FileTools.GetAllFileInfos(streamingAssetsPath);

            if (null == fileInfos)
            {
                return;
            }
            List <FileInfo> dbFileInfos = new List <FileInfo>();
            int             count       = fileInfos.Count;

            for (int i = 0; i < count; i++)
            {
                if (fileInfos[i].Extension == ".db")
                {
                    dbFileInfos.Add(fileInfos[i]);
                }
            }

            SQLite3Data data     = Resources.Load <SQLite3Data>("Sqlite3Data");
            string      dataPath = AssetDatabase.GetAssetPath(data);

            data = ScriptableObject.CreateInstance <SQLite3Data>();
            int dbCount = dbFileInfos.Count;

            data.AllData = new List <SQLite3SingleData>(dbCount);
            for (int i = 0; i < dbCount; i++)
            {
                SQLite3SingleData singleData = new SQLite3SingleData
                {
                    Name      = dbFileInfos[i].Name,
                    LocalName = MD5Tools.GetStringMd5(dbFileInfos[i].Name),
                    Md5       = MD5Tools.GetFileMd5(dbFileInfos[i].FullName)
                };
                string dirPath = dbFileInfos[i].DirectoryName;
                singleData.Directory = string.IsNullOrEmpty(dirPath) || dirPath == streamingAssetsPath
                    ? string.Empty
                    : dirPath.Replace('\\', '/').Replace(streamingAssetsPath, string.Empty);

                data.AllData.Add(singleData);
            }

            AssetDatabase.CreateAsset(data, dataPath);
            AssetDatabase.SaveAssets();
        }
        private static void BuildAudio()
        {
            string streamPath = Path.Combine(Application.streamingAssetsPath,
                                             EditorUserBuildSettings.activeBuildTarget.ToString(), "Audio");

            if (Directory.Exists(streamPath))
            {
                Directory.Delete(streamPath, true);
            }
            Directory.CreateDirectory(streamPath);

            DirectoryInfo dirInfo = new DirectoryInfo(AudioConfig.AUDIO_RES_ASSETS_PATH_S);

            FileInfo[] infos  = dirInfo.GetFiles();
            int        length = infos.Length;

            AssetBundleBuild[] levelDataList = new AssetBundleBuild[length];
            for (int i = 0; i < length; i++)
            {
                if (infos[i].Extension.Equals(".meta"))
                {
                    continue;
                }

                string        filename = Path.GetFileNameWithoutExtension(infos[i].Name).ToLower();
                AssetImporter importer = AssetImporter.GetAtPath(Path.Combine(AudioConfig.AUDIO_RES_ASSETS_PATH_S, infos[i].Name));
                importer.assetBundleName = filename;

                levelDataList[i] = new AssetBundleBuild
                {
                    assetBundleName = MD5Tools.GetStringMd5(filename),
                    assetNames      = new[] { importer.assetPath }
                };
            }

            BuildPipeline.BuildAssetBundles(streamPath, levelDataList,
                                            BuildAssetBundleOptions.ChunkBasedCompression,
                                            EditorUserBuildSettings.activeBuildTarget);

            WriteVersion(streamPath);

            AssetDatabase.Refresh();

            EditorUtility.DisplayDialog("Tips", "Build audio asset bundle completed.", "ok");
        }