using UnityEngine; using UnityEditor; using System.IO; public class FileExporter :…
- User
using UnityEngine; using UnityEditor; using System.IO;
public class FileExporter : MonoBehaviour { public string bundlePath; // Path to the .bundle file public string saveDirectory; // Directory to save the files
void Start() { // Load the AssetBundle AssetBundle bundle = AssetBundle.LoadFromFile(bundlePath); // Check if the AssetBundle is loaded successfully if (bundle != null) { // Get all asset names in the AssetBundle string[] assetNames = bundle.GetAllAssetNames(); // Iterate over each asset name foreach (string assetName in assetNames) { // Load the file from the AssetBundle as an Object Object fileObject = bundle.LoadAsset(assetName); // Convert the Object to a byte array byte[] fileBytes = fileObject != null ? EditorUtilityExtensions.GetBytesFromObject(fileObject) : null; if (fileBytes != null) { // Create the save directory if it doesn’t exist Directory.CreateDirectory(saveDirectory); // Set the save path for the file string savePath = Path.Combine(saveDirectory, Path.GetFileName(assetName)); // Write the file to disk File.WriteAllBytes(savePath, fileBytes); Debug.Log("File saved: " + assetName); } else { Debug.LogError("Failed to load file from AssetBundle: " + assetName); } } bundle.Unload(false); // Unload the AssetBundle } else { Debug.LogError("Failed to load AssetBundle."); } }}
public static class EditorUtilityExtensions { public static byte[] GetBytesFromObject(Object obj) { string tempPath = Path.GetTempFileName();
bool success = AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath, ExportPackageOptions.ExportDependencies); if (success) { return File.ReadAllBytes(tempPath); } return null; }}
Assets\Scripts\FileExporter.cs(65,116): error CS0117: 'ExportPackageOptions' does not contain a definition for 'ExportDependencies'
- Assistant
The error message suggests that the 'ExportPackageOptions' enum does not contain a definition for 'ExportDependencies'.
This issue may occur due to using an older version of Unity where the 'ExportPackageOptions' enum does not have the 'ExportDependencies' option.
To resolve this error, you can try the following solutions:
-
Update Unity: Make sure you are using the latest version of Unity. 'ExportDependencies' option was introduced in Unity 5.0, so if you are using an older version, consider updating Unity to a newer version.
-
Remove 'ExportDependencies' option: If updating Unity is not feasible, you can remove the 'ExportDependencies' option from the 'EditorUtilityExtensions' class. Modify the 'GetBytesFromObject' method to the following:
public static byte[] GetBytesFromObject(Object obj) { string tempPath = Path.GetTempFileName(); bool success = AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath); if (success) { return File.ReadAllBytes(tempPath); } return null; }By removing the 'ExportDependencies' option, the error should be resolved. However, please note that this might cause issues if your asset has dependencies that need to be exported as well.
Make sure to #ifdef the line like this:
bool success = true; #if UNITY_2018_4_OR_NEWER success = AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath, ExportPackageOptions.ExportDependencies); #else success = AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath); #endif- Use a different approach: Instead of exporting the package, you can directly access the assets in the project and read their raw data using
AssetDatabase.LoadAssetAtPathandAssetDatabase.LoadAllAssetsAtPathmethods. Then, you can convert these assets to byte arrays and save them to files.
Here is an example of how to modify the code using this approach:
// Modify the GetBytesFromObject method to remove dependencies and use LoadAssetAtPath method public static byte[] GetBytesFromObject(Object obj) { if (obj is Texture2D) { Texture2D texture = obj as Texture2D; return texture.EncodeToPNG(); } else if (obj is AudioClip) { AudioClip audioClip = obj as AudioClip; return UnityEngine.AudioExtensionMethods.Compress(audioClip, AudioCompressionFormat.PCM); } else { Debug.LogError("Unsupported asset type: " + obj.GetType().ToString()); return null; } } // Replace the line that calls GetBytesFromObject with the following code // Load the file from the AssetBundle as an Object Object fileObject = bundle.LoadAsset(assetName); string assetPath = AssetDatabase.GetAssetPath(fileObject); byte[] fileBytes = fileObject != null ? GetBytesFromObject(fileObject) : null;By using the
AssetDatabase.LoadAssetAtPathmethod, you can directly access the asset in your project without the need to export the package.Choose the solution that best fits your needs and Unity version.
-
- User
Assets\Scripts\FileExporter.cs(65,24): error CS0029: Cannot implicitly convert type 'void' to 'bool'
- Assistant
The error message suggests that there is a type mismatch and you are trying to assign a
voidvalue to aboolvariable or expression.Looking at the code, the error is in the following line:
bool success = AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath, ExportPackageOptions.ExportDependencies);The
ExportPackagefunction from theAssetDatabaseclass returnsvoid, which means it doesn't return any value. However, you are trying to assign the return value to thesuccessvariable of typebool.To resolve this error, you need to remove the assignment and the
successvariable. Since you are already checking if the export was successful in the if statement, you don't need thesuccessvariable.Here's the modified
GetBytesFromObjectfunction:public static byte[] GetBytesFromObject(Object obj) { string tempPath = Path.GetTempFileName(); AssetDatabase.ExportPackage(AssetDatabase.GetAssetPath(obj), tempPath, ExportPackageOptions.ExportDependencies); return File.ReadAllBytes(tempPath); }With these changes, the error should be resolved.