public void Perform()
            {
                setup.Build();
                AssetDatabase.Refresh();

                const string ResultFormat  = "加载完成: {0}\nSpringBone数: {1} Collider数: {2}";
                var          boneCount     = springBoneRoot.GetComponentsInChildren <SpringBone>(true).Length;
                var          colliderCount = SpringColliderSetup.GetColliderTypes()
                                             .Sum(type => springBoneRoot.GetComponentsInChildren(type, true).Length);
                var resultMessage = string.Format(ResultFormat, path, boneCount, colliderCount);

                Debug.Log(resultMessage);
            }
예제 #2
0
        public static void AlignCapsulesToParents(IEnumerable <SpringCapsuleCollider> capsules)
        {
            var childComponentsToIgnore = new List <System.Type> {
                typeof(SpringBonePivot)
            };

            childComponentsToIgnore.AddRange(SpringColliderSetup.GetColliderTypes());

            var undoObjects = new List <Object>();

            undoObjects.AddRange(capsules.Select(item => (Object)item));
            undoObjects.AddRange(capsules.Select(item => (Object)item.transform));
            Undo.RecordObjects(undoObjects.ToArray(), "Align capsules to parents");
            foreach (var capsule in capsules)
            {
                capsule.transform.localPosition = Vector3.zero;
                var parent = capsule.transform.parent;
                if (parent.childCount >= 1)
                {
                    var childPositions = Enumerable.Range(0, parent.childCount)
                                         .Select(index => parent.GetChild(index))
                                         .Where(child => !GameObjectHasComponentOfAnyType(child.gameObject, childComponentsToIgnore))
                                         .Select(child => child.position);
                    var tailPosition = Vector3.zero;
                    foreach (var position in childPositions)
                    {
                        tailPosition += position;
                    }
                    tailPosition /= childPositions.Count();

                    var         length    = (tailPosition - parent.position).magnitude;
                    const float Threshold = 0.0001f;
                    if (length > Threshold)
                    {
                        capsule.transform.LookAt(tailPosition);
                        capsule.transform.Rotate(90f, 0f, 0f, Space.Self);
                        capsule.height = length;
                    }
                }
            }
        }
