public void Initialize() { if (EflList.Count > 0) {//already inited. return; } //fixed : 2012.5.25. can't add child game object during foreach.. //ArrayList tobeAdded = new ArrayList(); foreach (Transform child in transform) { EffectLayer el = (EffectLayer)child.GetComponent(typeof(EffectLayer)); if (el == null) { continue; } if (el.Material == null) { //Debug.LogWarning("effect layer: " + el.gameObject.name + " don't have a material, please assign a material first!"); continue; } Material mat = el.Material; //ver 1.2.1 mat.renderQueue = mat.shader.renderQueue; mat.renderQueue += el.Depth; EflList.Add(el); MeshFilter Meshfilter; MeshRenderer Meshrenderer; Transform oldMesh = transform.Find("mesh " + mat.name); if (oldMesh != null) {//already instaniate by object cache.just recreate vertex pool.\ Meshfilter = (MeshFilter)oldMesh.GetComponent(typeof(MeshFilter)); Meshrenderer = (MeshRenderer)oldMesh.GetComponent(typeof(MeshRenderer)); Meshfilter.sharedMesh = new Mesh(); Meshfilter.sharedMesh.Clear(); MatDic[mat.name] = new VertexPool(Meshfilter.sharedMesh, mat); //fixed 2012.7.20 if already exist, Add to MeshList too. if (!MeshList.Contains(oldMesh.gameObject)) { MeshList.Add(oldMesh.gameObject); } } if (!MatDic.ContainsKey(mat.name)) { GameObject obj = new GameObject("mesh " + mat.name); //obj.transform.parent = this.transform; //tobeAdded.Add(obj); MeshList.Add(obj); obj.AddComponent("MeshFilter"); obj.AddComponent("MeshRenderer"); Meshfilter = (MeshFilter)obj.GetComponent(typeof(MeshFilter)); Meshrenderer = (MeshRenderer)obj.GetComponent(typeof(MeshRenderer)); Meshrenderer.castShadows = false; Meshrenderer.receiveShadows = false; Meshrenderer.renderer.sharedMaterial = mat; Meshfilter.sharedMesh = new Mesh(); MatDic[mat.name] = new VertexPool(Meshfilter.sharedMesh, mat); } } //now set each gameobject's parent. foreach (GameObject obj in MeshList) { obj.transform.parent = this.transform; //fixed 2012.6.25, obj.transform.position = Vector3.zero; obj.transform.rotation = Quaternion.identity; //fixed 2012.7.11, avoid the lossy scale influence the mesh object. Vector3 realLocalScale = Vector3.zero; realLocalScale.x = 1 / obj.transform.parent.lossyScale.x; realLocalScale.y = 1 / obj.transform.parent.lossyScale.y; realLocalScale.z = 1 / obj.transform.parent.lossyScale.z; obj.transform.localScale = realLocalScale * Scale; } //start eah effect layer. foreach (EffectLayer efl in EflList) { efl.Vertexpool = MatDic[efl.Material.name]; } //this.transform.position = Vector3.zero; //this.transform.rotation = Quaternion.identity; this.transform.localScale = Vector3.one; foreach (EffectLayer el in EflList) { el.StartCustom(); } Initialized = true; }