示例#1
0
        /// <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;
        }
示例#2
0
        /// <summary>
        /// Changes the user interface mesh.
        /// </summary>
        /// <param name="uiMeshName">User interface mesh name.</param>
        /// <param name="texture">Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeUIMesh(string uiMeshName, Texture texture, Material mat = null)
        {
            MaskableGraphic attachment = GetUIAttachmentByName(uiMeshName);

            if (!attachment)
            {
                return;
            }
            UIMesh um = attachment.GetComponent <UIMesh>();

            ChangeUIMesh(um, texture, mat);
        }
示例#3
0
        /// <summary>
        /// Changes the user interface mesh.
        /// </summary>
        /// <param name="uiMeshName">User interface mesh name.</param>
        /// <param name="newTextureFrameName">New texture frame name.</param>
        public void ChangeUIMesh(string uiMeshName, string newTextureFrameName)
        {
            MaskableGraphic attachment = GetUIAttachmentByName(uiMeshName);

            if (!attachment)
            {
                return;
            }

            UIMesh       um    = attachment.GetComponent <UIMesh>();
            TextureFrame frame = textureFrames.GetTextureFrame(newTextureFrameName);

            if (um != null && frame != null)
            {
                um.frame = frame;
            }
        }
 void OnEnable()
 {
     um = target as UIMesh;
 }
示例#5
0
        protected internal void UpdateCurrentDisplay()
        {
            m_SpriteFrame    = null;
            m_SpriteMesh     = null;
            m_SpriteRenderer = null;
            m_Armature       = null;
            m_Image          = null;
            m_UIFrame        = null;
            m_UIMesh         = null;
            m_CurrentDisplay = null;
            if (armature == null)
            {
                return;
            }

            if (__displayIndex > -1 && transform.childCount > 0)
            {
                Transform skin = (armature.skins == null || armature.skins.Length <= 1) ?transform :transform.Find(armature.skinName);
                if (skin && skin.childCount > 0)
                {
                    Transform child = skin.GetChild(__displayIndex);
                    m_CurrentDisplay = child.gameObject;
                    if (armature.isUGUI)
                    {
                        m_UIFrame = child.GetComponent <UIFrame>();
                        if (!m_UIFrame)
                        {
                            m_UIMesh = child.GetComponent <UIMesh>();
                            if (!m_UIMesh)
                            {
                                m_Image = child.GetComponent <Image>();
                                if (!m_Image)
                                {
                                    m_Armature = child.GetComponent <Armature>();
                                }
                            }
                        }
                    }
                    else
                    {
                        m_SpriteFrame = child.GetComponent <SpriteFrame>();
                        if (!m_SpriteFrame)
                        {
                            m_SpriteMesh = child.GetComponent <SpriteMesh>();
                            if (!m_SpriteMesh)
                            {
                                m_SpriteRenderer = child.GetComponent <SpriteRenderer>();
                                if (!m_SpriteRenderer)
                                {
                                    m_Armature = child.GetComponent <Armature>();
                                }
                            }
                        }
                        UpdateZOrder();
                    }
                }
            }
            else if (armature.skins != null && armature.skins.Length > 1)
            {
                for (int i = 0; i < transform.childCount; ++i)
                {
                    transform.GetChild(i).gameObject.SetActive(false);
                }
            }
        }
示例#6
0
        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;
        }
