首页 > 试题

Unity3d官方角色换装Demo源代码学习

更新时间:2024-12-23 16:36:38 阅读: 评论:0

2023年12月11日发(作者:什么样的目光)

Unity3d官方角色换装Demo源代码学习

由于兴趣爱好,工作之余会尝试做做RPG游戏,遇到角色换装这个功能没什么思路,于是在网上苦苦找寻相关资料,网上角色换装文章

还是有一些,不过大多讲的比较简单,最后没办法不得不拿官方的角色换装代码来研究,开始看到一头雾水,不过慢慢找到看这个Demo代

码的思路,终于理清了换装的思路。

其实,可以从角色换装资源打包代码看起,然后看换装代码。

以下是我看代码的思路: ->

换装的核心代码如下:

(1)打包代码

using c;

using ;

using UnityEditor;

using UnityEngine;

class CreateAstbundles

{

// This method creates an astbundle of each SkinnedMeshRenderer

// found in any lected character fbx, and adds any materials that

// are intended to be ud by the specific SkinnedMeshRenderer.

[MenuItem("Character Generator/Create Astbundles")]

static void Execute()

{

bool createdBundle = fal;

foreach (Object o in tered(typeof (Object), ts))

{

if (!(o is GameObject)) continue;

if (ns("@")) continue;

if (!etPath(o).Contains("/characters/")) continue;

GameObject characterFBX = (GameObject)o;

string name = r();

("******* Creating astbundles for: " + name + " *******");

// Create a directory to store the generated astbundles.

if (!(AstbundlePath))

Directory(AstbundlePath);

// Delete existing astbundles for current character.

string[] existingAstbundles = es(AstbundlePath);

foreach (string bundle in existingAstbundles)

{

if (th(".astbundle") && ns("/astbundles/" + name))

(bundle);

}

// Save bones and animations to a perate astbundle. Any

// possible combination of CharacterElements will u the

// asts as a ba. As we can not edit asts we instantiate

// the fbx and remove what we dont need. As only asts can be

// added to astbundles we save the result as a prefab and delete

// it as soon as the astbundle is created.

GameObject characterClone = (GameObject)tiate(characterFBX);

// postprocess animations: we need them animating even offscreen

foreach (Animation anim in ponentsInChildren())

eOnlyIfVisible = fal; foreach (SkinnedMeshRenderer smr in ponentsInChildren()) yImmediate(ject);

ponent(); Object characterBaPrefab = GetPrefab(characterClone, "characterba"); string path = AstbundlePath + name + "_undle"; stBundle(characterBaPrefab, null, path, tDependencies); Ast(etPath(characterBaPrefab)); // Collect materials. List materials = tAll(alsPath(characterFBX)); // Create astbundles for each SkinnedMeshRenderer. foreach (SkinnedMeshRenderer smr in ponentsInChildren(true)) { List toinclude = new List(); // Save the current SkinnedMeshRenderer as a prefab so it can be included // in the astbundle. As instantiating part of an fbx results in the // entire fbx being instantiated, we have to dispo of the entire instance // after we detach the SkinnedMeshRenderer in question. GameObject rendererClone = (GameObject)tiatePrefab(ject); GameObject rendererParent = ject; = null; yImmediate(rendererParent); Object rendererPrefab = GetPrefab(rendererClone, "rendererobject"); (rendererPrefab); // Collect applicable materials. foreach (Material m in materials) if (ns(r())) (m); // When asmbling a character, we load SkinnedMeshRenderers from astbundles, // and as such they have lost the references to their bones. To be able to // remap the SkinnedMeshRenderers to u the bones from the characterba astbundles, // we save the names of the bones ud. List boneNames = new List(); foreach (Transform t in ) (); string stringholderpath = "Asts/";

StringHolder holder = Instance (); t = y(); Ast(holder, stringholderpath); (tAtPath(stringholderpath, typeof (StringHolder))); // Save the astbundle. string bundleName = name + "_" + r(); path = AstbundlePath + bundleName + ".astbundle"; stBundle(null, y(), path, tDependencies); ("Saved " + bundleName + " with " + ( - 2) + " materials"); // Delete temp asts. Ast(etPath(rendererPrefab)); Ast(stringholderpath); createdBundle = true; } } if (createdBundle) e(); el } yDialog("Character Generator", "No Ast Bundles created. Select the characters folder in the Project pane to process all characters. Se

static Object GetPrefab(GameObject go, string name)

{

Object tempPrefab = EmptyPrefab("Asts/" + name + ".prefab");

tempPrefab = ePrefab(go, tempPrefab);

yImmediate(go);

return tempPrefab;

}

public static string AstbundlePath

{

get { return "astbundles" + orySeparatorChar; }

}

}

(2)角色换装代码 // Creates a character bad on the currentConfiguration recycling a // character ba, this way the position and animation of the character // are not changed. public GameObject Generate(GameObject root) { float startTime = meSinceStartup; // The SkinnedMeshRenderers that will make up a character will be // combined into one SkinnedMeshRenderers using multiple materials. // This will speed up rendering the resulting character. List combineInstances = new List(); List materials = new List(); List bones = new List(); Transform[] transforms = ponentsInChildren();

foreach (CharacterElement element in ) { SkinnedMeshRenderer smr = nnedMeshRenderer(); ge(als); for (int sub = 0; sub < hCount; sub++) { CombineInstance ci = new CombineInstance(); = Mesh; hIndex = sub; (ci); } // As the SkinnedMeshRenders are stored in astbundles that do not // contain their bones (tho are stored in the characterba astbundles) // we need to collect references to the bones we are using foreach (string bone in eNames()) { foreach (Transform transform in transforms) { if ( != bone) continue; (transform); break; } } y(ject); } // Obtain and configure the SkinnedMeshRenderer attached to // the character ba. SkinnedMeshRenderer r = ponent(); Mesh = new Mesh(); eMeshes(y(), fal, fal); = y(); als = y();

("Generating character took: " + (meSinceStartup - startTime) * 1000 + " ms"); return root; }

本文发布于:2023-12-11 19:14:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/88/41124.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Unity3d官方角色换装Demo源代码学习.doc

本文 PDF 下载地址:Unity3d官方角色换装Demo源代码学习.pdf

标签:换装   角色   代码   官方   源代码
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|
  • 我要关灯
    我要开灯
  • 返回顶部