static void Execute() { string skillResPublicPath = "Assets/Resources/Skill"; string packName = "Build/Skill/skilltemp.config"; string filePath = "Build/Skill"; if (Directory.Exists(filePath)) { Directory.Delete(filePath, true); } Directory.CreateDirectory(filePath); // 打包所有的配置文件 [3/16/2012 Ivan] BuildPipeline.PushAssetDependencies(); List <UnityEngine.Object> skills = EditorHelpers.CollectAll <UnityEngine.Object>(skillResPublicPath); LogManager.Log("curSkill " + skills.Count); BuildPipeline.BuildAssetBundle( null, skills.ToArray(), packName, options); BuildPipeline.PopAssetDependencies(); // 加密后并重新打包 [3/27/2012 Ivan] EncryptFile(packName); }
static void Execute() { string configResPrivatePath = "Assets/Resources/Private/"; string configResPublicPath = "Assets/Resources/Public/"; string packName = "Build/Config/allOrg.config"; string filePath = "Build/Config/"; if (Directory.Exists(filePath)) { Directory.Delete(filePath, true); } Directory.CreateDirectory(filePath); // 打包所有的配置文件 [3/16/2012 Ivan] BuildPipeline.PushAssetDependencies(); List <UnityEngine.Object> configs = EditorHelpers.CollectAll <UnityEngine.Object>(configResPrivatePath); configs.AddRange(EditorHelpers.CollectAll <UnityEngine.Object>(configResPublicPath)); BuildPipeline.BuildAssetBundle( null, configs.ToArray(), packName, options); BuildPipeline.PopAssetDependencies(); // 加密后并重新打包 [3/27/2012 Ivan] EncryptFile(packName); }
//CreatePartAssetBundle //Example: // bundle: female_face-1.assetbundle // - a: GameObject // name: PartAssetGameObjectName(renderobject), // type: GameObject(as Prefab) // - a: Materials // name: material name // type: material // - a: Bones // name: PartAssetBonesName(bonesname) // type: StringHolder static void CreatePartAssetBundles(GameObject fbx, string name) { List <Material> materials = EditorHelpers.CollectAll <Material>(MeterialsPath(fbx)); foreach (var skinnedMeshRender in fbx.GetComponentsInChildren <SkinnedMeshRenderer>(true)) // get all children with SkinnedMeshRender, even they are disabled { List <Object> assets = new List <Object>(); //Prefab for children with skinnedMeshRender GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(skinnedMeshRender.gameObject); //delete cloned parent object by side effect GameObject rendererCloneParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererCloneParent); //create prefab for GameObject Object rendererPrefab = CreatePrefab(rendererClone, CharacterElement.PartAssetGameObjectName); Object.DestroyImmediate(rendererClone); assets.Add(rendererPrefab); //Materials foreach (var material in materials) { if (material.name.Contains(skinnedMeshRender.name.ToLower())) { assets.Add(material); } } //Bones List <string> boneNames = new List <string>(); foreach (var transform in skinnedMeshRender.bones) //??? { boneNames.Add(transform.name); } string boneNameAssetPath = AssetPath + CharacterElement.AssetFileName(CharacterElement.PartAssetBonenames); StringHolder holder = ScriptableObject.CreateInstance <StringHolder>(); holder.content = boneNames.ToArray(); AssetDatabase.CreateAsset(holder, boneNameAssetPath); // Create bone asset Object boneAsset = AssetDatabase.LoadAssetAtPath(boneNameAssetPath, typeof(StringHolder)); assets.Add(boneAsset); //Create bundl by assets string bundlePath = AssetBundlePath + name + "_" + skinnedMeshRender.name.ToLower() + ".assetbundle"; BuildPipeline.BuildAssetBundle(null, assets.ToArray(), bundlePath); //Cleanup AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab)); AssetDatabase.DeleteAsset(boneNameAssetPath); } }
static void CreatePartAssetBundles(GameObject fbx, string name) { List <Material> materials = EditorHelpers.CollectAll <Material>(MaterialsPath(fbx)); foreach (SkinnedMeshRenderer smr in fbx.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { List <Object> toinclude = new List <Object>(); GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(smr.gameObject); GameObject rendererParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererParent); Object rendererPrefab = GetPrefab(rendererClone, "rendererobject"); toinclude.Add(rendererPrefab); // 若材质对象名称中包含子对象的名称(如eyes、face-1、face-2等则 // 视将材质对象加入列表。这里注意每个toinclude对象与一个 // SkinnedMeshRenderer对象对应,即与FBX对象的子对象对应。 foreach (Material m in materials) { if (m.name.Contains(smr.name.ToLower())) { toinclude.Add(m); } } List <string> boneNames = new List <string>(); foreach (Transform t in smr.bones) { boneNames.Add(t.name); } string stringholderpath = "Assets/bonenames.asset"; StringHolder holder = ScriptableObject.CreateInstance <StringHolder> (); holder.content = boneNames.ToArray(); AssetDatabase.CreateAsset(holder, stringholderpath); toinclude.Add(AssetDatabase.LoadAssetAtPath(stringholderpath, typeof(StringHolder))); string bundleName = name + "_" + smr.name.ToLower(); string path = AssetbundlePath + bundleName + ".assetbundle"; BuildPipeline.BuildAssetBundle(null, toinclude.ToArray(), path, BuildAssetBundleOptions.CollectDependencies); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab)); AssetDatabase.DeleteAsset(stringholderpath); } }
static void Execute() { bool createdBundle = false; foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) { if (!(o is GameObject)) { continue; } if (o.name.Contains("@")) { continue; } if (!AssetDatabase.GetAssetPath(o).Contains("/characters/")) { continue; } GameObject characterFBX = (GameObject)o; string name = characterFBX.name.ToLower(); Debug.Log("******* Creating assetbundles for: " + name + " *******"); // Create a directory to store the generated assetbundles. if (!Directory.Exists(AssetbundlePath)) { Directory.CreateDirectory(AssetbundlePath); } // Delete existing assetbundles for current character. string[] existingAssetbundles = Directory.GetFiles(AssetbundlePath); foreach (string bundle in existingAssetbundles) { if (bundle.EndsWith(".assetbundle") && bundle.Contains("/assetbundles/" + name)) { File.Delete(bundle); } } // Save bones and animations to a seperate assetbundle. Any // possible combination of CharacterElements will use these // assets as a base. 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 characterClone = (GameObject)Object.Instantiate(characterFBX); foreach (SkinnedMeshRenderer smr in characterClone.GetComponentsInChildren <SkinnedMeshRenderer>()) { Object.DestroyImmediate(smr.gameObject); } characterClone.AddComponent <SkinnedMeshRenderer>(); Object characterBasePrefab = GetPrefab(characterClone, "characterbase"); string path = AssetbundlePath + name + "_characterbase.assetbundle"; BuildPipeline.BuildAssetBundle(characterBasePrefab, null, path, BuildAssetBundleOptions.CollectDependencies); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(characterBasePrefab)); // Collect materials. List <Material> materials = EditorHelpers.CollectAll <Material>(GenerateMaterials.MaterialsPath(characterFBX)); // Create assetbundles for each SkinnedMeshRenderer. foreach (SkinnedMeshRenderer smr in characterFBX.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { List <Object> toinclude = new List <Object>(); // Save the current SkinnedMeshRenderer as a prefab so it can be included // in the assetbundle. As instantiating part of an fbx results in the // entire fbx being instantiated, we have to dispose of the entire instance // after we detach the SkinnedMeshRenderer in question. GameObject rendererClone = (GameObject)EditorUtility.InstantiatePrefab(smr.gameObject); GameObject rendererParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererParent); Object rendererPrefab = GetPrefab(rendererClone, "rendererobject"); toinclude.Add(rendererPrefab); // Collect applicable materials. foreach (Material m in materials) { if (m.name.Contains(smr.name.ToLower())) { toinclude.Add(m); } } // When assembling a character, we load SkinnedMeshRenderers from assetbundles, // and as such they have lost the references to their bones. To be able to // remap the SkinnedMeshRenderers to use the bones from the characterbase assetbundles, // we save the names of the bones used. List <string> boneNames = new List <string>(); foreach (Transform t in smr.bones) { boneNames.Add(t.name); } string stringholderpath = "Assets/bonenames.asset"; AssetDatabase.CreateAsset(new StringHolder(boneNames.ToArray()), stringholderpath); toinclude.Add(AssetDatabase.LoadAssetAtPath(stringholderpath, typeof(StringHolder))); // Save the assetbundle. string bundleName = name + "_" + smr.name.ToLower(); path = AssetbundlePath + bundleName + ".assetbundle"; BuildPipeline.BuildAssetBundle(null, toinclude.ToArray(), path, BuildAssetBundleOptions.CollectDependencies); Debug.Log("Saved " + bundleName + " with " + (toinclude.Count - 2) + " materials"); // Delete temp assets. AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab)); AssetDatabase.DeleteAsset(stringholderpath); createdBundle = true; } } if (createdBundle) { UpdateCharacterElementDatabase.Execute(); } else { EditorUtility.DisplayDialog("Character Generator", "No Asset Bundles created. Select the characters folder in the Project pane to process all characters. Select subfolders to process specific characters.", "Ok"); } }
static void Execute() { bool validMaterial = false; foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) { if (!(o is GameObject)) { continue; } if (o.name.Contains("@")) { continue; } if (!AssetDatabase.GetAssetPath(o).Contains("/characters/")) { continue; } GameObject characterFbx = (GameObject)o; // Create directory to store generated materials. if (!Directory.Exists(MaterialsPath(characterFbx))) { Directory.CreateDirectory(MaterialsPath(characterFbx)); } // Collect all textures. List <Texture2D> textures = EditorHelpers.CollectAll <Texture2D>(CharacterRoot(characterFbx) + "textures"); // Create materials for each SkinnedMeshRenderer. foreach (SkinnedMeshRenderer smr in characterFbx.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { // Check if this SkinnedMeshRenderer has a normalmap. Texture2D normalmap = null; foreach (Texture2D t in textures) { if (!t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } normalmap = t; break; } // Create a Material for each texture which name contains // the SkinnedMeshRenderer name. foreach (Texture2D t in textures) { if (t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } validMaterial = true; string materialPath = MaterialsPath(characterFbx) + "/" + t.name.ToLower() + ".mat"; // Dont overwrite existing materials, as we would // lose existing material settings. if (File.Exists(materialPath)) { continue; } // Use a default shader according to artist preferences. string shader = "Specular"; if (normalmap != null) { shader = "Bumped Specular"; } // Create the Material Material m = new Material(Shader.Find(shader)); m.SetTexture("_MainTex", t); if (normalmap != null) { m.SetTexture("_BumpMap", normalmap); } AssetDatabase.CreateAsset(m, materialPath); } } } AssetDatabase.Refresh(); if (!validMaterial) { EditorUtility.DisplayDialog("Character Generator", "No Materials created. Select the characters folder in the Project pane to process all characters. Select subfolders to process specific characters.", "Ok"); } }
static void CreateCharacter() { bool createdBundle = false; // 创建一个文件夹保存生成的assetbundles. string exportPath = Application.streamingAssetsPath + "/character/"; if (!Directory.Exists(exportPath)) { Directory.CreateDirectory(exportPath); } BundleXml xml = new BundleXml(); xml.Open(); foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) { if (!(o is GameObject)) { continue; } if (o.name.Contains("@")) { continue; } if (!AssetDatabase.GetAssetPath(o).Contains("/characters/")) { continue; } GameObject characterFBX = (GameObject)o; string name = characterFBX.name.ToLower(); Debug.Log("******* Creating assetbundles for: " + name + " *******"); GameObject characterClone = (GameObject)Object.Instantiate(characterFBX); foreach (Animation anim in characterClone.GetComponentsInChildren <Animation>()) { anim.cullingType = AnimationCullingType.BasedOnClipBounds; } // 把网格对象删除只留下骨架. foreach (SkinnedMeshRenderer smr in characterClone.GetComponentsInChildren <SkinnedMeshRenderer>()) { Object.DestroyImmediate(smr.gameObject); } // 把骨架保存到assetbundle. string assetName = name;// +"_characterbase"; string bundleName = assetName + ASSETBUNDLE_EXT; string path = exportPath + bundleName; characterClone.AddComponent <SkinnedMeshRenderer>(); Object characterBasePrefab = GetPrefab(characterClone, assetName); BuildPipeline.BuildAssetBundle(null, new Object[] { characterBasePrefab }, path, BuildAssetBundleOptions.CollectDependencies, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(characterBasePrefab)); xml.UpdateAssetBundle("character/" + bundleName); xml.UpdateAsset(assetName, null); // 收集角色文件夹下的所有材质. List <Material> materials = EditorHelpers.CollectAll <Material>(GenerateMaterials.MaterialsPath(characterFBX)); // 把每个SkinnedMeshRenderer单独保存到assetbundle. foreach (SkinnedMeshRenderer smr in characterFBX.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { List <Object> toinclude = new List <Object>(); // Save the current SkinnedMeshRenderer as a prefab so it can be included // in the assetbundle. As instantiating part of an fbx results in the // entire fbx being instantiated, we have to dispose of the entire instance // after we detach the SkinnedMeshRenderer in question. GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(smr.gameObject); GameObject rendererParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererParent); Object rendererPrefab = GetPrefab(rendererClone, "rendererobject"); toinclude.Add(rendererPrefab); // 收集对应的材质 foreach (Material m in materials) { if (m.name.Contains(smr.name.ToLower())) { toinclude.Add(m); } } // When assembling a character, we load SkinnedMeshRenderers from assetbundles, // and as such they have lost the references to their bones. To be able to // remap the SkinnedMeshRenderers to use the bones from the characterbase assetbundles, // we save the names of the bones used. List <string> boneNames = new List <string>(); foreach (Transform t in smr.bones) { boneNames.Add(t.name); } string stringholderpath = "Assets/bonenames.asset"; StringHolder holder = ScriptableObject.CreateInstance <StringHolder>(); holder.content = boneNames.ToArray(); AssetDatabase.CreateAsset(holder, stringholderpath); toinclude.Add(AssetDatabase.LoadAssetAtPath(stringholderpath, typeof(StringHolder))); // Save the assetbundle. bundleName = name + "_" + smr.name.ToLower(); path = exportPath + bundleName + ASSETBUNDLE_EXT; BuildPipeline.BuildAssetBundle(null, toinclude.ToArray(), path, BuildAssetBundleOptions.CollectDependencies, EditorUserBuildSettings.activeBuildTarget); Debug.Log("Saved " + bundleName + " with " + (toinclude.Count - 2) + " materials"); xml.UpdateAssetBundle("character/" + bundleName + ASSETBUNDLE_EXT); foreach (Object m in toinclude) { if (m is Material) { xml.UpdateAsset((m as Material).name, null); } } // Delete temp assets. AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(rendererPrefab)); AssetDatabase.DeleteAsset(stringholderpath); createdBundle = true; } } xml.Close(); if (!createdBundle) { EditorUtility.DisplayDialog("Character Generator", "No Asset Bundles created. Select the characters folder in the Project pane to process all characters. Select subfolders to process specific characters.", "Ok"); } }
static void Execute() { bool createdBundle = false; string objPath = "Character/A001"; foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) { if (!(o is GameObject)) { continue; } if (o.name.Contains("@")) { continue; } if (!AssetDatabase.GetAssetPath(o).Contains("/Character/")) { continue; } GameObject characterFBX = (GameObject)o; string name = characterFBX.name; if (name != "A001" && name != "Dog001") { continue; } Debug.Log("******* Creating assetbundles for: " + name + " *******"); // Create a directory to store the generated assetbundles. if (!Directory.Exists(AssetbundlePath)) { Directory.CreateDirectory(AssetbundlePath); } if (!Directory.Exists(AssetbundlePath + objPath)) { Directory.CreateDirectory(AssetbundlePath + objPath); } // Delete existing assetbundles for current character. string[] existingAssetbundles = Directory.GetFiles(AssetbundlePath); foreach (string bundle in existingAssetbundles) { if (bundle.EndsWith(".assetbundle") && bundle.Contains("/assetbundles/" + name)) { File.Delete(bundle); } } // Save bones and animations to a seperate assetbundle. Any // possible combination of CharacterElements will use these // assets as a base. 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 characterClone = (GameObject)Object.Instantiate(characterFBX); // postprocess animations: we need them animating even offscreen foreach (Animation anim in characterClone.GetComponentsInChildren <Animation>()) { anim.animateOnlyIfVisible = false; } foreach (SkinnedMeshRenderer smr in characterClone.GetComponentsInChildren <SkinnedMeshRenderer>()) { Object.DestroyImmediate(smr.gameObject); } foreach (MeshRenderer smr in characterClone.GetComponentsInChildren <MeshRenderer>()) { Object.DestroyImmediate(smr.gameObject); } characterClone.AddComponent <SkinnedMeshRenderer>(); CreatePrefab(characterClone, "characterbase" + name); //string path = AssetbundlePath + objPath + "/" + name + "_characterbase.assetbundle"; //BuildPipeline.BuildAssetBundle(characterBasePrefab, null, path, BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.UncompressedAssetBundle,AssetBundleTarget); //AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(characterBasePrefab)); //ZipUtil.CompressFileLZMA(path,AssetbundlePath + objPath + "/" + name + "_characterbase.zip"); //File.Delete(path); // Collect materials. //List<Material> materials = EditorHelpers.CollectAll<Material>(GenerateMaterials.MaterialsPath(characterFBX)); List <Material> materials = EditorHelpers.CollectAll <Material>("Assets/Resources/Character/Materials"); Dictionary <string, List <string> > skinMeshBoneDic = new Dictionary <string, List <string> >(); // Create assetbundles for each SkinnedMeshRenderer. List <CharacterElement> characterElements = new List <CharacterElement>(); foreach (SkinnedMeshRenderer smr in characterFBX.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { // Save the current SkinnedMeshRenderer as a prefab so it can be included // in the assetbundle. As instantiating part of an fbx results in the // entire fbx being instantiated, we have to dispose of the entire instance // after we detach the SkinnedMeshRenderer in question. GameObject rendererClone = (GameObject)EditorUtility.InstantiatePrefab(smr.gameObject); GameObject rendererParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererParent); CreatePrefab(rendererClone, smr.gameObject.name); List <string> boneNames = new List <string>(); foreach (Transform t in smr.bones) { boneNames.Add(t.name); } skinMeshBoneDic.Add(smr.gameObject.name, boneNames); } const string majorDelimiter = "&"; const string assignDelimiter = "="; string filePath = Application.dataPath + "/Resources/Config/SkinMeshedBones.txt"; try { using (FileStream t_file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { t_file.Seek(0, SeekOrigin.Begin); using (StreamWriter t_sw = new StreamWriter(t_file)) { foreach (string skinMeshName in skinMeshBoneDic.Keys) { List <string> boneNames = skinMeshBoneDic[skinMeshName]; string oneLineStr = skinMeshName + majorDelimiter; for (int i = 0; i < boneNames.Count; i++) { oneLineStr += boneNames[i] + assignDelimiter; } t_sw.WriteLine(oneLineStr); } } } } catch (System.Exception ex) { Debug.LogError("Write config file failed, Exception:" + ex.Message); } } }
/// <summary> /// 生成目标模型的材质 /// 低模和高模用同一套材质,所以只分离其中一套材质即可。这里只分离高模的 /// </summary> /// <param name="targetModel"></param> /// <param name="errorInfo"></param> /// <returns></returns> public static bool GenerateMaterials(GameObject targetModel, out string errorInfo) { // 检查模型的正确性 if (!CheckModelValid(targetModel, out errorInfo)) { return(false); } // 材质存放路径 string matPath; if (string.IsNullOrEmpty(MATERIALS_PATH)) { matPath = MaterialsPath(targetModel); } else { matPath = MATERIALS_PATH; } var modelName = targetModel.name.Replace(M_MODELPREFIX, ""); matPath += modelName + "/"; string texPath; if (string.IsNullOrEmpty(TEXTURES_PATH)) { texPath = CharacterRoot(targetModel) + "textures"; } else { texPath = TEXTURES_PATH; } if (!Directory.Exists(matPath)) { Directory.CreateDirectory(matPath); } DeleteSpecFiles(matPath, modelName); //获取指定路径的所有textures(材质所需要的原textures) List <Texture2D> textures = EditorHelpers.CollectAll <Texture2D>(texPath); var smrs = targetModel.GetComponentsInChildren <SkinnedMeshRenderer>(true); foreach (var smr in smrs) { // 只用分离一套,所以过滤掉低模的 if (smr.name.Contains("low")) { continue; } Texture2D normalMap = null; Texture2D hurtFlashTex = null; Texture2D burstFlashTex = null; foreach (var t in textures) { if (!t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } normalMap = t; break; } foreach (var t in textures) { if (t.name.ToLower().Contains("hurtflash")) { hurtFlashTex = t; continue; } if (t.name.ToLower().Contains("burstflash")) { burstFlashTex = t; continue; } } foreach (var t in textures) { if (t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } string materialPath = matPath + smr.name.ToLower() + ".mat"; if (File.Exists(materialPath)) { continue; } string shader = normalMap != null ? ConstDefineManager.BumpShaderName : ConstDefineManager.BaseShaderName; Material m = new Material(Shader.Find(shader)); m.SetColor("_Color", new Color(0.6f, 0.6f, 0.6f)); m.SetTexture("_MainTex", t); if (normalMap != null) { m.SetTexture("_BumpMap", normalMap); } if (hurtFlashTex != null) { m.SetTexture("_RimLightTex", hurtFlashTex); } if (burstFlashTex != null) { m.SetTexture("_BurstStepTex", burstFlashTex); } AssetDatabase.CreateAsset(m, materialPath); } } return(true); }
/// <summary> /// 生成角色蒙皮预设件 /// </summary> /// <param name="targetModel"></param> /// <param name="errorInfo"></param> /// <returns></returns> public static bool GenerateRoleSkinAsset(GameObject targetModel, out string errorInfo) { if (!CheckModelValid(targetModel, out errorInfo)) { return(false); } var modelName = targetModel.name.Replace(M_MODELPREFIX, ""); string roleSkinPath = SKINEDMESH_PATH + modelName + "/"; if (!Directory.Exists(roleSkinPath)) { Directory.CreateDirectory(roleSkinPath); } DeleteSpecFiles(roleSkinPath); List <Material> materials = EditorHelpers.CollectAll <Material>(MATERIALS_PATH + modelName); foreach (var smr in targetModel.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { SkinMeshRenderHolder smrHolder = ScriptableObject.CreateInstance <SkinMeshRenderHolder>(); smrHolder.Mats = new List <Material>(); smrHolder.Bones = new List <string>(); GameObject rendererClone = (GameObject)PrefabUtility.InstantiatePrefab(smr.gameObject); GameObject rendererParent = rendererClone.transform.parent.gameObject; rendererClone.transform.parent = null; Object.DestroyImmediate(rendererParent); Object rendererPrefab = GetPrefab(rendererClone, roleSkinPath + smr.name + "_smr.prefab"); smrHolder.SMRGO = rendererPrefab; foreach (Material m in materials) { string smrTemp; Debug.Log("smr.name = " + smr.name); if (smr.name.Contains("Low")) { smrTemp = smr.name.Substring(0, smr.name.LastIndexOf("_")); } else { smrTemp = smr.name; } //string smrTemp = smr.name.Substring(0, smr.name.LastIndexOf("_")); //smrTemp = smr.name.Substring(0, "Char01_001_low".LastIndexOf("_")); //Debug.Log("smrTemp = " + smrTemp); //Debug.Log("m = " + m.name); if (m.name.Contains(smrTemp.ToLower())) { smrHolder.Mats.Add(m); } //if (m.name.Contains(smr.name.ToLower())) smrHolder.Mats.Add(m); } //Debug.Log("smrHolder.mats = " + smrHolder.Mats.Count); //List<string> boneNames = new List<string>(); //字符串列表,记录子蒙皮模型对应的骨骼名称清单。 foreach (Transform t in smr.bones) { smrHolder.Bones.Add(t.name); } AssetDatabase.CreateAsset(smrHolder, roleSkinPath + smr.name + ".asset"); } return(true); }
private void SaveUIObj() { //打包UI对象 BuildAssetBundleOptions options = BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies; List <Object> objects = new List <Object>(); List <Object> objectsTexture = new List <Object>(); //game objects string localDir = exportPath + "AssetBundles/"; //must create directory before buildAssetBundle if (Directory.Exists(localDir)) { Directory.Delete(localDir, true); } Directory.CreateDirectory(localDir); BuildPipeline.PushAssetDependencies(); // 打包UI的通用部分 [2/8/2012 Ivan] List <UnityEngine.Object> commonObjs = EditorHelpers.CollectAll <UnityEngine.Object>(uiCommonResPath); string comName = localDir + GetUIPackName("UICommon"); BuildPipeline.BuildAssetBundle( null, commonObjs.ToArray(), comName, options); // 如果是单独打包的话,这里就要弹出依赖项,否则就公用common部分 [3/22/2012 Ivan] if (packUiAlone) { BuildPipeline.PopAssetDependencies(); } BuildPipeline.PushAssetDependencies(); List <UnityEngine.Object> fonts = EditorHelpers.CollectAll <UnityEngine.Object>(uiFontsResPath); string fontName = localDir + GetUIPackName("UIFonts"); BuildPipeline.BuildAssetBundle( null, fonts.ToArray(), fontName, options); List <UnityEngine.Object> uiPrefabs = EditorHelpers.CollectAll <UnityEngine.Object>(uiPrefabPath); UnityEngine.Object[] packPrefab = new UnityEngine.Object[1]; //打包UI对象 for (int i = 0; i < uiPrefabs.Count; i++) { packPrefab[0] = uiPrefabs[i]; if (packPrefab != null && packPrefab[0] != null && (onlyPackWinName == "" || onlyPackWinName == packPrefab[0].name)) { BuildPipeline.PushAssetDependencies(); BuildPipeline.BuildAssetBundle(null, packPrefab, localDir + packPrefab[0].name + ".ui", options); BuildPipeline.PopAssetDependencies(); } } // 弹出Fonts [4/20/2012 Ivan] BuildPipeline.PopAssetDependencies(); // 如果不是单独打包的话,这里才弹出依赖项 [3/22/2012 Ivan] if (!packUiAlone) { BuildPipeline.PopAssetDependencies(); } // 打包Icon部分 [2/17/2012 Ivan] BuildPipeline.PushAssetDependencies(); List <UnityEngine.Object> uiObjs = EditorHelpers.CollectAll <UnityEngine.Object>(uiIconResPath); string uiPackName = localDir + GetUIPackName("Icons"); BuildPipeline.BuildAssetBundle( null, uiObjs.ToArray(), uiPackName, options); BuildPipeline.PopAssetDependencies(); //DestroyAllPrefab(); }
static void Execute() { bool validMaterial = false; //Texture2D causticTex = EditorHelpers.Collect<Texture2D>(TexturesPath(characterFbx)); Texture2D causticTex = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Resources/Textures/caustic_01.png", typeof(Texture2D)); foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)) { if (!(o is GameObject)) { continue; } if (o.name.Contains("@")) { continue; } if (!AssetDatabase.GetAssetPath(o).Contains("/Models/")) { continue; } GameObject characterFbx = (GameObject)o; // Create directory to store generated materials. if (!Directory.Exists(MaterialsPath(characterFbx))) { Directory.CreateDirectory(MaterialsPath(characterFbx)); } // Collect all textures. List <Texture2D> textures = EditorHelpers.CollectAll <Texture2D>(TexturesPath(characterFbx)); // Create materials for each SkinnedMeshRenderer. foreach (SkinnedMeshRenderer smr in characterFbx.GetComponentsInChildren <SkinnedMeshRenderer>(true)) { // Check if this SkinnedMeshRenderer has a normalmap. Texture2D normalmap = null; foreach (Texture2D t in textures) { if (!t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } normalmap = t; break; } // Create a Material for each texture which name contains // the SkinnedMeshRenderer name. foreach (Texture2D t in textures) { if (t.name.ToLower().Contains("normal")) { continue; } if (!t.name.ToLower().Contains(smr.name.ToLower())) { continue; } validMaterial = true; string materialPath = MaterialsPath(characterFbx) + "/" + t.name + ".mat"; // Dont overwrite existing materials, as we would // lose existing material settings. if (File.Exists(materialPath)) { continue; } // Use a default shader according to artist preferences. string shader = "Custom/CommonFish";//"Render Depth";// "Mobile/Unlit (Supports Lightmap)";// "Custom/NLitTextureColorAlpha"; if (normalmap != null) { shader = "Custom/CommonFish"; //"Render Depth";//"Mobile/Unlit (Supports Lightmap)";//"Custom/NLitTextureColorAlpha"; } if (smr.name.Equals("11023") || smr.name.Equals("12003") || smr.name.Equals("13009") || smr.name.Equals("13017")) { //水母用特殊材质 shader = "Custom/NLitTextureColorAlpha"; // Create the Material Material m = new Material(Shader.Find(shader)); m.SetTexture("_MainTex", t); if (normalmap != null) { m.SetTexture("_BumpMap", normalmap); } AssetDatabase.CreateAsset(m, materialPath); } else { //鱼用变通材质 // Create the Material Material m = new Material(Shader.Find(shader)); m.SetTexture("_mainTex", t); m.SetTexture("_causticTex", causticTex); m.SetFloat("_intencity", 0.7f); m.SetFloat("_scrollSpeedY", -0.2f); //m.DisableKeyword("USING_CAUSTIC_OFF"); m.EnableKeyword("USING_CAUSTIC"); if (normalmap != null) { m.SetTexture("_BumpMap", normalmap); } AssetDatabase.CreateAsset(m, materialPath); } } } } AssetDatabase.Refresh(); if (!validMaterial) { EditorUtility.DisplayDialog("Character Generator", "No Materials created. Select the characters folder in the Project pane to process all characters. Select subfolders to process specific characters.", "Ok"); } }