Guest User

Untitled

a guest
Jul 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. public class ABBuilder
  2. {
  3. static readonly Windows.ConsoleWindow Console = new Windows.ConsoleWindow();
  4. private static readonly string Dest = Application.dataPath + "/AB";
  5. private static string _dataPath = Application.dataPath;
  6.  
  7. [MenuItem("Tools/Copy Assets")]
  8. public static void CopyAssets()
  9. {
  10. var path = "C:/AB_Upload";
  11. var files = Directory.GetFiles(path);
  12.  
  13. var imgPath = "Assets/AB_ref/ImageAsset.png";
  14. var vidPath = "Assets/AB_ref/VidAsset.mp4";
  15.  
  16. var imgAsset = AssetDatabase.LoadAssetAtPath<Texture2D>(imgPath);
  17. var vidAsset = AssetDatabase.LoadAssetAtPath<VideoClip>(vidPath);
  18. var destFolderPath = "Assets/Data";
  19.  
  20. foreach (var pre_assetPath in files)
  21. {
  22. var assetPath = pre_assetPath.Replace("\\", "/");
  23. Debug.LogFormat("Copying {0}", assetPath);
  24.  
  25. if(string.IsNullOrEmpty(assetPath))continue;
  26. var extension = new FileInfo(assetPath).Extension;
  27.  
  28. var assetDest = destFolderPath + assetPath.Substring(assetPath.LastIndexOf('/'));
  29. assetDest = AssetDatabase.GenerateUniqueAssetPath(assetDest);
  30.  
  31. if (extension == ".png")
  32. {
  33. AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(imgAsset.GetInstanceID()), assetDest);
  34. File.Copy(assetPath, assetDest, true);
  35. }
  36. else if(extension == ".mp4")
  37. {
  38. AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(vidAsset.GetInstanceID()), assetDest);
  39. File.Copy(assetPath, assetDest, true);
  40. }
  41. else
  42. {
  43. continue;
  44. }
  45. Debug.LogFormat("Copied {0}", assetDest);
  46. }
  47.  
  48. AssetDatabase.SaveAssets();
  49. AssetDatabase.Refresh();
  50. }
  51.  
  52. [MenuItem("Tools/Build Asset Bundle from Data Folder")]
  53. public static void BuildAssetBundle()
  54. {
  55. AssetBundleBuild build = new AssetBundleBuild();
  56. build.assetBundleName = "data";
  57.  
  58. string[] files = AssetDatabase.GetAllAssetPaths().ToList().Where(x => x.Contains("Assets/Data/")).ToArray();
  59. build.assetNames = files;
  60. BuildPipeline.BuildAssetBundles("Assets/AB", new AssetBundleBuild[1] { build }, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
  61. //todo copy assetbundle to dest folder
  62. }
  63.  
  64. public static void Start()
  65. {
  66. Console.Initialize();
  67. Console.SetTitle( "Test Server" );
  68. Application.logMessageReceived += HandleLog;
  69. var sw = new Stopwatch();
  70. sw.Start();
  71. Debug.Log(" ");
  72. Debug.Log("Start copying assets");
  73.  
  74. CopyAssets();
  75. BuildAssetBundle();
  76.  
  77. Debug.Log("Finished Building Asset Bundle.. took : " + sw.Elapsed);
  78. sw.Stop();
  79. Console.Shutdown();
  80. }
  81.  
  82. //
  83. // Debug.Log* callback
  84. //
  85. //todo define logtype so we can filter all unesseary unity msgs
  86. static void HandleLog( string message, string stackTrace, LogType type )
  87. {
  88. if ( type == LogType.Warning )
  89. System.Console.ForegroundColor = ConsoleColor.Yellow;
  90. else if ( type == LogType.Error )
  91. System.Console.ForegroundColor = ConsoleColor.Red;
  92. else
  93. System.Console.ForegroundColor = ConsoleColor.White;
  94.  
  95. System.Console.WriteLine( message );
  96. }
  97. }
Add Comment
Please, Sign In to add comment