/// <summary> /// 同步加载资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public T LoadAssets <T>(string path) where T : UnityEngine.Object { using (zstring.Block()) { zstring assetName = path.ToLower(); zstring ext = Path.GetExtension(assetName); if (!zstring.IsNullOrEmpty(ext)) { assetName = assetName.Replace(ext, ""); } AssetBundle bundle = TryGetBundleByFile(assetName); if (bundle == null) { return(null); } string assetRoot = GResource.RuntimeAssetsRoot.ToLower(); if (!assetName.StartsWith(assetRoot)) { assetName = zstring.Concat(assetRoot, path); } var asset = bundle.LoadAsset(assetName) as T; if (asset == null) { Debug.LogWarning(zstring.Format("Cant find ab: {0}", assetName)); } return(asset); } }
public Sprite LoadSprite(GameObject owner, string spriteName, string atlasName) { using (zstring.Block()) { zstring assetName = atlasName.ToLower(); zstring ext = Path.GetExtension(assetName); if (!string.IsNullOrEmpty(ext)) { assetName = assetName.Replace(ext, ""); } string bundleRelativePath = assetName; if (!assetbundleMap.TryGetValue(assetName, out bundleRelativePath)) { Debug.LogWarning(zstring.Format("{0} has no assetbundle resource", assetName)); return(null); } UIAtlasCache atlasCache = null; if (!atlasMap.TryGetValue(bundleRelativePath, out atlasCache)) { AssetBundle bundle = TryGetBundleByFile(assetName); if (bundle == null) { return(null); } var assets = bundle.LoadAllAssets <Sprite>(); if (assets == null) { Debug.LogWarning(zstring.Format("Cant find sprite: {0} in bundle {1}", spriteName, bundleRelativePath)); } atlasCache = new UIAtlasCache(assets); atlasMap[bundleRelativePath] = atlasCache; } Sprite sprite = atlasCache.GetSprite(spriteName); TextureWatcher watcher = owner.transform.GetOrAddComponent <TextureWatcher>(); watcher.AddBundleName(bundleRelativePath); return(sprite); } }
public Texture LoadTexture(GameObject owner, string assetPath) { using (zstring.Block()) { zstring assetName = assetPath.ToLower(); zstring ext = Path.GetExtension(assetName); if (!zstring.IsNullOrEmpty(ext)) { assetName = assetName.Replace(ext, ""); } AssetBundle bundle = TryGetBundleByFile(assetName); if (bundle == null) { return(null); } TextureWatcher watcher = owner.transform.GetOrAddComponent <TextureWatcher>(); string bundleName; if (assetbundleMap.TryGetValue(assetName, out bundleName)) { watcher.AddBundleName(bundleName); } string assetRoot = GResource.RuntimeAssetsRoot.ToLower(); if (!assetName.StartsWith(assetRoot)) { assetName = zstring.Concat(assetRoot, assetPath); } var asset = bundle.LoadAsset(assetName) as Texture; if (asset == null) { Debug.LogWarning(zstring.Format("Cant find ab: {0}", assetName)); } return(asset); } }
void zstringTest() { using (profiler.Sample("zstring")) { using (zstring.Block()) { using (profiler.Sample("Format")) { zstring gf = zstring.Format("Number = {0}, Float = {1} String = {2}", 123, 3.148f, "Text"); int x = 10; } using (profiler.Sample("Concat")) { zstring it = zstring.Concat("That's ", "a lot", " of", " strings", " to ", "concat"); int x = 10; } using (profiler.Sample("Substring + IndexOf + LastIndexOf")) { zstring path = "Path/To/Some/File.txt"; int period = path.IndexOf('.'); var ext = path.Substring(period + 1); var file = path.Substring(path.LastIndexOf('/') + 1, 4); int x = 10; } using (profiler.Sample("Replace (char)")) { zstring input = "This is some sort of text"; zstring replacement = input.Replace('o', '0').Replace('i', '1'); int x = 10; } using (profiler.Sample("Replace (string)")) { zstring input = "m_This is the is is form of text"; zstring replacement = input.Replace("m_", "").Replace("is", "si"); int x = 10; } using (profiler.Sample("Concat + Intern")) { for (int i = 0; i < 4; i++) { dict[zstring.Concat("Item", i).Intern()] = i; } outsideString1 = zstring.Concat("I'm ", "long ", "gone ", "by ", "the ", "end ", "of ", "this ", "gstring block"); outsideString = zstring.Concat("I'll ", "be ", "still ", "around ", "here").Intern(); int x = 10; } using (profiler.Sample("ToUpper + ToLower")) { zstring s1 = "Hello"; zstring s2 = s1.ToUpper(); zstring s3 = s2 + s1.ToLower(); int x = 10; } //using (profiler.Sample("Intern")) //{ // zstring s1 = "hello world"; // zstring s2 = s1 + UnityEngine.Random.Range(0, 10000); // string a = s2.Intern(); //} if (!bigStringTest) { return; } using (profiler.Sample("BigStringEval")) { zstring s1 = bigString; zstring s2 = s1 + "hello"; } } } }