01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class AssetBundleEditor : Editor { public static string sourcePath = Application.dataPath + "/Resource" ; private static string AssetBundlesOutputPath = Application.dataPath + "/StreamingAssets" ; [MenuItem( "AssetBundles/BuildAssetBundle" )] public static void BuildAssetBundle() { string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(BuildTarget.iOS)); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } //根据BuildSetting里面所激活的平台进行打包 设置过AssetBundleName的都会进行打包 BuildPipeline.BuildAssetBundles( "Assets/StreamingAssets/IOS" ,BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS); AssetDatabase.Refresh(); Debug.Log( "打包完成" ); } } public class Platform { public static string GetPlatformFolder(BuildTarget target) { switch (target) { case BuildTarget.Android: return "Android" ; case BuildTarget.iOS: return "IOS" ; case BuildTarget.StandaloneWindows: case BuildTarget.StandaloneWindows64: return "Windows" ; case BuildTarget.StandaloneOSXIntel: case BuildTarget.StandaloneOSXIntel64: case BuildTarget.StandaloneOSXUniversal: return "OSX" ; default : return null ; } } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using UnityEngine; using System.Collections; using System.Collections.Generic; public class LoadAssetBundle : MonoBehaviour { public string PathURL; public GameObject CurrentObj; void Start() { #if UNITY_IPHONE PathURL = "file://" +Application.dataPath + "/Raw/IOS" ; #elif UNITY_STANDALONE_WIN || UNITY_EDITOR PathURL = "file://" +Application.streamingAssetsPath+ "/IOS" ; #endif } void OnGUI() { if (GUI.Button( new Rect(100,100,100,100), "加载美少女" )) { StartCoroutine (GetAssetBundleObj( "girl_001" ,PathURL)); } } public IEnumerator GetAssetBundleObj( string objName, string path= "" ) { string filePath = System.IO.Path.Combine (path,objName); WWW w = new WWW(filePath); //利用www类加载 yield return w; AssetBundle curBundleObj = w.assetBundle; //获得AssetBundle AssetBundleRequest obj = curBundleObj.LoadAssetAsync(objName, typeof (GameObject)); //异步加载GameObject类型 yield return obj; CurrentObj = Instantiate(obj.asset) as GameObject; yield return null ; curBundleObj.Unload( false ); //卸载所有包含在bundle中的对象,已经加载的才会卸载 w.Dispose(); } } |
联系客服