1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
public
class
NewBehaviourScript : MonoBehaviour {
public
Avatar a;
// Use this for initialization
void
Start () {
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
Debug.Log(
"meshFilters:"
+ meshFilters.Length);
CombineInstance[] combine =
new
CombineInstance[meshFilters.Length];
MeshRenderer[] meshRendererList = GetComponentsInChildren<MeshRenderer>();
//获取自身和所有子物体中所有MeshRenderer组件
Material[] mats =
new
Material[meshRendererList.Length];
//新建材质球数组
Texture2D[] texList =
new
Texture2D[meshRendererList.Length];
int
i = 0;
while
(i < meshFilters.Length)
{
//mats[meshFilters.Length-i-1] = meshRendererList[i].sharedMaterial; //获取材质球列表
mats[i] = meshRendererList[i].sharedMaterial;
//
Color c = mats[i].color;
/*
* */
Texture2D tx = mats[i].GetTexture(
"_MainTex"
)
as
Texture2D;
Texture2D tx2D =
new
Texture2D(tx.width, tx.height, TextureFormat.ARGB32,
false
);
tx2D.SetPixels(tx.GetPixels(0, 0, tx.width, tx.height));
tx2D.Apply();
//
texList[i] = tx2D;
combine[i].mesh = meshFilters[i].sharedMesh;
//combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
combine[i].transform = transform.worldToLocalMatrix * meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.SetActive(
false
);
i++;
}
//
string
shName = mats[0].shader.name;
Material matNew =
new
Material(mats[0].shader);
matNew.CopyPropertiesFromMaterial(mats[0]);
Texture2D texNew =
new
Texture2D(1024, 1024);
matNew.SetTexture(
"_MainTex"
, texNew);
Rect[] rects = texNew.PackTextures(texList, 0);
//, 2048);
//
GameObject quad = GameObject.Find(
"Quad"
);
quad.GetComponent<MeshRenderer>().material.mainTexture = texNew;
Mesh meshNew =
new
Mesh();
//
i = 0;
while
(i < meshFilters.Length)
{
int
uvLen = combine[i].mesh.uv.Length;
Vector2[] uvData = combine[i].mesh.uv;
for
(
int
j = 0;j< uvLen;j++)
{
uvData[j].x = uvData[j].x * rects[i].width + rects[i].x;
uvData[j].y = uvData[j].y * rects[i].height + rects[i].y;
}
//直接设不行
//combine[i].mesh.uv = uvData;
meshFilters[i].mesh.uv = uvData;
combine[i].mesh = meshFilters[i].mesh;
i++;
}
transform.GetComponent<MeshFilter>().mesh = meshNew;
transform.GetComponent<MeshFilter>().name =
"xxx"
;
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine,
true
);
//,truefalse);////
//为合并后的GameObject指定材质
//transform.GetComponent<MeshRenderer>().sharedMaterials =mats;// materialNew;//
transform.GetComponent<MeshRenderer>().sharedMaterial = matNew;
transform.gameObject.SetActive(
true
);
}
// Update is called once per frame
void
Update () {
}
}
|
本文转自jiahuafu博客园博客,原文链接http://www.cnblogs.com/jiahuafu/p/6718069.html如需转载请自行联系原作者
jiahuafu