Esempio n. 1
0
    /// <summary>
    /// 打包手持、翅膀、尾巴、左右手持、肩膀
    /// </summary>
    /// <param name="characterFBX">模型</param>
    /// <param name="bundleParentPath">生成文件父路径</param>
    static void ProcMaterials_FileForWingHip(GameObject characterFBX, string bundleParentPath)
    {
        string bundleName = characterFBX.name;

        if (bundleName.StartsWith("wing_") || bundleName.StartsWith("hip_") || bundleName.StartsWith("lefthand_") ||
            bundleName.StartsWith("righthand_") || bundleName.StartsWith("shoulders_") ||
            bundleName.StartsWith("leftear_") || bundleName.StartsWith("rightear_"))
        {
            GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(characterFBX);
            string     desPath       = bundleParentPath + bundleName + ".clh";
            if (File.Exists(desPath))
            {
                File.Delete(desPath);
            }
            List <Object> includeList    = new List <Object>();
            Object        rendererPrefab = GenerateResource.ReplacePrefab(rendererClone, rendererClone.name);
            includeList.Add(rendererPrefab);

            if (!Directory.Exists(bundleParentPath))
            {
                Directory.CreateDirectory(bundleParentPath);
            }

            BuildAssetBundle.Build(null, includeList.ToArray(), desPath);

            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab));
        }
    }
Esempio n. 2
0
    static void ExportNoMeshSocksMaterial(Material mat)
    {
        bool   needBuild = true;
        string matPath   = AssetDatabase.GetAssetPath(mat);
        string matName   = mat.name;

        if (IsIOS())
        {
            //在IOS环境下,假如不是ios资源,查找到存在ios mat资源,则不编译该文件
            if (!IsIOSFile(matPath))
            {
                string matIOSPath = GetExtendPath(matPath, "_ios");

                if (File.Exists(matIOSPath))
                {
                    needBuild = false;
                }
            }
            else
            {
                matName = CheckIOSMat(mat.name);
            }
        }
        else
        {
            //非ios环境下,假如它是ios mat文件 ,则不编译该文件
            if (IsIOSFile(matPath))
            {
                needBuild = false;
            }
        }

        if (needBuild)
        {
            GameObject o = new GameObject(matName);

            SkinnedMeshRenderer smr = o.AddComponent <SkinnedMeshRenderer>();
            smr.sharedMaterial = mat;
            Object rendererPrefab = GenerateResource.ReplacePrefab(o, o.name);
            string desPath        = MaterialsAssetbundlePath + rendererPrefab.name + ".clh";
            if (File.Exists(desPath))
            {
                File.Delete(desPath);
            }

            BuildPipeline.PushAssetDependencies();

            BuildAssetBundle.Build(null, new Object[] { rendererPrefab }, desPath);

            BuildPipeline.PopAssetDependencies();
            Debug.Log("Saved " + rendererPrefab.name);

            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab));
        }
    }
Esempio n. 3
0
    static void ProcBones_File(string filePath)
    {
        Object o = AssetDatabase.LoadMainAssetAtPath(filePath);

        if (o is GameObject)
        {
            GameObject characterFBX = (GameObject)o;
            string     bundleName   = characterFBX.name;
            Debug.Log("******* Creating Bones assetbundles for: " + bundleName + " *******");

            //remove bones;
            GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(characterFBX);
            Animator   at            = rendererClone.GetComponent <Animator>();
            if (at != null)
            {
                Object.DestroyImmediate(at);
            }

            Object rendererPrefab   = GenerateResource.ReplacePrefab(rendererClone, rendererClone.name);
            string bundleParentPath = BonesAssetbundlePath;

            if (!Directory.Exists(bundleParentPath))
            {
                Directory.CreateDirectory(bundleParentPath);
            }

            if (!BuildAssetBundle.IsLegalAsset(bundleName))
            {
                Debug.LogError("Generate bone error, asset name is not all lower," + filePath);
                EditorUtility.DisplayDialog("Error", "Generate bone error, asset name is not all lower,Please try again!" + filePath, "OK");
                return;
            }

            BuildAssetBundle.Build(rendererPrefab, null, bundleParentPath + bundleName + ".res");
            Debug.Log("Saved Bones to '" + bundleParentPath + bundleName + ".res");

            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab));
            GameObject.DestroyImmediate(rendererClone);
        }
    }
