characterCustomezition的资源打包代码分析
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine; class CreateAssetbundles
{
// This method creates an assetbundle of each SkinnedMeshRenderer
// found in any selected character fbx, and adds any materials that
// are intended to be used by the specific SkinnedMeshRenderer.
[MenuItem("Character Generator/Create Assetbundles")]
static void Execute()
{
bool createdBundle = false;
foreach (Object o in Selection.GetFiltered(typeof (Object), SelectionMode.DeepAssets))//返回通过类型和选择模式过滤的当前选择的物体。 {
if (!(o is GameObject)) continue;//如果不是GameObject就跳过本次循环
if (o.name.Contains("@")) continue;//如果是动画片段就跳过本次循环
if (!AssetDatabase.GetAssetPath(o).Contains("/characters/")) continue;//如果含有指定的目录名就跳过本次循环 GameObject characterFBX = (GameObject)o;//将o强制转换为GameObject
string name = characterFBX.name.ToLower();//获取名字 Debug.Log("******* Creating assetbundles for: " + name + " *******"); // Create a directory to store the generated assetbundles.
if (!Directory.Exists(AssetbundlePath))//检查AssetbundlePath是否存在
Directory.CreateDirectory(AssetbundlePath);//如果不存在就创建目录 // Delete existing assetbundles for current character.
string[] existingAssetbundles = Directory.GetFiles(AssetbundlePath);//获取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);//克隆一个GO
foreach (SkinnedMeshRenderer smr in characterClone.GetComponentsInChildren<SkinnedMeshRenderer>())//得到子物体的SkinnedMeshRenderer组件不包括Inactive
Object.DestroyImmediate(smr.gameObject);//销毁资源
characterClone.AddComponent<SkinnedMeshRenderer>();//添加SkinnedMeshRenderer组件到Clone体
Object characterBasePrefab = GetPrefab(characterClone, "characterbase");//得到一个预制件,并销毁clone体
string path = AssetbundlePath + name + "_characterbase.assetbundle";//路径及文件名
BuildPipeline.BuildAssetBundle(characterBasePrefab, null, path, BuildAssetBundleOptions.CollectDependencies);//建一个压缩的unity3d文件,包含资源的集合
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(characterBasePrefab));//销毁预制件 // Collect materials.
List<Material> materials = EditorHelpers.CollectAll<Material>(GenerateMaterials.MaterialsPath(characterFBX));//获取fbx目录下的所有Material // Create assetbundles for each SkinnedMeshRenderer.
foreach (SkinnedMeshRenderer smr in characterFBX.GetComponentsInChildren<SkinnedMeshRenderer>(true))//获取fbx及子物体的SkinnedMeshRenderer组件包括Inactive
{
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);//clone给定的预制件
GameObject rendererParent = rendererClone.transform.parent.gameObject;//获取父对象
rendererClone.transform.parent = null;//清空clone体的父对象引用
Object.DestroyImmediate(rendererParent);//摧毁父对象
Object rendererPrefab = GetPrefab(rendererClone, "rendererobject");//得到一个预制件,并销毁clone体
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)));//返回在指定位置stringholderpath下第一个类型是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 - ) + " 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 Object GetPrefab(GameObject go, string name)
{
Object tempPrefab = EditorUtility.CreateEmptyPrefab("Assets/" + name + ".prefab");//创建一个empty预制件
tempPrefab = EditorUtility.ReplacePrefab(go, tempPrefab);//将GO替换为tmpPrefab
Object.DestroyImmediate(go);//销毁资源
return tempPrefab;//返回tmpPrefab
} public static string AssetbundlePath
{
get { return "assetbundles" + Path.DirectorySeparatorChar; }
}
}
原地址:http://www.cnblogs.com/hisiqi/p/3203473.html
characterCustomezition的资源打包代码分析的更多相关文章
- 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)
构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...
- Unity资源打包学习笔记(二)、如何实现高效的unity AssetBundle热更新
转载请标明出处:http://www.cnblogs.com/zblade/ 0x01 目的 在实际的游戏开发中,对于游戏都需要进行打补丁的操作,毕竟,测试是有限的,而bug是无法预估的.那么在手游中 ...
- Unity资源打包学习笔记(一)、详解AssetBundle的流程
转载请标明出处:http://www.cnblogs.com/zblade/ 本文参照unity官网上对于assetBundle的一系列讲解,主要针对assetbundle的知识点做一个梳理笔记,也为 ...
- 详解vue静态资源打包中的坑与解决方案
本文主要解决: 1.vue-cli默认配置打包后部署至特定路径下静态资源路径错误问题; 2.静态资源打包使用相对路径后css文件引入图片路径错误问题. 一.问题 vue-cli 脚手架生成的默认打包配 ...
- 跟我从零基础学习Unity3D开发--资源打包篇(AssetBundle)
好久没更新了,一直在加班敢项目进度.这里和关注我的博客的童鞋表示一下歉意!这里有我录的Unity3D从零开始的视频教程大家可以关注一下:http://www.imooc.com/view/555 视 ...
- 常用 Java 静态代码分析工具的分析与比较
常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...
- [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(3)
这个系列已经写了5篇,链接地址如下: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Auto ...
- Unity手游之路<十一>资源打包Assetbundle
http://blog.csdn.net/janeky/article/details/17652021 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制 ...
- wifi display代码 分析
转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTS ...
随机推荐
- nodejs简易实现一下bigpipe
今天刚好看到bigpipe的文章,写个demo试一下: nodejs的实现: var fs = require('fs'); module.exports = function(req , res){ ...
- Git.Framework 框架随手记--ORM查询数据集合 二
一. 分组查询 在SQL中使用Group By 来对数据分组,在实际中分组中一般与聚合函数一并使用.在Git.Framework中提供了相应的分组方法 DataTable Group(T entit ...
- (旧)子数涵数·Flash——遮罩动画
一.打开Flash,并新建一个flash文档 二.然后,用矩形工具,快捷键R,绘制一个背景图 三.新建一个图层 四.右击刚刚新建的"图层2"图层,选择"遮罩层" ...
- 理解C#事件
前面文章中介绍了委托相关的概念,委托实例保存这一个或一组操作,程序中将在某个特定的时刻通过委托实例使用这些操作. 如果做过GUI程序开发,可能对上面的描述会比较熟悉.在GUI程序中,单击一个butto ...
- 10、面向对象以及winform的简单运用(isMdicontainer的设置、timer控件进行倒计时的制作)
IsMdicontainer的设置 这是对于整个窗体的设置,将一个窗体的IsMdicontainer设置为true之后,再打开新窗体便可以让新窗体被父容器包括在内. 操作方法: 1)先建立一个子窗体C ...
- Jquery-EasyUI学习~
为了回顾,简单记录下EasyUI如何使用: 先来张效果图: 这张图是从后台获取数据,然后进行展示的. 我这里利用的是EF-MVC. 先说下View视图里面的HTML代码是如何写的: @{ ViewBa ...
- sublime text下代码太长brackethighlighter不能正确显示闭合高亮的解决方法
用brackethighlighter显示高亮一直都有这个问题...也没在网上找到解决方案,就一直凑合着用,今天翻着配置文件玩,改了参数发现问题解决了...... 修改search_threshold ...
- Ajax实现联动效果
用到了地区的数据库 html代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="s ...
- MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决
把自己的项目从MVC4升级到了MVC5,结果问题一大堆,View的设计环境出现了"当前上下文中不存在ViewBag"的问题: 虽然不影响编译,但是看了总是不爽,而且语法提示也没有了 ...
- Json-转换
js转换 引用json.js(将json格式转换成字符串 var name = document.getElementById("name").value; var retries ...