static void Build(GameObject go) { if (go == null) return; BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle; UIAssetbundleInfo.Dependency dep = new UIAssetbundleInfo.Dependency(); dep.name = go.name; UIAssetbundleInfo.Dependency exist = UIAssetbundleInfo.instance.dependencies.Find(p => p.name == go.name); if (exist != null) UIAssetbundleInfo.instance.dependencies.Remove(exist); UIAssetbundleInfo.instance.dependencies.Add(dep); UISprite[] ws = go.GetComponentsInChildren<UISprite>(true); List<Texture> textures = new List<Texture>(); List<Material> materials = new List<Material>(); foreach (var w in ws) { Texture tex = w.atlas.texture; if (tex != null && !textures.Contains(tex)) { textures.Add(tex); } Material mat = w.atlas.spriteMaterial; if (mat!=null && !materials.Contains(mat)) materials.Add(mat); } //UITexture will use another strategy UILabel[] labels = go.GetComponentsInChildren<UILabel>(true); List<UIFont> uiFonts = new List<UIFont>(); List<Font> dynamicfonts = new List<Font>(); foreach (var label in labels) { UIFont uifont = label.font; if (uifont == null) continue; if (uifont.isDynamic) { Font f = uifont.dynamicFont; if (f != null && !dynamicfonts.Contains(f)) dynamicfonts.Add(f); } else { Texture tex = uifont.texture; if (tex != null && !textures.Contains(tex)) textures.Add(tex); Material mat = uifont.material; if (mat != null && !materials.Contains(mat)) materials.Add(mat); } } UIPlaySound[] sounds = go.GetComponentsInChildren<UIPlaySound>(true); List<AudioClip> clips = new List<AudioClip>(); foreach (var sound in sounds) { AudioClip audio = sound.audioClip; if (audio != null && !clips.Contains(sound.audioClip)) clips.Add(sound.audioClip); } ////////////////////////////////////////////////////////////////////////// BuildPipeline.PushAssetDependencies(); foreach (var tex in textures) { string path = AssetDatabase.GetAssetPath(tex.GetInstanceID()); //Debug.Log(UIAssetbundleSettings.buildTextureTargetPath); string fileName = "tex_" + tex.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildTextureTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.atlasPaths.Add(fileName); } foreach (var font in dynamicfonts) { string path = AssetDatabase.GetAssetPath(font.GetInstanceID()); string fileName = "font_" + font.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildFontTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.dynamicFontPaths.Add(fileName); } foreach (var clip in clips) { string path = AssetDatabase.GetAssetPath(clip.GetInstanceID()); string fileName ="audio_"+ clip.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildAudioTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.audioPaths.Add(fileName); } foreach (var mat in materials) { string path = AssetDatabase.GetAssetPath(mat.GetInstanceID()); string fileName = "mat_" + mat.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildMaterialTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.materialPaths.Add(fileName); } ////////////////////////////////////////////////////////////////////////// BuildPipeline.PushAssetDependencies(); string goPath = AssetDatabase.GetAssetPath(go.GetInstanceID()); BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(goPath), null, UIAssetbundleSettings.buildTargetPath + "/" + go.name + UIAssetbundleInfo.ext, options, UIAssetbundleSettings.instance.buildTarget); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); }
IEnumerator CoLoadUIAssetbundle(string name) { UIAssetbundleInfo.Dependency dep = UIAssetbundleInfo.instance.dependencies.Find(p => p.name == name); //first load texture if (dep.atlasPaths != null && dep.atlasPaths.Count > 0) { foreach (var path in dep.atlasPaths) { string texName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); string fullPath = UIAssetbundleInfo.texturePathUrl + "/" + path; AssetbundleEntry abe = loadedTextures.Find(p => p.name == texName); if (abe != null) { abe.refCount++; yield return(null); } else { using (WWW www = new WWW(fullPath)) { yield return(www); if (www.error != null) { throw new Exception("WWW download:" + www.error); } abe = new AssetbundleEntry(); abe.name = texName; abe.assetBundle = www.assetBundle; abe.refCount++; Debug.Log("add to textures"); loadedTextures.Add(abe); www.assetBundle.LoadAll(); } } } } //load font if (dep.dynamicFontPaths != null && dep.dynamicFontPaths.Count > 0) { foreach (var path in dep.dynamicFontPaths) { string fontName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); string fullPath = UIAssetbundleInfo.fontPathUrl + "/" + path; AssetbundleEntry abe = loadedFonts.Find(p => p.name == fontName); if (abe != null) { abe.refCount++; yield return(null); } else { using (WWW www = new WWW(fullPath)) { yield return(www); if (www.error != null) { throw new Exception("WWW download:" + www.error); } abe = new AssetbundleEntry(); abe.name = fontName; abe.assetBundle = www.assetBundle; abe.refCount++; loadedFonts.Add(abe); www.assetBundle.LoadAll(); } } } } //load audioClip if (dep.audioPaths != null && dep.audioPaths.Count > 0) { foreach (var path in dep.audioPaths) { string audioName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); string fullPath = UIAssetbundleInfo.audioPathUrl + "/" + path; AssetbundleEntry abe = loadedAudioClips.Find(p => p.name == audioName); if (abe != null) { abe.refCount++; yield return(null); } else { using (WWW www = new WWW(fullPath)) { yield return(www); if (www.error != null) { throw new Exception("WWW download:" + www.error); } abe = new AssetbundleEntry(); abe.name = audioName; abe.assetBundle = www.assetBundle; abe.refCount++; loadedAudioClips.Add(abe); www.assetBundle.LoadAll(); } } } } if (dep.materialPaths != null && dep.materialPaths.Count > 0) { foreach (var path in dep.materialPaths) { string materialName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); string fullPath = UIAssetbundleInfo.materialPathUrl + "/" + path; AssetbundleEntry abe = loadedMaterials.Find(p => p.name == materialName); if (abe != null) { abe.refCount++; yield return(null); } else { using (WWW www = new WWW(fullPath)) { yield return(www); if (www.error != null) { throw new Exception("WWW download:" + www.error); } abe = new AssetbundleEntry(); abe.name = materialName; abe.assetBundle = www.assetBundle; abe.refCount++; loadedMaterials.Add(abe); www.assetBundle.LoadAll(); } } } } //then load the prefab WWW bundle = new WWW(UIAssetbundleInfo.pathUrl + "/" + name + UIAssetbundleInfo.ext); yield return(bundle); GameObject go = Instantiate(bundle.assetBundle.mainAsset) as GameObject; go.name = name; go.transform.parent = root; go.transform.localScale = Vector3.one; //if i want create a lot,i should't unload go.transform.localPosition = Vector3.zero; //set UIAnchor's camera bundle.assetBundle.Unload(false); }
public void UnloadResource(string name) { UIAssetbundleInfo.Dependency dep = UIAssetbundleInfo.instance.dependencies.Find(p => p.name == name); if (dep.atlasPaths != null && dep.atlasPaths.Count > 0) { foreach (var path in dep.atlasPaths) { string texName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); AssetbundleEntry abe = loadedTextures.Find(p => p.name == texName); if (abe != null) { abe.refCount--; if (abe.refCount <= 0) { abe.assetBundle.Unload(true); loadedTextures.Remove(abe); Logger.Log("unload " + abe.name); } } } } if (dep.dynamicFontPaths != null && dep.dynamicFontPaths.Count > 0) { foreach (var path in dep.dynamicFontPaths) { string fontName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); AssetbundleEntry abe = loadedFonts.Find(p => p.name == fontName); if (abe != null) { abe.refCount--; if (abe.refCount <= 0) { abe.assetBundle.Unload(true); loadedFonts.Remove(abe); Logger.Log("unload " + abe.name); } } } } if (dep.audioPaths != null && dep.audioPaths.Count > 0) { foreach (var path in dep.audioPaths) { string audioName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); AssetbundleEntry abe = loadedAudioClips.Find(p => p.name == audioName); if (abe != null) { abe.refCount--; if (abe.refCount <= 0) { abe.assetBundle.Unload(true); loadedAudioClips.Remove(abe); Logger.Log("unload " + abe.name); } } } } if (dep.materialPaths != null && dep.materialPaths.Count > 0) { foreach (var path in dep.materialPaths) { string audioName = path.Substring(0, path.Length - UIAssetbundleInfo.ext.Length); AssetbundleEntry abe = loadedMaterials.Find(p => p.name == audioName); if (abe != null) { abe.refCount--; if (abe.refCount <= 0) { abe.assetBundle.Unload(true); loadedMaterials.Remove(abe); Logger.Log("unload " + abe.name); } } } } }
static void Build(GameObject go) { if (go == null) { return; } BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle; UIAssetbundleInfo.Dependency dep = new UIAssetbundleInfo.Dependency(); dep.name = go.name; UIAssetbundleInfo.Dependency exist = UIAssetbundleInfo.instance.dependencies.Find(p => p.name == go.name); if (exist != null) { UIAssetbundleInfo.instance.dependencies.Remove(exist); } UIAssetbundleInfo.instance.dependencies.Add(dep); UISprite[] ws = go.GetComponentsInChildren <UISprite>(true); List <Texture> textures = new List <Texture>(); List <Material> materials = new List <Material>(); foreach (var w in ws) { Texture tex = w.atlas.texture; if (tex != null && !textures.Contains(tex)) { textures.Add(tex); } Material mat = w.atlas.spriteMaterial; if (mat != null && !materials.Contains(mat)) { materials.Add(mat); } } //UITexture will use another strategy UILabel[] labels = go.GetComponentsInChildren <UILabel>(true); List <UIFont> uiFonts = new List <UIFont>(); List <Font> dynamicfonts = new List <Font>(); foreach (var label in labels) { UIFont uifont = label.font; if (uifont == null) { continue; } if (uifont.isDynamic) { Font f = uifont.dynamicFont; if (f != null && !dynamicfonts.Contains(f)) { dynamicfonts.Add(f); } } else { Texture tex = uifont.texture; if (tex != null && !textures.Contains(tex)) { textures.Add(tex); } Material mat = uifont.material; if (mat != null && !materials.Contains(mat)) { materials.Add(mat); } } } UIPlaySound[] sounds = go.GetComponentsInChildren <UIPlaySound>(true); List <AudioClip> clips = new List <AudioClip>(); foreach (var sound in sounds) { AudioClip audio = sound.audioClip; if (audio != null && !clips.Contains(sound.audioClip)) { clips.Add(sound.audioClip); } } ////////////////////////////////////////////////////////////////////////// BuildPipeline.PushAssetDependencies(); foreach (var tex in textures) { string path = AssetDatabase.GetAssetPath(tex.GetInstanceID()); //Debug.Log(UIAssetbundleSettings.buildTextureTargetPath); string fileName = "tex_" + tex.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildTextureTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.atlasPaths.Add(fileName); } foreach (var font in dynamicfonts) { string path = AssetDatabase.GetAssetPath(font.GetInstanceID()); string fileName = "font_" + font.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildFontTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.dynamicFontPaths.Add(fileName); } foreach (var clip in clips) { string path = AssetDatabase.GetAssetPath(clip.GetInstanceID()); string fileName = "audio_" + clip.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildAudioTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.audioPaths.Add(fileName); } foreach (var mat in materials) { string path = AssetDatabase.GetAssetPath(mat.GetInstanceID()); string fileName = "mat_" + mat.name + UIAssetbundleInfo.ext; BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(path), null, UIAssetbundleSettings.buildMaterialTargetPath + "/" + fileName, options, UIAssetbundleSettings.instance.buildTarget); dep.materialPaths.Add(fileName); } ////////////////////////////////////////////////////////////////////////// BuildPipeline.PushAssetDependencies(); string goPath = AssetDatabase.GetAssetPath(go.GetInstanceID()); BuildPipeline.BuildAssetBundle( AssetDatabase.LoadMainAssetAtPath(goPath), null, UIAssetbundleSettings.buildTargetPath + "/" + go.name + UIAssetbundleInfo.ext, options, UIAssetbundleSettings.instance.buildTarget); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); }