Пример #1
0
        public static void RegisterCollisionTriggerCallbacksOnChildren(
            Transform transform,
            ChildColliderSensor.OnChildCollisionEnterDelegate on_collision_enter_child,
            ChildColliderSensor.OnChildTriggerEnterDelegate on_trigger_enter_child,
            ChildColliderSensor.OnChildCollisionExitDelegate on_collision_exit_child,
            ChildColliderSensor.OnChildTriggerExitDelegate on_trigger_exit_child,
            bool debug = false)
        {
            var children_with_colliders = transform.GetComponentsInChildren <Collider>(transform.gameObject);

            foreach (var child in children_with_colliders)
            {
                var child_sensor = child.gameObject.AddComponent <ChildColliderSensor>();
                child_sensor.OnCollisionEnterDelegate = on_collision_enter_child;
                child_sensor.OnTriggerEnterDelegate   = on_trigger_enter_child;
                child_sensor.OnCollisionExitDelegate  = on_collision_exit_child;
                child_sensor.OnTriggerExitDelegate    = on_trigger_exit_child;
                if (debug)
                {
                    Debug.Log(transform.name + " has " + child_sensor.name + " registered");
                }
            }
        }
Пример #2
0
        public static void RegisterCollisionTriggerCallbacksOnChildren(
            Component caller,
            Transform parent,
            ChildColliderSensor.OnChildCollisionEnterDelegate on_collision_enter_child,
            ChildColliderSensor.OnChildTriggerEnterDelegate on_trigger_enter_child   = null,
            ChildColliderSensor.OnChildCollisionExitDelegate on_collision_exit_child = null,
            ChildColliderSensor.OnChildTriggerExitDelegate on_trigger_exit_child     = null,
            ChildColliderSensor.OnChildCollisionStayDelegate on_collision_stay_child = null,
            ChildColliderSensor.OnChildTriggerStayDelegate on_trigger_stay_child     = null,
            bool debug = false)
        {
            var children_with_colliders = parent.GetComponentsInChildren <Collider>();

            foreach (var child in children_with_colliders)
            {
                var child_sensors = child.GetComponents <ChildColliderSensor>();
                ChildColliderSensor collider_sensor = null;
                foreach (var child_sensor in child_sensors)
                {
                    if (child_sensor.Caller != null && child_sensor.Caller == caller)
                    {
                        collider_sensor = child_sensor;
                        break;
                    }

                    if (child_sensor.Caller == null)
                    {
                        child_sensor.Caller = caller;
                        collider_sensor     = child_sensor;
                        break;
                    }
                }

                if (collider_sensor == null)
                {
                    collider_sensor        = child.gameObject.AddComponent <ChildColliderSensor>();
                    collider_sensor.Caller = caller;
                }

                if (on_collision_enter_child != null)
                {
                    collider_sensor.OnCollisionEnterDelegate = on_collision_enter_child;
                }

                if (on_trigger_enter_child != null)
                {
                    collider_sensor.OnTriggerEnterDelegate = on_trigger_enter_child;
                }

                if (on_collision_exit_child != null)
                {
                    collider_sensor.OnCollisionExitDelegate = on_collision_exit_child;
                }

                if (on_trigger_exit_child != null)
                {
                    collider_sensor.OnTriggerExitDelegate = on_trigger_exit_child;
                }

                if (on_trigger_stay_child != null)
                {
                    collider_sensor.OnTriggerStayDelegate = on_trigger_stay_child;
                }

                if (on_collision_stay_child != null)
                {
                    collider_sensor.OnCollisionStayDelegate = on_collision_stay_child;
                }

                if (debug)
                {
                    Debug.Log(
                        caller.name
                        + " has created "
                        + collider_sensor.name
                        + " on "
                        + child.name
                        + " under parent "
                        + parent.name);
                }
            }
        }