Esempio n. 1
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);
                }
            }
        }
        public override void OnInspectorGUI()
        {
            Armature armature = target as Armature;

            if (armature == null)
            {
                return;
            }

            bool haveGroup = false;

                        #if UNITY_5_6_OR_NEWER
            haveGroup = armature.sortingGroup != null;
                        #endif

            if (!armature.isUGUI && !haveGroup)
            {
                selectedOption = EditorGUILayout.Popup("Sorting Layer", selectedOption, sortingLayerNames);
                if (sortingLayerNames[selectedOption] != armature.sortingLayerName)
                {
                    Undo.RecordObject(armature, "Sorting Layer");
                    armature.sortingLayerName = sortingLayerNames[selectedOption];
                    EditorUtility.SetDirty(armature);
                }

                int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", armature.sortingOrder);
                if (newSortingLayerOrder != armature.sortingOrder)
                {
                    Undo.RecordObject(armature, "Edit Sorting Order");
                    armature.sortingOrder = newSortingLayerOrder;
                    EditorUtility.SetDirty(armature);
                }

                if (GUILayout.Button("Update All Sorting", GUILayout.Height(20)))
                {
                    armature.sortingLayerName = armature.sortingLayerName;
                    armature.sortingOrder     = armature.sortingOrder;
                    EditorUtility.SetDirty(armature);

                    foreach (Renderer render in armature.GetComponentsInChildren <Renderer>(true))
                    {
                        render.sortingLayerName = armature.sortingLayerName;
                        render.sortingOrder     = armature.sortingOrder;
                        EditorUtility.SetDirty(render);

                        SpriteFrame sf = render.GetComponent <SpriteFrame>();
                        if (sf)
                        {
                            sf.sortingLayerName = armature.sortingLayerName;
                            sf.sortingOrder     = armature.sortingOrder;
                            UnityEditor.EditorUtility.SetDirty(sf);
                        }
                        else
                        {
                            SpriteMesh sm = render.GetComponent <SpriteMesh>();
                            if (sm)
                            {
                                sm.sortingLayerName = armature.sortingLayerName;
                                sm.sortingOrder     = armature.sortingOrder;
                                UnityEditor.EditorUtility.SetDirty(sm);
                            }
                        }
                    }

                    foreach (Armature sonArmature in armature.GetComponentsInChildren <Armature>(true))
                    {
                        sonArmature.sortingLayerName = sonArmature.sortingLayerName;
                        sonArmature.sortingOrder     = sonArmature.sortingOrder;
                        EditorUtility.SetDirty(sonArmature);
                    }

                    if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
                EditorGUILayout.Space();
            }

            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("color"), true);
            if (!Application.isPlaying)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_FlipX"), true);
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_FlipY"), true);
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_PreMultiplyAlpha"), true);

            if (!Application.isPlaying)
            {
                if (!armature.isUGUI)
                {
                                        #if UNITY_5_6_OR_NEWER
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("sortType"), true);
                                        #else
                    armature.sortType = Armature.SortType.ZSpace;
                                        #endif
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("zSpace"), true);

                    if (sortType != armature.sortType)
                    {
                        sortType = armature.sortType;
                        armature.sortingOrder = armature.sortingOrder;
                    }
                }
            }
            if (armature.anims != null && armature.anims.Length > 0)
            {
                int temp = (int)armature.animIndex;
                System.Collections.Generic.List <string> animsList = new System.Collections.Generic.List <string>(armature.anims);
                animsList.Insert(0, "<None>");
                armature.animIndex = EditorGUILayout.Popup("Current Animation", temp + 1, animsList.ToArray()) - 1;
                if (armature.animIndex != temp && !Application.isPlaying)
                {
                    UnityEditor.EditorUtility.SetDirty(armature);
                    if (!Application.isPlaying && !string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("anims"), true);
            if (armature.skins != null && armature.skins.Length > 1)
            {
                int temp = armature.skinIndex;
                armature.skinIndex = EditorGUILayout.Popup("Skins", armature.skinIndex, armature.skins);
                if (temp != armature.skinIndex && !Application.isPlaying)
                {
                    UnityEditor.EditorUtility.SetDirty(armature);
                    if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("slots"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("bones"), true);
            if (armature.isUGUI)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("uiAttachments"), true);
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("attachments"), true);
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("sonArmatures"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("parentArmature"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("textureFrames"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("poseData"), true);
            serializedObject.ApplyModifiedProperties();

            if (!Application.isPlaying && armature.flipX != flipX)
            {
                armature.flipX = armature.flipX;
                flipX          = armature.flipX;
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && armature.flipY != flipY)
            {
                armature.flipY = armature.flipY;
                flipY          = armature.flipY;
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && armature.zSpace != zspace)
            {
                zspace = armature.zSpace;
                armature.ResetSlotZOrder();
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }

            if (armature.preMultiplyAlpha != preMultiplyAlpha)
            {
                preMultiplyAlpha          = armature.preMultiplyAlpha;
                armature.preMultiplyAlpha = preMultiplyAlpha;
                if (!Application.isPlaying && !string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && !armature.isUGUI && armature.parentArmature == null)
            {
                ArmatureMesh am = armature.gameObject.GetComponent <ArmatureMesh>();
                if (!am)
                {
                    if (GUILayout.Button("Add Armature Mesh", GUILayout.Height(20)))
                    {
                        am = armature.gameObject.AddComponent <ArmatureMesh>();
                    }
                }
            }
            GUILayout.Space(5);
            if (GUILayout.Button("Set To Pose", GUILayout.Height(20)))
            {
                armature.SetToPose();
            }
        }