예제 #1
0
    void Pause()
    {
        Predicate <Rigidbody2D> rigidbodyPredicate =
            obj => !obj.IsSleeping()
            &&
            Array.FindIndex(ignoreGameObjects, gameObject => gameObject == obj.gameObject) < 0;

        pausingRigidbodies  = Array.FindAll(transform.GetComponentsInChildren <Rigidbody2D>(), rigidbodyPredicate);
        rigidbodyVelocities = new Rigidbody2DVelocity[pausingRigidbodies.Length];
        var ArrayLength = pausingRigidbodies.Length;

        for (int i = 0; i < ArrayLength; i++)
        {
            rigidbodyVelocities[i] = new Rigidbody2DVelocity(pausingRigidbodies[i]);
            pausingRigidbodies[i].Sleep();
        }

        Predicate <MonoBehaviour> monoBehaviourPredicate =
            obj => obj.enabled &&
            obj != this &&
            Array.FindIndex(ignoreGameObjects, gameObject => gameObject == obj.gameObject) < 0;

        pausingMonobehaviours = Array.FindAll(transform.GetComponentsInChildren <MonoBehaviour>(), monoBehaviourPredicate);
        foreach (var monoBehaviour in pausingMonobehaviours)
        {
            monoBehaviour.enabled = false;
        }
    }
    /// <summary>
    /// ポーズ状態にする
    /// 対象は自身の子オブジェクト.
    /// MonoBehaviorとRigidbodyを停止させる.
    /// </summary>
    void Pause()
    {
        /// Rigidbodyの停止
        // 子要素から、スリープ中でなく、IgnoreGameObjectsに含まれていないRigidbodyを抽出
        Predicate <Rigidbody2D> rigidbodyPredicate =
            rigid => !rigid.IsSleeping() &&
            Array.FindIndex(this.IgnoreGameObjects, gameObject => gameObject == rigid.gameObject) == -1;

//ポーズするRigidbodyを取得
        this.pausingRigidbodies  = Array.FindAll(transform.GetComponentsInChildren <Rigidbody2D>(), rigidbodyPredicate);
        this.rigidbodyVelocities = new Rigidbody2DVelocity[pausingRigidbodies.Length];

        //対象のrigidbodyの現在の状態を保存してから止める
        for (int i = 0; i < pausingRigidbodies.Length; i++)
        {
            // 速度、角速度も保存しておく
            rigidbodyVelocities[i] = new Rigidbody2DVelocity(pausingRigidbodies[i]);
            pausingRigidbodies[i].Sleep();
        }


        /// MonoBehaviourの停止
        // 子要素から、activeかつこのインスタンスでないもの、IgnoreGameObjectsに含まれていないMonoBehaviourを抽出
        Predicate <MonoBehaviour> monoBehaviourPredicate =
            obj => obj.enabled &&
            obj != this &&
            Array.FindIndex(this.IgnoreGameObjects, gameObject => gameObject == obj.gameObject) < 0;

        this.pausingMonoBehaviours = Array.FindAll(this.transform.GetComponentsInChildren <MonoBehaviour>(), monoBehaviourPredicate);
        //抽出した対象monoBehaviourを全て停止させる
        foreach (var monoBehaviour in pausingMonoBehaviours)
        {
            monoBehaviour.enabled = false;
        }
        ///Animatorの停止
        Predicate <Animator> animatorPredicate =
            animator => animator.enabled &&
            Array.FindIndex(this.IgnoreGameObjects, gameObject => gameObject == animator.gameObject) == -1;

        this.pausingAnimators = Array.FindAll(this.transform.GetComponentsInChildren <Animator>(), animatorPredicate);
        //抽出したAnimatorを停止させる
        foreach (var animator in this.pausingAnimators)
        {
            animator.enabled = false;
        }
    }