示例#7
0
        //init
        public void InitShow()
        {
            DragonBoneShowArmature.AddBones(this);
            DragonBoneShowArmature.AddSlot(this);
            DragonBoneShowArmature.ShowBones(this);
            DragonBoneShowArmature.ShowSlots(this);
            DragonBoneShowArmature.ShowSkins(this);
            DragonBoneShowArmature.SetIKs(this);
            DragonBoneAnimFile.CreateAnimFile(this);

            Armature armature = _armature.GetComponent <Armature>();

            m_prefabs.Add(_armature.gameObject);
            armature.textureFrames = m_TextureFrames;

            //update slot display
            for (int s = 0; s < m_slots.Count; ++s)
            {
                m_slots[s].displayIndex = m_slots[s].displayIndex;
            }

            if (armature.isUGUI)
            {
                UnityEngine.UI.MaskableGraphic[] renders = _armature.GetComponentsInChildren <UnityEngine.UI.MaskableGraphic>(true);
                armature.uiAttachments = renders;
                armature.attachments   = new Renderer[0];
            }
            else
            {
                Renderer[] renders = _armature.GetComponentsInChildren <Renderer>(true);
                armature.attachments   = renders;
                armature.uiAttachments = new UnityEngine.UI.MaskableGraphic[0];
            }
            armature.slots        = m_slots.ToArray();
            armature.bones        = m_bones.ToArray();
            armature.zSpace       = zoffset;
            armature.sonArmatures = m_sonArmature.ToArray();
            if (armatureAnimList.ContainsKey(armature.name))
            {
                armature.anims = armatureAnimList[armature.name].ToArray();
            }
            armature.ResetSlotZOrder();

            string path = AssetDatabase.GetAssetPath(animTextAsset);

            path = path.Substring(0, path.LastIndexOf('/')) + "/" + _armature.name;


            //create pose data
            PoseData poseData = AssetDatabase.LoadAssetAtPath <PoseData>(path + "_Pose.asset");

            if (poseData == null)
            {
                poseData = ScriptableObject.CreateInstance <PoseData>();
                AssetDatabase.CreateAsset(poseData, path + "_Pose.asset");
            }
            poseData.slotDatas = new PoseData.SlotData[armature.slots.Length];
            for (int i = 0; i < armature.slots.Length; ++i)
            {
                poseData.slotDatas[i]              = new PoseData.SlotData();
                poseData.slotDatas[i].color        = armature.slots[i].color;
                poseData.slotDatas[i].displayIndex = armature.slots[i].displayIndex;
                poseData.slotDatas[i].zorder       = armature.slots[i].z;
                armature.slots[i].SendMessage("UpdateSlotByInheritSlot", SendMessageOptions.DontRequireReceiver);
            }
            poseData.boneDatas = new PoseData.TransformData[armature.bones.Length];
            for (int i = 0; i < armature.bones.Length; ++i)
            {
                poseData.boneDatas[i]          = new PoseData.TransformData();
                poseData.boneDatas[i].x        = armature.bones[i].localPosition.x;
                poseData.boneDatas[i].y        = armature.bones[i].localPosition.y;
                poseData.boneDatas[i].sx       = armature.bones[i].localScale.x;
                poseData.boneDatas[i].sy       = armature.bones[i].localScale.y;
                poseData.boneDatas[i].rotation = armature.bones[i].localEulerAngles.z;
            }
            if (isUGUI)
            {
                poseData.displayDatas = new PoseData.DisplayData[armature.uiAttachments.Length];
                for (int i = 0; i < armature.uiAttachments.Length; ++i)
                {
                    poseData.displayDatas[i] = new PoseData.DisplayData();
                    UnityEngine.UI.MaskableGraphic render = armature.uiAttachments[i];

                    UIFrame sf = render.GetComponent <UIFrame>();
                    if (sf)
                    {
                        poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                        poseData.displayDatas[i].color = sf.color;
                    }
                    else
                    {
                        UIMesh sm = render.GetComponent <UIMesh>();
                        if (sm)
                        {
                            poseData.displayDatas[i].type   = PoseData.AttachmentType.MESH;
                            poseData.displayDatas[i].color  = sm.color;
                            poseData.displayDatas[i].vertex = (Vector3[])sm.vertices.Clone();
                            if (sm.weights == null || sm.weights.Length == 0)
                            {
                                for (int k = 0; k < poseData.displayDatas[i].vertex.Length; ++k)
                                {
                                    poseData.displayDatas[i].vertex[k] /= 100f;
                                }
                            }
                        }
                        else
                        {
                            UnityEngine.UI.Image sr = render.GetComponent <UnityEngine.UI.Image>();
                            if (sr)
                            {
                                poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                                poseData.displayDatas[i].color = sr.color;
                            }
                            else
                            {
                                poseData.displayDatas[i].type = PoseData.AttachmentType.BOX;
                            }
                        }
                    }
                    poseData.displayDatas[i].transform          = new PoseData.TransformData();
                    poseData.displayDatas[i].transform.x        = render.transform.localPosition.x;
                    poseData.displayDatas[i].transform.y        = render.transform.localPosition.y;
                    poseData.displayDatas[i].transform.sx       = render.transform.localScale.x;
                    poseData.displayDatas[i].transform.sy       = render.transform.localScale.y;
                    poseData.displayDatas[i].transform.rotation = render.transform.localEulerAngles.z;
                    render.transform.localScale *= unit;
                }
            }
            else
            {
                poseData.displayDatas = new PoseData.DisplayData[armature.attachments.Length];
                for (int i = 0; i < armature.attachments.Length; ++i)
                {
                    poseData.displayDatas[i] = new PoseData.DisplayData();
                    Renderer render = armature.attachments[i];

                    SpriteFrame sf = render.GetComponent <SpriteFrame>();
                    if (sf)
                    {
                        poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                        poseData.displayDatas[i].color = sf.color;
                    }
                    else
                    {
                        SpriteMesh sm = render.GetComponent <SpriteMesh>();
                        if (sm)
                        {
                            poseData.displayDatas[i].type   = PoseData.AttachmentType.MESH;
                            poseData.displayDatas[i].color  = sm.color;
                            poseData.displayDatas[i].vertex = (Vector3[])sm.vertices.Clone();
                        }
                        else
                        {
                            SpriteRenderer sr = render.GetComponent <SpriteRenderer>();
                            if (sr)
                            {
                                poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                                poseData.displayDatas[i].color = sr.color;
                            }
                            else
                            {
                                poseData.displayDatas[i].type = PoseData.AttachmentType.BOX;
                            }
                        }
                    }
                    poseData.displayDatas[i].transform          = new PoseData.TransformData();
                    poseData.displayDatas[i].transform.x        = render.transform.localPosition.x;
                    poseData.displayDatas[i].transform.y        = render.transform.localPosition.y;
                    poseData.displayDatas[i].transform.sx       = render.transform.localScale.x;
                    poseData.displayDatas[i].transform.sy       = render.transform.localScale.y;
                    poseData.displayDatas[i].transform.rotation = render.transform.localEulerAngles.z;
                }
            }
            armature.poseData = poseData;
            AssetDatabase.Refresh();
            EditorUtility.SetDirty(poseData);
            AssetDatabase.SaveAssets();
            if (armature.textureFrames && armature.textureFrames.materials != null && armature.textureFrames.materials.Length > 0)
            {
                armature.preMultiplyAlpha = armature.textureFrames.materials[0].GetFloat("_BlendSrc") == (int)UnityEngine.Rendering.BlendMode.One;
            }
            else
            {
                armature.preMultiplyAlpha = true;
            }

            if (armature.isUGUI)
            {
                GameObject canvas = GameObject.Find("/Canvas");
                if (canvas)
                {
                    _armature.SetParent(canvas.transform);
                    _armature.localScale    = Vector3.one / unit;
                    _armature.localPosition = Vector3.zero;
                }
            }

            if (genPrefab)
            {
                string     prefabPath = path + ".prefab";
                GameObject prefab     = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);
                if (!prefab)
                {
                    PrefabUtility.CreatePrefab(prefabPath, _armature.gameObject, ReplacePrefabOptions.ConnectToPrefab);
                }
                else
                {
                    PrefabUtility.ReplacePrefab(_armature.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
                }
            }
        }
