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
ponent
StringHolder holder = Instance
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
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
("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 条评论) |