Unity5自動命名Assetbundle並打包


using UnityEngine;  
using System.Collections;  
using UnityEditor;  
using System.IO;  
   
/// <summary>  
/// 把Resource下的資源打包成.unity3d 到StreamingAssets目錄下  
/// </summary>  
public class Builder : Editor  
{  
    public static string sourcePath = Application.dataPath + "/Resources";  
    const string AssetBundlesOutputPath = "Assets/StreamingAssets";  
   
    [MenuItem("Tools/AssetBundle/Build")]  
    public static void BuildAssetBundle()  
    {  
        ClearAssetBundlesName ();  
   
        Pack (sourcePath);  
   
        string outputPath = Path.Combine (AssetBundlesOutputPath,Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));  
        if (!Directory.Exists (outputPath))  
        {  
            Directory.CreateDirectory(outputPath);  
        }  
   
        //根據BuildSetting里面所激活的平台進行打包  
        BuildPipeline.BuildAssetBundles (outputPath,0,EditorUserBuildSettings.activeBuildTarget);  
   
        AssetDatabase.Refresh ();  
   
        Debug.Log ("打包完成");  
   
    }  
   
    /// <summary>  
    /// 清除之前設置過的AssetBundleName,避免產生不必要的資源也打包  
    /// 之前說過,只要設置了AssetBundleName的,都會進行打包,不論在什么目錄下  
    /// </summary>  
    static void ClearAssetBundlesName()  
    {  
        int length = AssetDatabase.GetAllAssetBundleNames ().Length;  
        Debug.Log (length);  
        string[] oldAssetBundleNames = new string[length];  
        for (int i = 0; i < length; i++)   
        {  
            oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];  
        }  
   
        for (int j = 0; j < oldAssetBundleNames.Length; j++)   
        {  
            AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);  
        }  
        length = AssetDatabase.GetAllAssetBundleNames ().Length;  
        Debug.Log (length);  
    }  
   
    static void Pack(string source)  
    {  
        DirectoryInfo folder = new DirectoryInfo (source);  
        FileSystemInfo[] files = folder.GetFileSystemInfos ();  
        int length = files.Length;  
        for (int i = 0; i < length; i++) {  
            if(files[i] is DirectoryInfo)  
            {  
                Pack(files[i].FullName);  
            }  
            else  
            {  
                if(!files[i].Name.EndsWith(".meta"))  
                {  
                    file (files[i].FullName);  
                }  
            }  
        }  
    }  
   
    static void file(string source)  
    {  
        string _source = Replace (source);  
        string _assetPath = "Assets" + _source.Substring (Application.dataPath.Length);  
        string _assetPath2 = _source.Substring (Application.dataPath.Length + 1);  
        //Debug.Log (_assetPath);  
   
        //在代碼中給資源設置AssetBundleName  
        AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);  
        string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);  
        assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");  
        //Debug.Log (assetName);  
        assetImporter.assetBundleName = assetName;  
    }  
   
    static string Replace(string s)  
    {  
        return s.Replace("\\","/");  
    }  
}  
   
public class Platform   
{  
    public static string GetPlatformFolder(BuildTarget target)  
    {  
        switch (target)  
        {  
        case BuildTarget.Android:  
            return "Android";  
        case BuildTarget.iOS:  
            return "IOS";  
        case BuildTarget.WebPlayer:  
            return "WebPlayer";  
        case BuildTarget.StandaloneWindows:  
        case BuildTarget.StandaloneWindows64:  
            return "Windows";  
        case BuildTarget.StandaloneOSXIntel:  
        case BuildTarget.StandaloneOSXIntel64:  
        case BuildTarget.StandaloneOSXUniversal:  
            return "OSX";  
        default:  
            return null;  
        }  
    }  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM