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 - 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 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的资源打包代码分析,布布扣,bubuko.com

时间: 2024-10-11 23:07:45

characterCustomezition的资源打包代码分析的相关文章

完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)

构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式进行配置,所以维护起来相当困难.Gradle:Gradle采用增量构建.Gradle通过Groovy编程而不是传统的XML声明进行配置.Gradle可以很好地配合Maven进行依赖管理,并且把Ant脚本当作头等公民.字节码操作 编程操作Java字节码的函数库. ASM:通用底层字节码操作及分析

Java静态代码分析工具Infer

Java静态代码分析工具Infer 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.Infer介绍 Infer是Facebook最新开源的静态程序分析工具,用于在发布移动应用之前对代码进行分析,找出潜在的问题.目前Facebook使用此工具分析Facebook的App,包括Android.iOS.Facebook Messenger和Instagram等. Facebook称该工具帮助其每个月检查出应用潜在的数百个Bug,例如一些空指针访问.资源

常用 Java 静态代码分析工具的分析与比较

转载自: http://www.oschina.net/question/129540_23043 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBugs,PMD,Jtest),最后从功能.特性等方面对它们进行分析和比较,希望能够帮助 Java 软件开发人员了解静态代码分析工具,并选择合适的工具应用到软件开发中. 引言 在 Java 软件开发过程中,开发团队往往要花费大量的时间和精力发现并修改代

NodeManager代码分析之NodeManager启动过程

1.NodeManager概述 NodeManager(NM)是YARN中每个节点上的代理,它管理Hadoop集群中单个计算节点,包括与ResourceManger保持通信,监督Container的生命周期管理,监控每个Container的资源使用(内存.CPU等)情况,追踪节点健康状况,管理日志和不同应用程序用到的附属服务. NodeManager整体架构: 2.NodeManager分析 接下来将按照启动NodeManager时代码执行的顺序为主线进行代码分析. 2.1 main函数 打印N

《linux 内核完全剖析》 exit.c 代码分析笔记

exit.c 代码分析笔记 release 释放进程的函数release() 主要根据指定进程的任务数据结构指针,在任务数组中删除指定的进程指针,释放相关内存页,并立刻让内核重新调度进程的运行. void release(struct task_struct * p) //释放p指向的进程 { int i; if (!p) //常规检测p是否为0 return; if (p == current) { //不能把自己给释放了 printk("task releasing itself\n\r&q

wifi display代码 分析

转自:http://blog.csdn.net/lilian0118/article/details/23168531 这一章中我们来看Wifi Display连接过程的建立,包含P2P的部分和RTSP的部分,首先来大致看一下Wifi Display规范相关的东西. HIDC: Human Interface Device Class  (遵循HID标准的设备类)UIBC: User Input Back Channel  (UIBC分为两种,一种是Generic,包含鼠标.键盘等:另一种是HI

恶意代码分析——动、静态分析基础技术

一.静态分析基础技术 1.可通过用软件计算恶意程序MD5值,然后检索该MD5值来获取信息并作为标签使用  [md5deep  winmd5] 2.通过检索恶意代码字符串获得相应的功能调用解释.功能行为及模块调用.当可检索字符串非常少时,有可能被加壳处理,(注意"LoadLibrary"和"GetProcAddress"两个字符串,它们是用来加载或调用其他函数功能的),此时需要用外壳检测工具进行检测.脱壳处理  [字符串检索:Strings  外壳检测:PEiD] 3

[Asp.net 5] DependencyInjection项目代码分析4-微软的实现(3)

这个系列已经写了5篇,链接地址如下: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Autofac [Asp.net 5] DependencyInjection项目代码分析3-Ninject [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(1) [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(2) 如果想

Unity资源打包之Assetbundle

转  Unity资源打包之Assetbundle 本文原创版权归 csdn janeky 所有,转载请详细注明原创作者及出处,以示尊重! 作者:janeky 原文:http://blog.csdn.net/janeky/article/details/17652021 如果这篇文章对你有帮助,敬请关注作者<Unity手游之路>系列教程. 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制成预设Prefab,打包成场景.今天我们来一起学习官方推荐的As