|
| 1 | +usingUnityEditor; |
| 2 | +usingUnityEngine; |
| 3 | +usingSystem.IO; |
| 4 | +usingSystem.IO.Compression; |
| 5 | +usingSystem.Collections.Generic; |
| 6 | + |
| 7 | +namespaceSanAndreasUnity.Editor |
| 8 | +{ |
| 9 | +[InitializeOnLoad] |
| 10 | +publicclassNavMeshFileExtractor |
| 11 | +{ |
| 12 | +privateconststringNavMeshesFolderPath="NavMeshes/"; |
| 13 | +privateconststringNavMeshesArchiveFileName="NavMeshes.zip"; |
| 14 | + |
| 15 | + |
| 16 | +staticNavMeshFileExtractor() |
| 17 | +{ |
| 18 | +stringfullFolderPath=Path.Combine(Application.dataPath,NavMeshesFolderPath); |
| 19 | + |
| 20 | +if(!Directory.Exists(fullFolderPath)) |
| 21 | +{ |
| 22 | +Debug.LogError("Failed to extract nav mesh files: folder with nav meshes not found"); |
| 23 | +return; |
| 24 | +} |
| 25 | + |
| 26 | +stringzipFilePath=Path.Combine(fullFolderPath,NavMeshesArchiveFileName); |
| 27 | +if(!File.Exists(zipFilePath)) |
| 28 | +return; |
| 29 | + |
| 30 | +Debug.Log("Attempting to extract nav mesh files"); |
| 31 | + |
| 32 | +varextractedFileNames=newList<string>(); |
| 33 | +longtotalFileSizeExtracted=0; |
| 34 | + |
| 35 | +usingvarzipArchive=newZipArchive(File.OpenRead(zipFilePath),ZipArchiveMode.Read); |
| 36 | +foreach(varentryinzipArchive.Entries) |
| 37 | +{ |
| 38 | +if(string.IsNullOrWhiteSpace(entry.Name)) |
| 39 | +continue; |
| 40 | + |
| 41 | +usingvarwriteStream=File.OpenWrite(Path.Combine(fullFolderPath,entry.Name)); |
| 42 | +usingvarentryStream=entry.Open(); |
| 43 | +entryStream.CopyTo(writeStream,4*1024*1024); |
| 44 | +writeStream.Flush(true); |
| 45 | + |
| 46 | +writeStream.Dispose(); |
| 47 | +entryStream.Dispose(); |
| 48 | + |
| 49 | +extractedFileNames.Add(entry.Name); |
| 50 | +totalFileSizeExtracted+=entry.Length; |
| 51 | +} |
| 52 | + |
| 53 | +// close archive so it can be deleted |
| 54 | +zipArchive.Dispose(); |
| 55 | + |
| 56 | +// delete zip file to reduce size of project, and to prevent it from being extracted again |
| 57 | +File.Delete(zipFilePath); |
| 58 | + |
| 59 | +// also delete it's meta file |
| 60 | +stringmetaFilePath=zipFilePath+".meta"; |
| 61 | +if(File.Exists(metaFilePath)) |
| 62 | +File.Delete(metaFilePath); |
| 63 | + |
| 64 | +AssetDatabase.Refresh(); |
| 65 | + |
| 66 | +Debug.Log($"Successfully extracted nav mesh files, total extracted files' size{totalFileSizeExtracted}, "+ |
| 67 | +$"files extracted:\n{string.Join("\n",extractedFileNames)}"); |
| 68 | +} |
| 69 | +} |
| 70 | +} |