制作 public static void CreateAssetBunldes() { Object[] SelectedAsset= Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); foreach (Object obj in SelectedAsset) { string sourcePath = AssetDatabase.GetAssetPath(obj); string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle"; if (File.Exists(targetPath)) File.Delete(targetPath); if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone)) { Debug.Log(obj.name + "资源打包成功"); } else { Debug.Log(obj.name + "资源打包失败"); } } } //访问 拷贝 加载 public GameObject mainui; public GameObject uiroot; void Start() { string dataPath = Application.streamingAssetsPath; string path = dataPath + "/"+ "MainUi.assetbundle"; Debug.Log ("path ---" + path); if (File.Exists (path)) { dataPath = Application.persistentDataPath; string PathURL = dataPath + "/Raw"; if (!Directory.Exists (PathURL)) Directory.CreateDirectory(PathURL); PathURL += "/MainUi.assetbundle"; Debug.Log ("PathURL ---" + path); if(!File.Exists(PathURL)) File.Create(PathURL); File.Copy(path, PathURL,true); Debug.Log ("move ok......"); } else { Debug.Log("file not exits...."); } // } void Update() { } void OnGUI() { if (GUI.Button (new Rect (10, 10, 100, 100), "open ui")) { Destroy(mainui); // mainui = Instantiate(Resources.Load("MainUi")) as GameObject; // if(mainui != null && uiroot != null) // { // mainui.transform.parent = uiroot.transform; // mainui.transform.localPosition = Vector3.zero; // mainui.transform.localScale = Vector3.one; // } StartCoroutine(DownloadAssetAndScene()); } if (GUI.Button (new Rect (10, 110, 100, 100), "close ui")) { if(mainui !=null) { Destroy(mainui); } } } IEnumerator DownloadAssetAndScene() { //下载assetbundle,加载Cube // string Path = Application.streamingAssetsPath + "/MainUi.assetbundle"; //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。 // string PathURL = // #if UNITY_ANDROID // "jar:file://" + Application.dataPath + "!/assets/MainUi.assetbundle"; // #elif UNITY_IPHONE // Application.persistentDataPath + "/StreamingAssets/MainUi.assetbundle"; // #elif UNITY_STANDALONE_WIN || UNITY_EDITOR // "file://" + Application.dataPath + "/StreamingAssets/MainUi.assetbundle"; // #else // string.Empty; // #endif string dataPath = Application.persistentDataPath; string PathURL = @"file://" + dataPath + "/Raw/"+ "MainUi.assetbundle"; Debug.Log ("path ==" + PathURL); using (WWW asset = new WWW(PathURL)) { yield return asset; if(string.IsNullOrEmpty(asset.error)) { AssetBundle bundle = asset.assetBundle; mainui = Instantiate(bundle.Load("MainUi")) as GameObject; if(mainui != null && uiroot != null) { mainui.transform.parent = uiroot.transform; mainui.transform.localPosition = Vector3.zero; mainui.transform.localScale = Vector3.one; } bundle.Unload(false); }else { Debug.Log(asset.error); } } }
|