Esempio n. 4
0
    static void ProcMaterials_FileForWingHip(GameObject characterFBX)
    {
        if (!IsNeedBone(characterFBX.name))
        {
            GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(characterFBX);
            string     desPath       = MaterialsAssetbundlePath + characterFBX.name + ".clh";
            if (File.Exists(desPath))
            {
                File.Delete(desPath);
            }
            List <Object> includeList    = new List <Object>();
            Object        rendererPrefab = GenerateResource.ReplacePrefab(rendererClone, rendererClone.name);
            includeList.Add(rendererPrefab);

            BuildPipeline.PushAssetDependencies();

            BuildAssetBundle.Build(null, includeList.ToArray(), desPath);

            BuildPipeline.PopAssetDependencies();
            AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab));
        }
    }
Esempio n. 5
0
    static void ProcEnchantEffect_File(string filePath, string dirPath, string destParentPath, List <string> shdList)
    {
        GameObject effectPrefab = AssetDatabase.LoadMainAssetAtPath(dirPath) as GameObject;

        if (effectPrefab == null)
        {
            Debug.Log(dirPath);
            return;
        }

        string bundleName = effectPrefab.name;

        List <GameObject> particallist = new List <GameObject>();
        Dictionary <string, DependencyAsset> allAssetBundleMap = new Dictionary <string, DependencyAsset>();            //记录所有已经打包的Asset

        GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(effectPrefab);

        for (int i = rendererClone.transform.childCount - 1; i >= 0; --i)
        {
            Transform childTran = rendererClone.transform.GetChild(i);
            if (childTran.name.Contains("Bip01"))
            {
                FindParticalFromBonse(particallist, childTran);
                break;
            }
        }

        Object rendererPrefab  = null;
        string effectAssetPath = string.Empty;

        if (bundleName.Contains("group"))
        {
            rendererPrefab = GenerateResource.ReplacePrefab(rendererClone, rendererClone.name);
        }
        else
        {
            GameObject effectRoot = new GameObject();
            effectRoot.name = bundleName;
            foreach (GameObject p in particallist)
            {
                GameObject gParent = new GameObject(GetTransfromPathName(p.transform.parent));
                gParent.transform.position   = p.transform.parent.position;
                gParent.transform.rotation   = p.transform.parent.rotation;
                gParent.transform.localScale = Vector3.one;
                p.transform.parent           = gParent.transform;
                gParent.transform.parent     = effectRoot.transform;
            }

            rendererPrefab = GenerateResource.ReplacePrefab(effectRoot, effectRoot.name);
        }
        effectAssetPath = AssetDatabase.GetAssetPath(rendererPrefab);

        AssetDatabase.Refresh();                //刷新
        AssetDatabase.SaveAssets();             //保存

        AssetBundleConfig modeConfig = new AssetBundleConfig();
        Dictionary <string, List <DependencyAsset> > allAssetRefs = BuildAssetBundle.GetAssetDependencieRefs((new List <string>()
        {
            effectAssetPath
        }).ToArray());

        string dirName = bundleName;

        //打包依赖资源
        foreach (string modePath in allAssetRefs.Keys)
        {
            string modeName = BuildAssetBundle.GetAssetName(modePath);
            List <DependencyAsset> depList = allAssetRefs[modePath];
            for (int i = 0; i < depList.Count; ++i)
            {
                List <Object> includeList = new List <Object>();

                DependencyAsset dasset           = depList[i];
                AssetBundleType arType           = dasset.AssetType;
                string          assetName        = dasset.AssetName;
                string          assetFullName    = dasset.AssetName + "." + dasset.AssetSuffix;
                string          assetPath        = dasset.AssetPath;
                string          bundleParentPath = destParentPath + arType + "/";
                string          bundlePath       = bundleParentPath + assetName + "." + arType.ToString().ToLower();

                if (modePath.Equals(assetPath))
                {
                    //重新改变路径
                    bundleParentPath = destParentPath + assetName + "/";
                    bundlePath       = bundleParentPath + assetName + ".enceff";
                }
                else
                {
                    //忽略原始FBX或者Prefab文件,也忽略了最终需要生成的FBX或Prefab
                    if (arType == AssetBundleType.Pre)
                    {
                        continue;
                    }
                    if (arType == AssetBundleType.Shd && shdList.Contains(assetFullName))
                    {
                        continue;
                    }

                    modeConfig.AddConfig(modeName, arType, assetName, i);
                }

                if (!allAssetBundleMap.ContainsKey(assetName))
                {
                    if (!Directory.Exists(bundleParentPath))
                    {
                        Directory.CreateDirectory(bundleParentPath);
                    }
                    if (modePath.Equals(assetPath))
                    {
                        BuildPipeline.PushAssetDependencies();
                    }

                    if (arType == AssetBundleType.Shd)
                    {
                        BuildShaderExtend.GenerateShaderExtend(assetPath, dasset.IsLeaf);
                    }
                    else
                    {
                        if (!BuildAssetBundle.IsLegalAsset(assetName))
                        {
                            string errorTips = string.Format("Generate enchant effect warning, asset name is not all lower,FileName is {0},AssetName is {1}",
                                                             dirName, assetPath);
                            Debug.LogError(errorTips);
                            EditorUtility.DisplayDialog("Error", errorTips + "Please try again!", "OK");
                            return;
                        }

                        Object obj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));
                        includeList.Add(obj);

                        BuildAssetBundle.Build(obj, includeList.ToArray(), bundlePath, dasset.IsLeaf);

                        if (modePath.Equals(assetPath))
                        {
                            BuildPipeline.PopAssetDependencies();
                        }
                        allAssetBundleMap.Add(assetName, dasset);
                    }
                }
                else
                {
                    DependencyAsset usedAsset = allAssetBundleMap[assetName];
                    if (usedAsset.AssetType != arType)
                    {
                        Debug.LogError("Build EnchantEffect error, same asset name has been found.AssetName=" +
                                       assetPath + "," + usedAsset.AssetPath);
                    }
                }
            }
        }

        if (!Directory.Exists(destParentPath + dirName))
        {
            Directory.CreateDirectory(destParentPath + dirName);
        }
        string configDirPath = destParentPath + dirName + "/" + bundleName + ".txt";

        modeConfig.SaveConfig(configDirPath);

        //删除临时资源
        AssetDatabase.DeleteAsset(effectAssetPath);
        GameObject.DestroyImmediate(rendererClone);
    }
