簡介:

基本上就是把 很多物體結合成一個物體 的作法,這種做法有很多優點,例如:
1. 提高效能
2. 統一材質 (只要建立一個材質,就能控制、分配給所有物體)
3. 動畫控制方便 (像是你要在 Unity 做人物換衣服、換物件 卻還要有 人物走路動畫 + 人物攻擊動畫 ...等等 ,
    將不同衣服、武器、褲子、人物骨架...等等結合成一個物體,再由同一組骨架控制動畫。
    這就能夠實現不同組合角色,卻可以做一樣的動畫、動作)

入門教學步驟:

1. 先在 Unity 中建立 空物件 ( Empty ) 
2. 再創建兩個 Cube 方塊,並放入 空物件底下 (可以改成你自己的模型)
3. 把 MyClass 程式碼丟進 空物件上 ( MyClass 程式碼在文章下面)
4. (可選) 建立一個 Material 材質,並且丟進 空物件上
5. 執行

執行前:

Unity - A.unity - New Unity Project 2 - PC, Mac & Linux Standalone  

執行後:

Unity - A.unity - New Unity Project 2 - PC, Mac & Linux Standalone2  


// MyClass 程式碼
// 把程式碼丟到最上層的"空物體"中

using UnityEngine;
using System.Collections;

public class MyClass : MonoBehaviour
{
    void Start ()
    {
        MeshFilter [] meshFilters = GetComponentsInChildren<MeshFilter> ();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];

        for (int i = 0; i < meshFilters.Length; i++) {
            combine [i].mesh = meshFilters [i].sharedMesh;
            combine [i].transform = meshFilters [i].transform.localToWorldMatrix;
            meshFilters [i].gameObject.active = false;
        }

            transform.GetComponent<MeshFilter> ().mesh = new Mesh ();
            transform.GetComponent<MeshFilter> ().mesh.CombineMeshes (combine);
            transform.gameObject.active = true;
    }
}

 

 

 

 

 

arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(2) 人氣()