查看: 809|回复: 0
打印 上一主题 下一主题

[其他] 分包加载

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-2-28 20:28:00 |只看该作者 |倒序浏览
分包加载
  1. AssetbundleLoad.js脚本

  2. -------------------------------------------------------------

  3. var house : GameObject;

  4. var carpet : Object;

  5. var mirror : GameObject;

  6. var textures : GameObject;

  7. var mainobject : GameObject;

  8. var names : String;

  9. function Start()

  10. {

  11. var www=new WWW(“http://www.xxx.com/test/AssetBundle.unity3d”); //从服务器加载

  12. //var www=new WWW(“file:///C:/1/AssetBundle.unity3d”); //从本地加载,绝对路径

  13. yield www;

  14. //一起选择Room,Carpet和Mirror的Prefab后导出,那么Room会成为mainAsset ,其他的用AssetBundle.Load(“name”)来加载

  15. //mainobject=Instantiate(www.assetBundle.mainAsset);

  16. house=Instantiate(www.assetBundle.Load(“Room”));

  17. carpet=Instantiate(www.assetBundle.Load(“asd”)); //奇怪的是,就是它的名字不能写成carpet或者Carpet

  18. mirror=Instantiate(www.assetBundle.Load(“Mirror”));

  19. textures=Instantiate(www.assetBundle.Load(“Textures”));

  20. names=www.assetBundle.mainAsset.name;

  21. //www.assetBundle.Unload(false);

  22. renderer.material.mainTexture=textures.GetComponent(“abc”).A;

  23. }

  24. function Update ()

  25. {

  26. }

  27. ---------------------------------------------------------------------

  28. abc.js

  29. ----------------------------------------------------------------------

  30. var A: Texture2D;

  31. var B: Texture2D;

  32. var C: Texture2D;

  33. function Update ()

  34. {

  35. }

  36. ---------------------------------------------------------------------

  37. AssetbundleBuild.cs

  38. ---------------------------------------------------------------------

  39. /// <summary>

  40. /// Exports a player compatible AssetBundle containing the selected objects, including dependencies.

  41. /// </summary>

  42. /// <remarks>Editor scripts are removed from the exported Assets.</remarks>

  43. using System.Collections.Generic;

  44. using System.IO;

  45. using UnityEditor;

  46. using UnityEngine;

  47. class AssetbundleEditor

  48. {

  49. [MenuItem(“Assets/Build AssetBundle”)]

  50. public static void ExportPlayerAssetBundle()

  51. {

  52. string tempRelativePath = “Assets/Temp/”;

  53. string tempAbsolutePath = Application.dataPath + “/../” + tempRelativePath;

  54. // Bring up save dialog.

  55. string path = EditorUtility.SaveFilePanel(“Save AssetBundle”, “”, “AssetBundle”, “unity3d”);

  56. if (path.Length > 0)

  57. {

  58. // Get all selected objects.

  59. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

  60. //Object[] processedSelection = new Object[selection.Length];

  61. Object[] processedSelection=selection;

  62. for (int i = 0; i < selection.Length; i++)

  63. {

  64. // Clone the original object.

  65. Object currentObject = selection[i];

  66. bool isPrefab = currentObject != null && currentObject.GetType() == typeof(GameObject);

  67. if (isPrefab)

  68. {

  69. if (!Directory.Exists(tempAbsolutePath))

  70. Directory.CreateDirectory(tempAbsolutePath);

  71. // Remove unneeded scripts from the prefab.

  72. /*

  73. Object clonedPrefab = EditorUtility.CreateEmptyPrefab(string.Format(“{0}{1}.prefab”, tempRelativePath, currentObject.name));

  74. if (clonedPrefab != null)

  75. {

  76. clonedPrefab = EditorUtility.ReplacePrefab((GameObject)currentObject, clonedPrefab);

  77. //EditorBase component = ((GameObject)clonedPrefab).GetComponent(typeof(EditorBase)) as EditorBase;

  78. UnityEditor component = ((GameObject)clonedPrefab).GetComponent(typeof(UnityEditor)) as UnityEditor;

  79. if (component != null)

  80. GameObject.DestroyImmediate(component, true);

  81. EditorUtility.SetDirty(clonedPrefab);

  82. processedSelection[i] = clonedPrefab;

  83. }

  84. */

  85. }

  86. }

  87. // Save changes to AssetDatabase and import processed prefabs.

  88. EditorApplication.SaveAssets();

  89. AssetDatabase.Refresh();

  90. // Export the processed AssetBundle.

  91. BuildPipeline.BuildAssetBundle(Selection.activeObject, processedSelection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);

  92. Selection.objects = selection;

  93. // Remove all cloned objects from the project.

  94. for (int i = 0; i < processedSelection.Length; i++)

  95. {

  96. if (processedSelection[i] != null)

  97. AssetDatabase.DeleteAsset(string.Format(“{0}{1}.prefab”, tempRelativePath, processedSelection[i].name));

  98. }

  99. if (Directory.Exists(tempAbsolutePath))

  100. Directory.Delete(tempAbsolutePath, false);

  101. }

  102. }

  103. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-30 20:31 , Processed in 0.080894 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部