예제 #3
0
        public static void DeleteAllChildCollidersFromSelection()
        {
            if (Application.isPlaying)
            {
                Debug.LogError("再生モードを止めてください。");
                return;
            }

            if (Selection.gameObjects.Length == 0)
            {
                return;
            }

            var queryMessage = "本当に選択中のオブジェクトの全子供のコライダーを削除しますか?";

            if (!EditorUtility.DisplayDialog("選択コライダーを削除", queryMessage, "削除", "キャンセル"))
            {
                return;
            }

            var charaBones = Selection.gameObjects
                             .SelectMany(gameObject => GameObjectExtensions.GameObjectUtil.GetAllBones(gameObject.transform.root.gameObject))
                             .Distinct();

            var colliderTypes = SpringColliderSetup.GetColliderTypes();
            var deadColliders = new List <Component>();

            foreach (var gameObject in Selection.gameObjects)
            {
                foreach (var type in colliderTypes)
                {
                    deadColliders.AddRange(gameObject.GetComponentsInChildren(type, true));
                }
            }
            deadColliders = deadColliders.Distinct().ToList();

            var probablyDeadGameObjects = deadColliders.Select(collider => collider.gameObject)
                                          .Distinct()
                                          .Where(gameObject => !charaBones.Contains(gameObject.transform) &&
                                                 gameObject.GetComponents <Component>().Count() <= 1 &&
                                                 gameObject.transform.childCount == 0)
                                          .ToArray();

            var springBones = GameObjectExtensions.GameObjectUtil.FindComponentsOfType <SpringBone>();
            var undoObjects = new List <Object>(springBones.Select(item => (Object)item));

            undoObjects.AddRange(deadColliders.Select(item => (Object)item));
            undoObjects.AddRange(probablyDeadGameObjects.Select(item => (Object)item));
            Undo.RecordObjects(undoObjects.ToArray(), "Remove all selected child colliders");

            foreach (var springBone in springBones)
            {
                springBone.sphereColliders  = springBone.sphereColliders.Where(collider => !deadColliders.Contains(collider)).ToArray();
                springBone.capsuleColliders = springBone.capsuleColliders.Where(collider => !deadColliders.Contains(collider)).ToArray();
                springBone.panelColliders   = springBone.panelColliders.Where(collider => !deadColliders.Contains(collider)).ToArray();
            }

            foreach (var deadCollider in deadColliders)
            {
                SpringBoneSetup.DestroyUnityObject(deadCollider);
            }

            foreach (var gameObject in probablyDeadGameObjects)
            {
                SpringBoneSetup.DestroyUnityObject(gameObject);
            }
        }
            public static ParsedSpringBoneSetup ReadSpringBoneSetupFromText
            (
                GameObject springBoneRoot,
                GameObject colliderRoot,
                string recordText,
                IEnumerable <string> inputValidColliderNames
            )
            {
                List <TextRecordParsing.Record> rawSpringBoneRecords = null;
                List <TextRecordParsing.Record> rawPivotRecords      = null;

                try
                {
                    var sourceRecords = TextRecordParsing.ParseRecordsFromText(recordText);
                    TextRecordParsing.Record versionRecord = null;
                    DynamicsSetup.GetVersionFromSetupRecords(sourceRecords, out versionRecord);
                    rawSpringBoneRecords = TextRecordParsing.GetSectionRecords(sourceRecords, "SpringBones");
                    if (rawSpringBoneRecords == null || rawSpringBoneRecords.Count == 0)
                    {
                        rawSpringBoneRecords = TextRecordParsing.GetSectionRecords(sourceRecords, null)
                                               .Where(item => item != versionRecord)
                                               .ToList();
                    }
                    rawPivotRecords = TextRecordParsing.GetSectionRecords(sourceRecords, "Pivots");
                }
                catch (System.Exception exception)
                {
                    Debug.LogError("SpringBoneSetup: 元のテキストデータを読み込めませんでした!\n\n" + exception.ToString());
                    return(null);
                }

                var errors            = new List <DynamicsSetup.ParseMessage>();
                var pivotRecords      = SerializePivotRecords(rawPivotRecords, errors);
                var springBoneRecords = SerializeSpringBoneRecords(rawSpringBoneRecords, errors);

                var validObjectNames = springBoneRoot.GetComponentsInChildren <Transform>(true)
                                       .Select(item => item.name).Distinct().ToList();
                var validPivotRecords = new List <PivotSerializer>();

                VerifyPivotRecords(pivotRecords, validObjectNames, validPivotRecords, errors);

                var validPivotNames = new List <string>(validObjectNames);

                validPivotNames.AddRange(validPivotRecords.Select(record => record.name));

                var validColliderNames = new List <string>();
                var colliderTypes      = SpringColliderSetup.GetColliderTypes();

                validColliderNames.AddRange(colliderTypes
                                            .SelectMany(type => colliderRoot.GetComponentsInChildren(type, true))
                                            .Select(item => item.name));
                if (inputValidColliderNames != null)
                {
                    validColliderNames.AddRange(inputValidColliderNames);
                }

                var  validSpringBoneRecords = new List <SpringBoneSerializer>();
                bool hasMissingColliders;

                VerifySpringBoneRecords(
                    springBoneRecords,
                    validObjectNames,
                    validPivotNames,
                    validColliderNames,
                    validSpringBoneRecords,
                    out hasMissingColliders,
                    errors);

                if (hasMissingColliders)
                {
                    Debug.LogWarning("スプリングボーンセットアップ:一部のコライダーが見つかりません");
                }

                return(new ParsedSpringBoneSetup
                {
                    pivotRecords = validPivotRecords,
                    springBoneRecords = validSpringBoneRecords,
                    Errors = errors
                });
            }