### List Files and Check Directories Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Provides examples for listing files matching a pattern within the StreamingAssets directory and checking for the existence of directories. Supports recursive searching. ```csharp using System.IO; using UnityEngine; // List all XML files in StreamingAssets and its subdirectories string[] allXmlPaths = BetterStreamingAssets.GetFiles("\", "*.xml", SearchOption.AllDirectories); // List XML files only in the 'Config' directory and its subdirectories string[] configXmlPaths = BetterStreamingAssets.GetFiles("Config", "*.xml", SearchOption.AllDirectories); // Check if a directory exists Debug.Assert(BetterStreamingAssets.DirectoryExists("Config")); ``` -------------------------------- ### Read Data from Streaming Assets Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Demonstrates reading data from a file within the StreamingAssets directory. It includes error handling for missing files and provides examples for reading as a stream or all bytes at once. Paths are relative to the StreamingAssets folder. ```csharp using System.IO; using UnityEngine; public static class StreamingAssetReader { // Example for deserializing from XML public static Foo ReadFromXml(string path) { if (!BetterStreamingAssets.FileExists(path)) { Debug.LogErrorFormat("Streaming asset not found: {0}", path); return null; } using (var stream = BetterStreamingAssets.OpenRead(path)) { var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Foo)); return (Foo)serializer.Deserialize(stream); } } // Example for reading all bytes public static byte[] ReadAllBytesData(string path) { return BetterStreamingAssets.ReadAllBytes(path); } // Example for reading as a stream and seeking public static byte[] ReadStreamFooter(string path, int bytesToRead = 10) { byte[] footer = new byte[bytesToRead]; using (var stream = BetterStreamingAssets.OpenRead(path)) { stream.Seek(-footer.Length, SeekOrigin.End); stream.Read(footer, 0, footer.Length); } return footer; } } // Dummy class for XML deserialization example public class Foo { } ``` -------------------------------- ### Load Asset Bundles Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Demonstrates how to load Asset Bundles from the StreamingAssets directory, both synchronously and asynchronously. Asset bundle loading requires the main thread. ```csharp using UnityEngine; // Synchronous loading (main thread only) AssetBundle bundle = BetterStreamingAssets.LoadAssetBundle("MyAssetBundle"); // Asynchronous loading (main thread only) AssetBundleCreateRequest bundleOp = BetterStreamingAssets.LoadAssetBundleAsync("MyAssetBundle"); // You can then use bundleOp.assetBundle when it's ready. ``` -------------------------------- ### BetterStreamingAssets API Reference Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Comprehensive API documentation for the BetterStreamingAssets class, covering file operations, directory management, asset bundle loading, and Android-specific configurations. All methods operate on paths relative to the StreamingAssets directory. ```APIDOC BetterStreamingAssets: Initialize() - Initializes the plugin. Must be called on the main thread. FileExists(string path) - Checks if a file exists in StreamingAssets. - Parameters: - path: The relative path to the file. - Returns: bool - True if the file exists, false otherwise. OpenRead(string path) - Opens a file in StreamingAssets for reading. - Parameters: - path: The relative path to the file. - Returns: Stream - A readable stream for the file. - Note: Must be called on the main thread. ReadAllBytes(string path) - Reads the entire content of a file in StreamingAssets into a byte array. - Parameters: - path: The relative path to the file. - Returns: byte[] - The content of the file. - Note: Must be called on the main thread. GetFiles(string searchPattern, SearchOption searchOption) - Gets the names of files in the StreamingAssets directory that match a specified search pattern and option. - Parameters: - searchPattern: The search string to match against file names (e.g., "*.txt"). - searchOption: One of the enumeration values that specifies whether the search operation includes subdirectories. - Returns: string[] - An array of the full paths to files. - Note: Must be called on the main thread. DirectoryExists(string path) - Checks if a directory exists in StreamingAssets. - Parameters: - path: The relative path to the directory. - Returns: bool - True if the directory exists, false otherwise. LoadAssetBundle(string path) - Loads an AssetBundle synchronously from StreamingAssets. - Parameters: - path: The relative path to the AssetBundle file. - Returns: AssetBundle - The loaded AssetBundle. - Note: Must be called on the main thread. LoadAssetBundleAsync(string path) - Loads an AssetBundle asynchronously from StreamingAssets. - Parameters: - path: The relative path to the AssetBundle file. - Returns: AssetBundleCreateRequest - An object that can be used to track the asynchronous loading operation. - Note: Must be called on the main thread. IsAndroidCompressedStreamingAsset (event) - An event that allows custom logic to determine if a file is a compressed Streaming Asset on Android. - Signature: `Func IsAndroidCompressedStreamingAsset` - The delegate receives the file path and should return `false` if the file is NOT a Streaming Asset to prevent false positive warnings. AndroidIsCompressedFileStreamingAsset (partial method) - A partial method that can be implemented to provide custom logic for identifying compressed Streaming Assets on Android. - Signature: `static partial void AndroidIsCompressedFileStreamingAsset(string path, ref bool result)` - Implement this method to set `result` to `false` for files that are not Streaming Assets. ``` -------------------------------- ### Initialize BetterStreamingAssets Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Initializes the BetterStreamingAssets plugin. This method must be called on the main thread before any other operations are performed. ```csharp BetterStreamingAssets.Initialize(); ``` -------------------------------- ### Handle Android Compressed Assets Source: https://github.com/gwiazdorrr/betterstreamingassets/blob/master/README.md Provides mechanisms to manage false-positive warnings about compressed Streaming Assets on Android. Developers can hook into events or implement partial methods to exclude specific files from being flagged. ```csharp // Using an event handler BetterStreamingAssets.IsAndroidCompressedStreamingAsset += (path) => { if (path == "assets/my_custom_plugin_settings.json") { return false; // This file is not a Streaming Asset } return true; // Assume it's a Streaming Asset, will log assertion if compressed }; // Using a partial method implementation /* partial class BetterStreamingAssets { static partial void AndroidIsCompressedFileStreamingAsset(string path, ref bool result) { if (path == "assets/my_custom_plugin_settings.json") { result = false; } } } */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.