Esempio n. 1
0
    /// <summary>
    /// Extra Flag ->   ex:  Android/  AndroidSD/  AndroidHD/
    /// </summary>
    /// <param name="platfrom"></param>
    /// <param name="quality"></param>
    /// <returns></returns>
    public static string GetExportPath(BuildTarget platfrom, CResourceQuality quality = CResourceQuality.Sd)
    {
        string basePath = Path.GetFullPath(Application.dataPath + "/" + CCosmosEngine.GetConfig(CCosmosEngineDefaultConfig.AssetBundleBuildRelPath) + "/");

        if (!Directory.Exists(basePath))
        {
            CBuildTools.ShowDialog("路径配置错误: " + basePath);
            throw new System.Exception("路径配置错误");
        }

        string path = null;

        switch (platfrom)
        {
        case BuildTarget.Android:
        case BuildTarget.iPhone:
        case BuildTarget.StandaloneWindows:
            var platformName = CResourceModule.BuildPlatformName;
            if (quality != CResourceQuality.Sd)      // SD no need add
            {
                platformName += quality.ToString().ToUpper();
            }

            path = basePath + platformName + "/";
            break;

        default:
            CBuildTools.ShowDialog("构建平台配置错误");
            throw new System.Exception("构建平台配置错误");
        }
        return(path);
    }
Esempio n. 2
0
    /// <summary>
    /// 获取完整的打包路径,并确保目录存在
    /// </summary>
    /// <param name="path"></param>
    /// <param name="buildTarget"></param>
    /// <returns></returns>
    public static string MakeSureExportPath(string path, BuildTarget buildTarget, CResourceQuality quality)
    {
        path = CBuildTools.GetExportPath(buildTarget, quality) + path;

        string exportDirectory = path.Substring(0, path.LastIndexOf('/'));

        if (!System.IO.Directory.Exists(exportDirectory))
        {
            System.IO.Directory.CreateDirectory(exportDirectory);
        }

        path = path.Replace("/", @"\");

        return(path);
    }
Esempio n. 3
0
    public static uint BuildScriptableObject <T>(T scriptObject, string path, BuildTarget buildTarget, CResourceQuality quality) where T : ScriptableObject
    {
        const string tempAssetPath = "Assets/~Temp.asset";

        AssetDatabase.CreateAsset(scriptObject, tempAssetPath);
        T tempObj = (T)AssetDatabase.LoadAssetAtPath(tempAssetPath, typeof(T));

        if (tempObj == null)
        {
            throw new System.Exception();
        }

        uint crc = CBuildTools.BuildAssetBundle(tempObj, path, buildTarget, quality);

        AssetDatabase.DeleteAsset(tempAssetPath);

        return(crc);
    }
Esempio n. 4
0
    public static uint BuildAssetBundle(Object asset, string path, BuildTarget buildTarget, CResourceQuality quality)
    {
        if (asset == null || string.IsNullOrEmpty(path))
        {
            BuildError("BuildAssetBundle: {0}", path);
            return(0);
        }

        var    assetNameWithoutDir = asset.name.Replace("/", "").Replace("\\", ""); // 防止多重目录...
        string tmpPrefabPath       = string.Format("Assets/{0}.prefab", assetNameWithoutDir);

        PrefabType prefabType = PrefabUtility.GetPrefabType(asset);

        string relativePath = path;

        path = MakeSureExportPath(path, buildTarget, quality);

        uint crc = 0;

        if (asset is Texture2D)
        {
            var assetPath = AssetDatabase.GetAssetPath(asset);
            if (!string.IsNullOrEmpty(assetPath)) // Assets内的纹理
            {                                     // Texutre不复制拷贝一份
                _DoBuild(out crc, asset, null, path, relativePath, buildTarget);
            }
            else
            {
                // 内存的图片~临时创建Asset, 纯正的图片, 使用Sprite吧
                var memoryTexture = asset as Texture2D;
                var memTexName    = memoryTexture.name;

                var tmpTexPath = string.Format("Assets/Tex_{0}_{1}.png", memoryTexture.name, Path.GetRandomFileName());

                CDebug.LogWarning("【BuildAssetBundle】Build一个非Asset 的Texture: {0}", memoryTexture.name);

                File.WriteAllBytes(tmpTexPath, memoryTexture.EncodeToPNG());
                AssetDatabase.ImportAsset(tmpTexPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
                var tmpTex = (Texture2D)AssetDatabase.LoadAssetAtPath(tmpTexPath, typeof(Texture2D));

                asset = tmpTex;
                try
                {
                    asset.name = memTexName;

                    _DoBuild(out crc, asset, null, path, relativePath, buildTarget);
                }
                catch (Exception e)
                {
                    CDebug.LogException(e);
                }

                File.Delete(tmpTexPath);
                if (File.Exists(tmpTexPath + ".meta"))
                {
                    File.Delete(tmpTexPath + ".meta");
                }
            }
        }
        else if ((prefabType == PrefabType.None && AssetDatabase.GetAssetPath(asset) == string.Empty) ||
                 (prefabType == PrefabType.ModelPrefabInstance))          // 非prefab对象
        {
            Object tmpInsObj = (GameObject)GameObject.Instantiate(asset); // 拷出来创建Prefab
            Object tmpPrefab = PrefabUtility.CreatePrefab(tmpPrefabPath, (GameObject)tmpInsObj, ReplacePrefabOptions.ConnectToPrefab);
            asset = tmpPrefab;

            _DoBuild(out crc, asset, null, path, relativePath, buildTarget);

            GameObject.DestroyImmediate(tmpInsObj);
            AssetDatabase.DeleteAsset(tmpPrefabPath);
        }
        else if (prefabType == PrefabType.PrefabInstance)
        {
            var prefabParent = PrefabUtility.GetPrefabParent(asset);
            _DoBuild(out crc, prefabParent, null, path, relativePath, buildTarget);
        }
        else
        {
            //CDebug.LogError("[Wrong asse Type] {0}", asset.GetType());
            _DoBuild(out crc, asset, null, path, relativePath, buildTarget);
        }
        return(crc);
    }