static public Config DeserializeFromFile(string configPath) { if (BetterStreamingAssets.FileExists(configPath)) //File.Exists(configPath)) { if (Path.GetExtension(configPath) == ".json") { string json = BetterStreamingAssets.ReadAllText(configPath); return(Deserialize(json, "json")); } else if (Path.GetExtension(configPath) == ".xml") { string xml = File.ReadAllText(configPath); return(Deserialize(xml, "xml")); } else { Debug.LogError("the configuration file format is not supported"); throw new FormatException(); } } else { Debug.LogError("configuration file not found"); throw new FileNotFoundException(); } }
public void TestFilesInProjectExistInStreamingAssets() { var files = GetRealFiles("/", null, SearchOption.AllDirectories); foreach (var f in files) { Assert.IsTrue(BetterStreamingAssets.FileExists(f)); } Assert.IsFalse(BetterStreamingAssets.FileExists("FileThatShouldNotExist")); }
/// <summary> /// 是否存在文件 /// </summary> /// <returns></returns> static private bool IsExsitAsset(string filePath) { if (isUseSysIO) { return(File.Exists(filePath)); } else { return(BetterStreamingAssets.FileExists(filePath)); } }
private void LoadTargetData() { if (BetterStreamingAssets.FileExists(gameDataFileName)) { loadedData = JsonUtility.FromJson <TargetData>(BetterStreamingAssets.ReadAllText(gameDataFileName)); targets = loadedData.targets; print("loadtargetdata successfull"); } else { Debug.LogError("Cannot load game data!"); } }
private void Start() { Settings.PlayMode = 3; BetterStreamingAssets.Initialize(); bool easy_exist = BetterStreamingAssets.FileExists("/SudokuSample/easy.txt"); bool medium_exist = BetterStreamingAssets.FileExists("/SudokuSample/medium.txt"); bool hard_exist = BetterStreamingAssets.FileExists("/SudokuSample/hard.txt"); if (!easy_exist || !medium_exist || !hard_exist) { Settings.PlayMode = 0; return; } string easy_all_text = BetterStreamingAssets.ReadAllText("/SudokuSample/easy.txt"); string medium_all_text = BetterStreamingAssets.ReadAllText("/SudokuSample/medium.txt"); string hard_all_text = BetterStreamingAssets.ReadAllText("/SudokuSample/hard.txt"); string[] easy_maps = easy_all_text.Split('\n'); string[] medium_maps = medium_all_text.Split('\n'); string[] hard_maps = hard_all_text.Split('\n'); int easy_cnt = 0; foreach (var map in easy_maps) { fileManager.StartSaving($"easy{easy_cnt++}", map); } Settings.Easy_Cnt = easy_cnt; int medium_cnt = 0; foreach (var map in medium_maps) { fileManager.StartSaving($"medium{medium_cnt++}", map); } Settings.Medium_Cnt = medium_cnt; int hard_cnt = 0; foreach (var map in hard_maps) { fileManager.StartSaving($"hard{hard_cnt++}", map); } Settings.Hard_Cnt = hard_cnt; print($"Installed {easy_cnt}, {medium_cnt}, {hard_cnt}"); }
/// <summary> /// 是否存在资源文件 /// </summary> /// <param name="platform"></param> /// <param name="serverAsset"></param> /// <returns></returns> static public bool IsExsitAsset(RuntimePlatform platform, string assetName, string assetHashName) { //本地是否下载过hash文件(之前下到一半就中止了) var persistentHashPath = IPath.Combine(BApplication.persistentDataPath, BApplication.GetPlatformPath(platform), assetHashName); if (File.Exists(persistentHashPath)) { var hash = FileHelper.GetMurmurHash3(persistentHashPath); if (assetHashName.Equals(hash)) { return(true); } else { File.Delete(persistentHashPath); } } //persistent判断 var persistentAssetPath = IPath.Combine(BApplication.persistentDataPath, BApplication.GetPlatformPath(platform), assetName); if (File.Exists(persistentAssetPath)) { return(true); } /************母包资源的判断*************/ if (Application.isEditor && BDLauncher.Inst.GameConfig.ArtRoot == AssetLoadPathType.DevOpsPublish) { //devops var devopsAssetPath = IPath.Combine(BApplication.DevOpsPublishAssetsPath, BApplication.GetPlatformPath(platform), assetName); if (File.Exists(devopsAssetPath)) { return(true); } } else { //Streaming 文件判断,无需Streaming前缀 var streamingAssetPath = IPath.Combine(BApplication.GetPlatformPath(platform), assetName); if (BetterStreamingAssets.FileExists(streamingAssetPath)) { return(true); } } return(false); }
/// <summary> /// 加载本地的Asset.info /// </summary> private List <AssetItem> LoadLocalAssetInfo(RuntimePlatform platform) { var retList = new List <AssetItem>(); //优先加载persistent的Assets.info var persistentAssetInfoPath = BResources.GetAssetsInfoPath(BApplication.persistentDataPath, platform); if (File.Exists(persistentAssetInfoPath)) { var content = File.ReadAllText(persistentAssetInfoPath); retList = CsvSerializer.DeserializeFromString <List <AssetItem> >(content); } //streaming 和其他的Assets.info else { //根据加载模式不同,寻找不同目录下的其他配置 //打包时,本地会带一份ServerAssets.info以标记当前包携带的资源 var loadArtRoot = BDLauncher.Inst.GameConfig.ArtRoot; switch (loadArtRoot) { case AssetLoadPathType.Persistent: case AssetLoadPathType.StreamingAsset: { //TODO :BSA 读取,不需要Streaming前缀 var steamingAssetsInfoPath = IPath.Combine(BApplication.GetPlatformPath(platform), BResources.ASSETS_INFO_PATH); //var steamingAssetsInfoPath = GetAssetsInfoPath(BDApplication.streamingAssetsPath, platform); if (BetterStreamingAssets.FileExists(steamingAssetsInfoPath)) { var content = BetterStreamingAssets.ReadAllText(steamingAssetsInfoPath); retList = CsvSerializer.DeserializeFromString <List <AssetItem> >(content); } } break; case AssetLoadPathType.DevOpsPublish: { var path = GameConfig.GetLoadPath(loadArtRoot); var devopsAssetInfoPath = BResources.GetAssetsInfoPath(path, platform); if (File.Exists(devopsAssetInfoPath)) { var content = File.ReadAllText(devopsAssetInfoPath); retList = CsvSerializer.DeserializeFromString <List <AssetItem> >(content); } } break; } } return(retList); }
public void LoadExpressions() { if (images == null) { images = new Dictionary <string, Sprite>(); } string nameWithoutAccentuation; foreach (var expressao in expressoes) { if (images.ContainsKey(expressao)) { continue; } if (nome == "André") { nameWithoutAccentuation = "Andre"; } else if (nome == "Valéria") { nameWithoutAccentuation = "Valeria"; } else { nameWithoutAccentuation = nome; } var filePath = CharacterImageLocation + "/" + nameWithoutAccentuation + "/" + expressao + ".png"; //Get path of folder Debug.Log(filePath); if (!BetterStreamingAssets.FileExists(filePath)) { continue; } Texture2D tex = new Texture2D(2, 2); using (var stream = BetterStreamingAssets.OpenRead(filePath)) { MemoryStream ms = new MemoryStream(); stream.CopyTo(ms); tex.LoadImage(ms.ToArray()); images[expressao] = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); } } }
private void loadLanguages() { if (!BetterStreamingAssets.FileExists("/lang/" + nameLanguagesFile + ".json")) { Debug.LogErrorFormat("Streaming asset not found: {0}", "/lang/" + nameLanguagesFile + ".json"); } else { json = BetterStreamingAssets.ReadAllText("/lang/" + nameLanguagesFile + ".json"); lang = JsonUtility.FromJson <Languages>(json); if (interfaceLanguages != null) { interfaceLanguages.onChangeLanguage(); } } }
//Called by Central public void LoadLanguage(string languageCode) { string path = "Languages/" + languageCode + ".xml"; if (!BetterStreamingAssets.FileExists(path)) { Debug.LogErrorFormat("Streaming asset not found: {0}", path); } else { using (var stream = BetterStreamingAssets.OpenRead(path)) { var serializer = new System.Xml.Serialization.XmlSerializer(typeof(LanguageContainer)); LoadedLanguage = (LanguageContainer)serializer.Deserialize(stream); } OnNewLanguageLoaded.Invoke(); } }
public Texture2D LoadTexture(string FilePath) { // Load a PNG or JPG file from disk to a Texture2D // Returns null if load fails Texture2D Tex2D; byte[] FileData; if (BetterStreamingAssets.FileExists(FilePath)) { FileData = BetterStreamingAssets.ReadAllBytes(FilePath); Tex2D = new Texture2D(2, 2); // Create new "empty" texture if (Tex2D.LoadImage(FileData)) // Load the imagedata into the texture (size is set automatically) { return(Tex2D); // If data = readable -> return texture } } return(null); // Return null if load failed }
public static void init() { if (!isInitialized) { if (!BetterStreamingAssets.FileExists(questionsPath)) { Debug.Log("Could not find questions file!"); throw new FileNotFoundException("Could not find questions file!"); } string[] questionsFile = BetterStreamingAssets.ReadAllLines(questionsPath); string[] csvQuestions = String.Join(";", questionsFile).Split('#'); List <Question> questionList = new List <Question>(); foreach (string questionString in csvQuestions) { questionList.Add(new Question(questionString)); } questionList.Shuffle(); questions = new Queue <Question>(questionList); isInitialized = true; } }
private void LoadGameData() { // Path.Combine combines strings into a file path // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build //string filePath = Path.Combine(Application.streamingAssetsPath, gameDataFileName); if (BetterStreamingAssets.FileExists(gameDataFileName)) { // Read the json from the file into a string //string dataAsJson = File.ReadAllText(filePath); // Pass the json to JsonUtility, and tell it to create a GameData object from it //GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson); loadedData = JsonUtility.FromJson <GameData>(BetterStreamingAssets.ReadAllText(gameDataFileName)); // Retrieve the allRoundData property of loadedData Debug.Log("gameDataFileName"); Debug.Log(gameDataFileName); allRoundData = loadedData.allRoundData; } else { Debug.LogError("Cannot load game data!"); } }
private void initMaps() { string emptyPath = "/Empty/empty.png"; if (!BetterStreamingAssets.FileExists(emptyPath)) { Debug.Log("Missing empty sprite!"); } typeToRenderer.Add("Eyebrow", eyebrowRenderer); typeToRenderer.Add("Shirt", shirtRenderer); typeToRenderer.Add("Body", bodyRenderer); typeToRenderer.Add("Overall", overallRenderer); typeToRenderer.Add("Shoes", shoesRenderer); typeToRenderer.Add("Pants", pantsRenderer); typeToRenderer.Add("Hair", hairRenderer); typeToRenderer.Add("Helmet", helmetRenderer); typeToRenderer.Add("FacialFeatures", facialfeaturesRenderer); typeToRenderer.Add("FacialHair", facialhairRenderer); typeToRenderer.Add("Headpiece", headpieceRenderer); typeToRenderer.Add("Handheld", handheldRenderer); typeToRenderer.Add("Makeup", makeupRenderer); typeToRenderer.Add("Neckwear", neckwearRenderer); typeToRenderer.Add("Wings", wingsRenderer); typeToRenderer.Add("Eyelids", wingsRenderer); foreach (string type in typeToRenderer.Keys) { typeToCurrentIndex[type] = 0; elementTypeMap.Add(type, new List <string>()); if (type != "Body") { elementTypeMap[type].Add(emptyPath); } } }
/// <summary> /// 是否存在资源.并且校验hash /// </summary> /// <param name="platform"></param> /// <param name="serverAsset"></param> /// <returns></returns> static public bool IsExsitAssetWithCheckHash(RuntimePlatform platform, string assetName, string assetHash) { //本地是否下载过hash文件(之前下到一半就中止了),hash文件只会在 var persistentHashPath = IPath.Combine(BApplication.persistentDataPath, BApplication.GetPlatformPath(platform), assetHash); if (File.Exists(persistentHashPath)) { var hash = FileHelper.GetMurmurHash3(persistentHashPath); if (assetHash.Equals(hash)) { BDebug.Log($"hash文件存在 - {assetName} | hash - {assetHash}"); return(true); } else { File.Delete(persistentHashPath); } } //persistent判断 var persistentAssetPath = IPath.Combine(BApplication.persistentDataPath, BApplication.GetPlatformPath(platform), assetName); if (File.Exists(persistentAssetPath)) { var hash = FileHelper.GetMurmurHash3(persistentAssetPath); if (assetHash.Equals(hash)) { BDebug.Log($"persistent存在 - {assetName} | hash - {assetHash}"); return(true); } } /************母包资源的判断*************/ if (Application.isEditor && BDLauncher.Inst.GameConfig.ArtRoot == AssetLoadPathType.DevOpsPublish) { //devops var devopsAssetPath = IPath.Combine(BApplication.DevOpsPublishAssetsPath, BApplication.GetPlatformPath(platform), assetName); if (File.Exists(devopsAssetPath)) { var hash = FileHelper.GetMurmurHash3(devopsAssetPath); if (assetHash.Equals(hash)) { BDebug.Log($"devops存在 - {assetName} | hash - {assetHash}"); return(true); } } } else { //Streaming 文件判断,无需Streaming前缀 var streamingAssetPath = IPath.Combine(BApplication.GetPlatformPath(platform), assetName); if (BetterStreamingAssets.FileExists(streamingAssetPath)) { var bytes = BetterStreamingAssets.ReadAllBytes(streamingAssetPath); var hash = FileHelper.GetMurmurHash3(bytes); if (assetHash.Equals(hash)) { BDebug.Log($"streaming存在 - {assetName} | hash - {assetHash}"); return(true); } } } return(false); }
private IEnumerator TestHarness(ReadMode readMode, string path, TestType testType, int attempts, TestResultDelegate callback) { var stopwatch = new Stopwatch(); string[] assetNames = null; var streamingAssetsUrl = Path.Combine(StreamingAssetsPath, path).Replace('\\', '/'); long bytesRead = 0; long maxMemoryPeak = 0; long totalMemoryPeaks = 0; for (int i = 0; i < attempts; ++i) { WWW www = null; yield return(Resources.UnloadUnusedAssets()); GC.Collect(); GC.WaitForPendingFinalizers(); yield return(null); var memoryUnityBefore = Profiler.GetTotalAllocatedMemoryLong(); //var memoryMonoBefore = Profiler.GetMonoUsedSizeLong(); stopwatch.Start(); if (readMode == ReadMode.WWW) { www = new WWW(streamingAssetsUrl); { yield return(www); Profiler.BeginSample(testType.ToString()); switch (testType) { case TestType.CheckIfExists: if (!string.IsNullOrEmpty(www.error)) { throw new System.Exception(www.error); } break; case TestType.LoadBytes: bytesRead += www.bytes.Length; break; default: throw new NotSupportedException(); } Profiler.EndSample(); } } else if (readMode == ReadMode.BSA) { Profiler.BeginSample(testType.ToString()); switch (testType) { case TestType.CheckIfExists: if (!BetterStreamingAssets.FileExists(path)) { throw new System.InvalidOperationException(); } break; case TestType.LoadBytes: bytesRead += BetterStreamingAssets.ReadAllBytes(path).Length; break; } Profiler.EndSample(); } else if (readMode == ReadMode.Direct) { var p = streamingAssetsUrl; Profiler.BeginSample(testType.ToString()); switch (testType) { case TestType.CheckIfExists: if (!File.Exists(p)) { throw new System.InvalidOperationException(); } break; case TestType.LoadBytes: bytesRead += File.ReadAllBytes(p).Length; break; } Profiler.EndSample(); } stopwatch.Stop(); var memoryPeak = Math.Max(0, Profiler.GetTotalAllocatedMemoryLong() - memoryUnityBefore); // + Math.Max(0, Profiler.GetMonoUsedSizeLong() - memoryMonoBefore); maxMemoryPeak = System.Math.Max(memoryPeak, maxMemoryPeak); totalMemoryPeaks += memoryPeak; yield return(null); if (www != null) { www.Dispose(); } yield return(null); } yield return(Resources.UnloadUnusedAssets()); GC.Collect(); GC.WaitForPendingFinalizers(); yield return(null); callback(new TimeSpan(stopwatch.ElapsedTicks / attempts), bytesRead / attempts, totalMemoryPeaks / attempts, maxMemoryPeak, assetNames); }