Ejemplo n.º 1
0
        public static SpringManager PerformSetup
        (
            GameObject rootObject,
            IEnumerable <GameObject> newSpringBones,
            AutoSetupParameters parameters
        )
        {
            SpringBoneSetup.DestroySpringManagersAndBones(rootObject);
            SpringColliderSetup.DestroySpringColliders(rootObject);

            var springBones = new List <SpringBone>();

            foreach (var boneParent in newSpringBones.Where(boneParent => boneParent.transform.childCount > 0))
            {
                springBones.Add(boneParent.AddComponent <SpringBone>());
            }

            var manager = rootObject.AddComponent <SpringManager>();

            SpringBoneSetup.FindAndAssignSpringBones(manager, true);

            if (parameters.createPivots)
            {
                foreach (var springBone in springBones)
                {
                    SpringBoneSetup.CreateSpringPivotNode(springBone);
                }
            }

            return(manager);
        }
Ejemplo n.º 2
0
            public void Perform()
            {
                setup.Build();
                AssetDatabase.Refresh();

                const string ResultFormat  = "セットアップ完了: {0}\nボーン数: {1} コライダー数: {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);
            }
Ejemplo n.º 3
0
            public void BuildObjects(GameObject colliderRoot)
            {
                SpringColliderSetup.DestroySpringColliders(colliderRoot);
                var allChildren = colliderRoot.BuildNameToComponentMap <Transform>(true);

                BuildDynamicsNulls(allChildren, dynamicsNullRecords);

                // New objects may have been created by SetupDynamicNulls, so retrieve all the children again
                allChildren = colliderRoot.BuildNameToComponentMap <Transform>(true);
                foreach (var record in colliderRecords)
                {
                    BuildColliderFromRecord(allChildren, record);
                }

                allChildren = colliderRoot.BuildNameToComponentMap <Transform>(true);
                BuildComponents(allChildren, componentRecords);
            }
        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;
                    }
                }
            }
        }
        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 => 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 = 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);
            }
        }
Ejemplo n.º 6
0
            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
                });
            }