2023年12月11日發(fā)(作者:什么樣的目光)
Unity3d官方角色換裝Demo源代碼學(xué)習(xí)
由于興趣愛好,工作之余會(huì)嘗試做做RPG游戲,遇到角色換裝這個(gè)功能沒什么思路,于是在網(wǎng)上苦苦找尋相關(guān)資料,網(wǎng)上角色換裝文章
還是有一些,不過大多講的比較簡(jiǎn)單,最后沒辦法不得不拿官方的角色換裝代碼來研究,開始看到一頭霧水,不過慢慢找到看這個(gè)Demo代
碼的思路,終于理清了換裝的思路。
其實(shí),可以從角色換裝資源打包代碼看起,然后看換裝代碼。
以下是我看代碼的思路: ->
換裝的核心代碼如下:
(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; }
本文發(fā)布于:2023-12-11 19:14:07,感謝您對(duì)本站的認(rèn)可!
本文鏈接:http://www.newhan.cn/zhishi/a/88/41124.html
版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請(qǐng)勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除。
本文word下載地址:Unity3d官方角色換裝Demo源代碼學(xué)習(xí).doc
本文 PDF 下載地址:Unity3d官方角色換裝Demo源代碼學(xué)習(xí).pdf
| 留言與評(píng)論(共有 0 條評(píng)論) |