示例#8
0
        /// <summary>
        /// Sets to pose.
        /// </summary>
        public void SetToPose()
        {
            if (poseData)
            {
                for (int i = 0; i < poseData.boneDatas.Length && i < bones.Length; ++i)
                {
                    Transform bone = bones[i];
                    if (bone)
                    {
                        PoseData.TransformData transData = poseData.boneDatas[i];
                        bone.localPosition    = new Vector3(transData.x, transData.y, bone.localPosition.z);
                        bone.localScale       = new Vector3(transData.sx, transData.sy, bone.localScale.z);
                        bone.localEulerAngles = new Vector3(bone.localEulerAngles.x, bone.localEulerAngles.y, transData.rotation);
                    }
                }
                for (int i = 0; i < poseData.slotDatas.Length && i < slots.Length; ++i)
                {
                    Slot slot = slots[i];
                    if (slot)
                    {
                        slot.color        = poseData.slotDatas[i].color;
                        slot.displayIndex = poseData.slotDatas[i].displayIndex;
                        slot.z            = poseData.slotDatas[i].zorder;
                    }
                    m_SortedSlots = null;
                }
                if (isUGUI)
                {
                    for (int i = 0; i < poseData.displayDatas.Length && i < uiAttachments.Length; ++i)
                    {
                        MaskableGraphic mg = uiAttachments[i];
                        if (mg)
                        {
                            PoseData.DisplayData displayData = poseData.displayDatas[i];
                            switch (displayData.type)
                            {
                            case PoseData.AttachmentType.IMG:
                                UIFrame uf = mg.GetComponent <UIFrame>();
                                if (uf)
                                {
                                    uf.color = displayData.color;
                                }
                                else
                                {
                                    Image img = mg.GetComponent <Image>();
                                    if (img)
                                    {
                                        img.color = displayData.color;
                                    }
                                }
                                break;

                            case PoseData.AttachmentType.MESH:
                                UIMesh um = mg.GetComponent <UIMesh> ();
                                um.vertices = (Vector3[])displayData.vertex.Clone();
                                if (um.vertControlTrans != null && um.vertControlTrans.Length > 0)
                                {
                                    for (int j = 0; j < um.vertControlTrans.Length && j < um.vertices.Length; ++j)
                                    {
                                        Transform vctr = um.vertControlTrans [j];
                                        if (vctr)
                                        {
                                            vctr.localPosition = um.vertices [j];
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < poseData.displayDatas.Length && i < attachments.Length; ++i)
                    {
                        Renderer r = attachments[i];
                        if (r)
                        {
                            PoseData.DisplayData displayData = poseData.displayDatas[i];
                            switch (displayData.type)
                            {
                            case PoseData.AttachmentType.IMG:
                                SpriteFrame sf = r.GetComponent <SpriteFrame>();
                                if (sf)
                                {
                                    sf.color = displayData.color;
                                }
                                else
                                {
                                    SpriteRenderer sr = r.GetComponent <SpriteRenderer>();
                                    if (sr)
                                    {
                                        sr.color = displayData.color;
                                    }
                                }
                                break;

                            case PoseData.AttachmentType.MESH:
                                SpriteMesh sm = r.GetComponent <SpriteMesh>();
                                sm.vertices = (Vector3[])displayData.vertex.Clone();
                                if (sm.vertControlTrans != null)
                                {
                                    for (int j = 0; j < sm.vertControlTrans.Length && j < sm.vertices.Length; ++j)
                                    {
                                        Transform vctr = sm.vertControlTrans[j];
                                        if (vctr)
                                        {
                                            vctr.localPosition = sm.vertices[j];
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
            ResetSlotZOrder();
        }