Esempio n. 6
0
    static void ProcMaterials_File(Object o, List <string> matList)
    {
        if (o is GameObject)
        {
            GameObject characterFBX = (GameObject)o;
            string     bundleName   = characterFBX.name;
            Debug.Log("******* Creating Material assetbundles for: " + bundleName + " *******");
#if false
            string desPath = MaterialsAssetbundlePath + bundleName + ".clh";
            if (File.Exists(desPath))
            {
                File.Delete(desPath);
            }

            BuildPipeline.PushAssetDependencies();
            BuildPipeline.BuildAssetBundle(characterFBX, null, desPath,
                                           BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.DeterministicAssetBundle,
                                           EditorUserBuildSettings.activeBuildTarget);
            BuildPipeline.PopAssetDependencies();
#else
            if (!IsNeedBone(bundleName))
            {
                ProcMaterials_FileForWingHip(characterFBX);
                return;
            }
            //remove bones;
            GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(characterFBX);
            //GameObject Bonese = null;

            //获取骨骼下的所有特效
            List <GameObject> particallist = new List <GameObject>();
            for (int i = rendererClone.transform.childCount - 1; i >= 0; --i)
            {
                Transform childTran = rendererClone.transform.GetChild(i);
                if (childTran.name.Contains("Bip01"))
                {
                    FindParticalFromBonse(particallist, childTran);
                    break;
                }
            }

            for (int i = rendererClone.transform.childCount - 1; i >= 0; --i)
            {
                List <Object> includeList = new List <Object>();
                List <string> holderList  = new List <string>();

                Transform childTran = rendererClone.transform.GetChild(i);
                if (childTran.name.Contains("Bip01"))
                {
                    //FindParticalFromBonse( particallist ,childTran);
                    continue;
                }
                else
                {
                    childTran.parent = null;
                }

                string desPath = MaterialsAssetbundlePath + childTran.name + ".clh";
                if (File.Exists(desPath))
                {
                    File.Delete(desPath);
                }

                // we don't need to Save bones and animations . Any
                // As we can not edit assets we instantiate
                // the fbx and remove what we dont need. As only assets can be
                // added to assetbundles we save the result as a prefab and delete
                // it as soon as the assetbundle is created.
                GameObject partClone = childTran.gameObject;
                foreach (SkinnedMeshRenderer smr in partClone.GetComponentsInChildren <SkinnedMeshRenderer>(true))
                {
                    //Replace Mat for IOS
                    if (IsIOS())
                    {
                        string          matPath    = string.Empty;
                        string          matIOSPath = string.Empty;
                        List <Material> matList1   = new List <Material>();

                        foreach (Material mat in smr.sharedMaterials)
                        {
                            matPath    = AssetDatabase.GetAssetPath(mat);
                            matIOSPath = GetExtendPath(matPath, "_ios");
                            if (File.Exists(matIOSPath))
                            {
                                Material newMat = AssetDatabase.LoadAssetAtPath(FixToUnityPath(matIOSPath), typeof(Material)) as Material;
                                if (newMat != null)
                                {
                                    matList1.Add(newMat);
                                }
                            }
                            else
                            {
                                matList1.Add(mat);
                            }
                        }

                        smr.sharedMaterials = matList1.ToArray();
                    }

                    //判断蒙皮的骨骼是否是共用骨骼
                    if (IsCommonBone(smr))
                    {
                        //Bonesname
                        List <string> boneNames = new List <string>();
                        foreach (Transform t in smr.bones)
                        {
                            boneNames.Add(GetTransfromPathName(t));
                        }

                        StringHolder holder = ScriptableObject.CreateInstance <StringHolder>();
                        holder.content = boneNames.ToArray();

                        string holderPath = "Assets/" + smr.name + "bonenames.prefab";
                        AssetDatabase.CreateAsset(holder, holderPath);

                        includeList.Add(AssetDatabase.LoadAssetAtPath(holderPath, typeof(StringHolder)));
                        holderList.Add(holderPath);

                        foreach (Material mat in smr.sharedMaterials)
                        {
                            if (mat != null && mat.name.Contains("socks_") && !matList.Contains(mat.name))
                            {
                                matList.Add(mat.name);
                            }
                        }
                    }
                    else
                    {
                        smr.gameObject.name = smr.gameObject.name + "_nobone";
                    }
                }

                if (particallist.Count != 0)
                {
                    foreach (GameObject p in particallist)
                    {
                        if (p.name.Contains(partClone.name))
                        {
                            GameObject gParent = new GameObject(GetTransfromPathName(p.transform.parent));
                            gParent.transform.position   = p.transform.parent.position;
                            gParent.transform.rotation   = p.transform.parent.rotation;
                            gParent.transform.localScale = Vector3.one;
                            p.transform.parent           = gParent.transform;
                            gParent.transform.parent     = partClone.transform;
                        }
                    }
                    foreach (Transform t in partClone.transform)
                    {
                        if (t.childCount > 0)
                        {
                            particallist.Remove(t.GetChild(0).gameObject);
                        }
                    }
                }

                Object rendererPrefab = GenerateResource.ReplacePrefab(partClone, partClone.name);
                includeList.Add(rendererPrefab);

                BuildPipeline.PushAssetDependencies();

                BuildAssetBundle.Build(null, includeList.ToArray(), desPath);

                BuildPipeline.PopAssetDependencies();
                Debug.Log("Saved " + rendererPrefab.name);

                AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab));

                foreach (string s in holderList)
                {
                    AssetDatabase.DeleteAsset(s);
                }
            }

            GameObject.DestroyImmediate(rendererClone);
#endif
        }
    }