public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(mode);
            if (EditorGUI.EndChangeCheck())
            {
                containsOverrides = mode.enumValueIndex == 1;
                containsFollows   = mode.enumValueIndex == 0;
            }

            using (new EditorGUI.DisabledGroupScope(multiObject)) {
                string str = boneName.stringValue;
                if (str == "")
                {
                    str = "<None>";
                }
                if (multiObject)
                {
                    str = "<Multiple>";
                }

                using (new GUILayout.HorizontalScope()) {
                    EditorGUILayout.PrefixLabel("Bone");
                    if (GUILayout.Button(str, EditorStyles.popup))
                    {
                        BoneSelectorContextMenu(str, ((SkeletonUtilityBone)target).hierarchy.skeletonRenderer.skeleton.Bones, "<None>", TargetBoneSelected);
                    }
                }
            }

            EditorGUILayout.PropertyField(zPosition);
            EditorGUILayout.PropertyField(position);
            EditorGUILayout.PropertyField(rotation);
            EditorGUILayout.PropertyField(scale);

            using (new EditorGUI.DisabledGroupScope(containsFollows)) {
                EditorGUILayout.PropertyField(overrideAlpha);
                EditorGUILayout.PropertyField(parentReference);
            }

            EditorGUILayout.Space();

            using (new GUILayout.HorizontalScope()) {
                EditorGUILayout.Space();
                using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || utilityBone.bone.Children.Count == 0)) {
                    if (GUILayout.Button(SpineInspectorUtility.TempContent("Add Child Bone", Icons.bone), GUILayout.MinWidth(120), GUILayout.Height(24)))
                    {
                        BoneSelectorContextMenu("", utilityBone.bone.Children, "<Recursively>", SpawnChildBoneSelected);
                    }
                }
                using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || utilityBone.bone == null || containsOverrides)) {
                    if (GUILayout.Button(SpineInspectorUtility.TempContent("Add Override", Icons.poseBones), GUILayout.MinWidth(120), GUILayout.Height(24)))
                    {
                        SpawnOverride();
                    }
                }
                EditorGUILayout.Space();
            }
            EditorGUILayout.Space();
            using (new GUILayout.HorizontalScope()) {
                EditorGUILayout.Space();
                using (new EditorGUI.DisabledGroupScope(multiObject || !utilityBone.valid || !canCreateHingeChain)) {
                    if (GUILayout.Button(SpineInspectorUtility.TempContent("Create 3D Hinge Chain", Icons.hingeChain), GUILayout.MinWidth(120), GUILayout.Height(24)))
                    {
                        CreateHingeChain();
                    }
                    if (GUILayout.Button(SpineInspectorUtility.TempContent("Create 2D Hinge Chain", Icons.hingeChain), GUILayout.MinWidth(120), GUILayout.Height(24)))
                    {
                        CreateHingeChain2D();
                    }
                }
                EditorGUILayout.Space();
            }

            using (new EditorGUI.DisabledGroupScope(multiObject || boundingBoxTable.Count == 0)) {
                EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Bounding Boxes", Icons.boundingBox), EditorStyles.boldLabel);

                foreach (var entry in boundingBoxTable)
                {
                    Slot slot          = entry.Key;
                    var  boundingBoxes = entry.Value;

                    EditorGUI.indentLevel++;
                    EditorGUILayout.LabelField(slot.Data.Name);
                    EditorGUI.indentLevel++;
                    {
                        foreach (var box in boundingBoxes)
                        {
                            using (new GUILayout.HorizontalScope()) {
                                GUILayout.Space(30);
                                string buttonLabel = box.IsWeighted() ? box.Name + " (!)" : box.Name;
                                if (GUILayout.Button(buttonLabel, GUILayout.Width(200)))
                                {
                                    utilityBone.bone.Skeleton.UpdateWorldTransform();
                                    var bbTransform = utilityBone.transform.Find("[BoundingBox]" + box.Name);                                     // Use FindChild in older versions of Unity.
                                    if (bbTransform != null)
                                    {
                                        var originalCollider = bbTransform.GetComponent <PolygonCollider2D>();
                                        if (originalCollider != null)
                                        {
                                            SkeletonUtility.SetColliderPointsLocal(originalCollider, slot, box);
                                        }
                                        else
                                        {
                                            SkeletonUtility.AddBoundingBoxAsComponent(box, slot, bbTransform.gameObject);
                                        }
                                    }
                                    else
                                    {
                                        var newPolygonCollider = SkeletonUtility.AddBoundingBoxGameObject(null, box, slot, utilityBone.transform);
                                        bbTransform = newPolygonCollider.transform;
                                    }
                                    EditorGUIUtility.PingObject(bbTransform);
                                }
                            }
                        }
                    }
                    EditorGUI.indentLevel--;
                    EditorGUI.indentLevel--;
                }
            }

            BoneFollowerInspector.RecommendRigidbodyButton(utilityBone);

            serializedObject.ApplyModifiedProperties();
        }
Example #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);
        }
    }