Exemplo n.º 1
0
        public static StandardAnimator AddStandardAnimatorComponent(GameObject gameObject, SkeletonDataAsset skeletonDataAsset)
        {
            var skeleton = SkeletonAnimation.AddToGameObject(gameObject, skeletonDataAsset);
            var animator = skeleton.gameObject.AddComponent <StandardAnimator>();

            return(animator);
        }
Exemplo n.º 2
0
    void GenSkelPrefab(SkelType skelType)
    {
        int count = Selection.instanceIDs.Length;

        if (count == 0)
        {
            Debug.LogError("请选择骨骼所在的文件夹1");
        }

        List <string> paths = new List <string>();

        for (int i = 0; i < count; i++)
        {
            string path = AssetDatabase.GetAssetPath(Selection.instanceIDs[i]);
            if (path.Contains(".") || !path.Contains("sk"))
            {
                continue;
            }
            paths.Add(path);
        }
        if (paths == null || paths.Count == 0)
        {
            Debug.LogError("请选择骨骼所在的文件夹2");
        }

        Dictionary <Slot, List <BoundingBoxAttachment> > boundingBoxTable = new Dictionary <Slot, List <BoundingBoxAttachment> >();

        for (int i = 0; i < paths.Count; ++i)
        {
            string path       = paths[i];
            string targetpath = "";
            switch (skelType)
            {
            case SkelType.Bullet:
                targetpath = "Assets/GameRes/Prefab/Battle/Bullet/";
                break;

            case SkelType.Effect:
                targetpath = "Assets/GameRes/Prefab/Effect/";
                break;

            case SkelType.Item:
                targetpath = "Assets/GameRes/Prefab/Skeleton/";
                break;

            case SkelType.Role:
                targetpath = "Assets/GameRes/Prefab/Skeleton/";
                break;

            case SkelType.Boss:
                targetpath = "Assets/GameRes/Prefab/Skeleton/";
                break;
            }
            if (string.IsNullOrEmpty(targetpath))
            {
                Util.LogError("目标路径 Unkown");
                return;
            }

            boundingBoxTable.Clear();
            int    lastidx    = path.LastIndexOf('/') + 1 + 3; //3跳掉sk_
            string name       = path.Substring(lastidx, path.Length - lastidx);
            string skelPath   = path.Substring(0, path.LastIndexOf('/') + 1);
            string prefabName = name + ".prefab";
            string prefabPath = targetpath + prefabName;
            Debug.Log("name:" + prefabPath);
            if (File.Exists(prefabPath))
            {
                File.Delete(prefabPath);
            }
            GameObject go = new GameObject();
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale    = Vector3.one;
            go.layer = LayerMask.NameToLayer("UI");

            //设置RectTransform
            RectTransform rt = go.AddComponent <RectTransform>();
            rt.anchoredPosition = Vector2.zero;
            rt.sizeDelta        = new Vector2(80, 80);
            rt.localEulerAngles = Vector3.zero;

            GameObject skelGo = new GameObject();
            skelGo.name = "Skeleton";
            skelGo.transform.localPosition = Vector3.zero;
            skelGo.transform.localScale    = new Vector3(100, 100);
            skelGo.transform.SetParent(go.transform);

            //获取骨骼数据
            string            skelDataPath = skelPath + "sk_" + name + "/" + name + "_SkeletonData.asset";
            Object            obj          = AssetDatabase.LoadAssetAtPath <Object>(skelDataPath);
            SkeletonDataAsset skelData     = AssetDatabase.LoadAssetAtPath <SkeletonDataAsset>(skelDataPath);
            if (null == skelData)
            {
                Debug.LogError(name + "获取骨骼数据失败");
                return;
            }
            //创建骨骼
            SkeletonAnimation skelAnim = SkeletonAnimation.AddToGameObject(skelGo, skelData);
            //展开spine个骨骼结点到unity
            SkeletonUtility util = skelGo.AddComponent <SkeletonUtility>();
            util.SpawnHierarchy(SkeletonUtilityBone.Mode.Follow, true, true, true);

            if (skelType == SkelType.Bullet)
            {
                BoxCollider2D collider = go.AddComponent <BoxCollider2D>();
                go.tag             = "Bullet";
                collider.isTrigger = true;
                Rigidbody2D rbody = go.AddComponent <Rigidbody2D>();
                rbody.gravityScale = 0f;
            }
            if (skelType == SkelType.Effect)
            {
                //  go.AddComponent<SkeletonEffect>();
            }
            if (skelType == SkelType.Boss)
            {
                BoxCollider2D collider = go.AddComponent <BoxCollider2D>();
                go.tag             = "MonsterDying";
                collider.isTrigger = true;
                collider.enabled   = false;

                //创建多边形碰撞
                Skeleton skeleton  = skelAnim.Skeleton;
                int      slotCount = skeleton.Slots.Count;
                Skin     skin      = skeleton.Skin;
                if (skin == null)
                {
                    skin = skeleton.Data.DefaultSkin;
                }
                //遍历所有的骨骼结点
                for (int m = 0; m < slotCount; ++m)
                {
                    Slot slot = skeleton.Slots.Items[m];
                    List <Attachment> slotAttachments = new List <Attachment>();
                    skin.FindAttachmentsForSlot(skeleton.FindSlotIndex(slot.Data.Name), slotAttachments);
                    for (int n = 0; n < slotAttachments.Count; ++n)
                    {
                        BoundingBoxAttachment bounding = slotAttachments[n] as BoundingBoxAttachment;
                        if (bounding != null)
                        {
                            string    childName = GetChildName(slot.Bone);
                            Transform transform = util.boneRoot.Find(childName);
                            if (null != transform)
                            {
                                SkeletonUtility.AddBoundingBoxGameObject(null, bounding, slot, transform);
                            }
                        }
                    }
                }
            }
            if (skelType == SkelType.Role)
            {
                BoxCollider2D collider = go.AddComponent <BoxCollider2D>();
                collider.isTrigger = true;
                go.tag             = "Hero";
                collider.size      = new Vector2(30, 50);
                Rigidbody2D body = go.AddComponent <Rigidbody2D>();
                body.gravityScale   = 0.0f;
                body.velocity       = Vector2.zero;
                body.freezeRotation = true;
            }

            //创建预置
            Object prefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
            PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);

            AssetImporter ai = AssetImporter.GetAtPath(prefabPath);
            ai.assetBundleName    = name;
            ai.assetBundleVariant = "assetbundle";

            GameObject.DestroyImmediate(go);
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// 通过skeletonDataAsset创建角色
        /// </summary>
        /// <returns><c>true</c>, if spine role was created, <c>false</c> otherwise.</returns>
        /// <param name="skeletonDataAsset">Skeleton data asset.</param>
        /// <param name="animationName">Animation name.</param>
        /// <param name="skinName">Skin name.</param>
        /// <param name="pos">Position.</param>
        private System.Collections.IEnumerator Create(SpineAvatarType avatarType, EquipmentObject eo, SkeletonDataAsset skeletonDataAsset, string roleGameObjectName, string animationName, string skinName, bool loop, RoleEquipmentData data, Action <Avatar> createSucCallBack, Action <string> failCallBack, Action <bool> createQueueCallBack, bool isTP)
        {
            if (skeletonDataAsset == null)
            {
                if (failCallBack != null)
                {
                    failCallBack.Invoke("<-创建角色失败->");
                }
                yield break;
            }
            GameObject roleSpine = new GameObject {
                name = roleGameObjectName
            };

            roleObject        = roleSpine;
            skeletonAnimation = SkeletonAnimation.AddToGameObject(roleSpine, skeletonDataAsset);
            skeletonAnimation.Initialize(false);
            skeletonAnimation.Skeleton.SetSkin(skinName);
            skeletonAnimation.state.SetAnimation(SpineConstValue.TrackIndex, animationName, loop);
            CreateSlotAttachmentDic(skeletonAnimation.Skeleton.Data.DefaultSkin.Attachments);
            if (null == data)
            {
                equipmentData = new RoleEquipmentData();
                equipmentData.RoleEquipmentItems = new List <RoleEquipmentItem> ();
            }
            else
            {
                equipmentData = data;
            }
            CombineTextureData combineTextureData = new CombineTextureData();

            combineTextureData.resultData = new CombineFinishedData();
            RoleAnaimationData rad = new RoleAnaimationData {
                roleJsonStr = eo.equipmentJsonAsset.text
            };
            Avatar avatar = new SpineAvatar(equipmentData, skeletonAnimation, slotAttachmentDic, rad, combineTextureData, avatarType, roleObject, eo.equipmentJsonAsset.text);

            combineTextureData.resultData.self = avatar;
            if (createSucCallBack != null)
            {
                if (null != roleSpine)
                {
                    roleSpine.SetActive(false);
                    createSucCallBack.Invoke(avatar);
                }
            }
            //初始化角色的所有服装信息
            if (equipmentData.RoleEquipmentItems.Count > 0)
            {
                if (null != combineTextureData.initRoleCoroutine)
                {
                    GameManager.Instance.StopCoroutine(combineTextureData.initRoleCoroutine);
                }
                combineTextureData.initRoleCoroutine = GameManager.Instance.StartCoroutine(InitRoleEquipment(rad, skeletonAnimation, equipmentData, combineTextureData, slotAttachmentDic, avatar.GetDestoryTextureList, roleSpine.SetActive, null, isTP));
                yield return(combineTextureData.initRoleCoroutine);
            }
            else
            {
                roleSpine.SetActive(true);
            }
            if (null != createQueueCallBack)
            {
                createQueueCallBack.Invoke(true);
            }
        }