/// <summary> /// Changes the user interface mesh. /// </summary> /// <param name="um">Um.</param> /// <param name="texture">Texture.</param> /// <param name="mat">Mat.</param> public void ChangeUIMesh(UIMesh um, Texture texture, Material mat = null) { if (!um || !texture) { return; } TextureFrame frame = new TextureFrame(); frame.uiMaterial = mat == null ? um.material : mat; frame.texture = texture; frame.isRotated = false; frame.rect.x = 0; frame.rect.y = 0; frame.rect.width = texture.width; frame.rect.height = texture.height; frame.frameSize = new Rect(0, 0, texture.width, texture.height); frame.atlasTextureSize.x = texture.width; frame.atlasTextureSize.y = texture.height; if (!um.isCreatedMesh) { um.CreateMesh(); } um.frame = frame; if (mat) { um.material = mat; } //change texture um.texture = texture; }
static void ShowUIMesh(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor) { GameObject go = new GameObject(attachmentData.name); UIMesh sm = go.AddComponent <UIMesh>(); sm.raycastTarget = false; sm.transform.SetParent(skinParent); Vector3 localPos = Vector3.zero; localPos.x = attachmentData.x; localPos.y = attachmentData.y; go.transform.localPosition = localPos; Vector3 localSc = Vector3.one; localSc.x = attachmentData.scaleX; localSc.y = attachmentData.scaleY; go.transform.localScale = localSc; go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation); Transform[] bones = SetMeshVertex <UIMesh>(sm, attachmentData, armatureEditor); sm.uvs = attachmentData.uvs; sm.triangles = attachmentData.triangles; sm.colors = new Color32[sm.vertices.Length]; for (int i = 0; i < sm.colors.Length; ++i) { sm.colors[i] = Color.white; } if (armatureEditor.genMeshCollider && attachmentData.edges != null) { sm.edges = attachmentData.edges; } if (attachmentData.weights != null && attachmentData.weights.Count > 0) { sm.CreateMesh(); if (armatureEditor.ffdKV.ContainsKey(attachmentData.textureName)) { //Vertex controller sm.vertControlTrans = new Transform[sm.vertices.Length]; for (int i = 0; i < sm.vertices.Length; ++i) { GameObject gov = new GameObject(go.name + "_v" + i); gov.transform.parent = go.transform; gov.transform.localPosition = sm.vertices[i]; gov.transform.localScale = Vector3.zero; sm.vertControlTrans[i] = gov.transform; gov.SetActive(false); } } } else { sm.CreateMesh(); //Vertex controller sm.vertControlTrans = new Transform[sm.vertices.Length]; for (int i = 0; i < sm.vertices.Length; ++i) { GameObject gov = new GameObject(go.name + "_v" + i); gov.transform.parent = go.transform; gov.transform.localPosition = sm.vertices[i]; gov.transform.localScale = Vector3.zero; sm.vertControlTrans[i] = gov.transform; gov.SetActive(false); } } if (attachmentData.weights != null && attachmentData.weights.Count > 0) { List <Armature.BoneWeightClass> boneWeights = new List <Armature.BoneWeightClass>(); for (int i = 0; i < attachmentData.weights.Count; ++i) { int boneCount = (int)attachmentData.weights[i]; //骨骼数量 List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >(); for (int j = 0; j < boneCount * 2; j += 2) { int boneIdx = (int)attachmentData.weights[i + j + 1]; float weight = attachmentData.weights[i + j + 2]; boneWeightList.Add(new KeyValuePair <int, float>(boneIdx, weight)); } //sort boneWeightList,desc boneWeightList.Sort(delegate(KeyValuePair <int, float> x, KeyValuePair <int, float> y) { if (x.Value == y.Value) { return(0); } return(x.Value < y.Value? 1: -1); }); Armature.BoneWeightClass bw = new Armature.BoneWeightClass(); for (int j = 0; j < boneWeightList.Count; ++j) { if (j == 0) { bw.boneIndex0 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight0 = boneWeightList[j].Value; } else if (j == 1) { bw.boneIndex1 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight1 = boneWeightList[j].Value; } else if (j == 2) { bw.boneIndex2 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight2 = boneWeightList[j].Value; } else if (j == 3) { bw.boneIndex3 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight3 = boneWeightList[j].Value; } else if (j == 4) { bw.boneIndex4 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones); bw.weight4 = boneWeightList[j].Value; } } boneWeights.Add(bw); i += boneCount * 2; } Matrix4x4[] matrixArray = new Matrix4x4[bones.Length]; for (int i = 0; i < matrixArray.Length; ++i) { Transform bone = bones[i]; matrixArray[i] = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix; matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale); } sm.bones = bones; sm.bindposes = matrixArray; sm.weights = boneWeights.ToArray(); } sm.color = attachmentData.color; sm.UpdateMesh(); sm.UpdateVertexColor(); sm.frame = frame; }