Esempio n. 1
0
        public static void RegisterCollisionTriggerCallbacksOnChildren(
            Transform transform,
            ChildSensor.OnChildCollisionEnterDelegate OnCollisionEnterChild,
            ChildSensor.OnChildTriggerEnterDelegate OnTriggerEnterChild,
            ChildSensor.OnChildCollisionExitDelegate OnCollisionExitChild,
            ChildSensor.OnChildTriggerExitDelegate OnTriggerExitChild,
            bool debug = false)
        {
            var childrenWithColliders = transform.GetComponentsInChildren <Collider>(transform.gameObject);

            foreach (var child in childrenWithColliders)
            {
                var child_sensor = child.gameObject.AddComponent <ChildSensor>();
                child_sensor.OnCollisionEnterDelegate = OnCollisionEnterChild;
                child_sensor.OnTriggerEnterDelegate   = OnTriggerEnterChild;
                child_sensor.OnCollisionExitDelegate  = OnCollisionExitChild;
                child_sensor.OnTriggerExitDelegate    = OnTriggerExitChild;
                //Debug.Log(transform.name + " has " + child_sensor.name + " registered");
            }
        }
Esempio n. 2
0
        public static void RegisterCollisionTriggerCallbacksOnChildren(
            Component caller,
            Transform parent,
            ChildSensor.OnChildCollisionEnterDelegate on_collision_enter_child,
            ChildSensor.OnChildTriggerEnterDelegate on_trigger_enter_child   = null,
            ChildSensor.OnChildCollisionExitDelegate on_collision_exit_child = null,
            ChildSensor.OnChildTriggerExitDelegate on_trigger_exit_child     = null,
            ChildSensor.OnChildCollisionStayDelegate on_collision_stay_child = null,
            ChildSensor.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 <ChildSensor>();
                ChildSensor sensor        = null;
                foreach (var child_sensor in child_sensors)
                {
                    if (child_sensor.Caller != null && child_sensor.Caller == caller)
                    {
                        sensor = child_sensor;
                        break;
                    }

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

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

                if (on_collision_enter_child != null)
                {
                    sensor.OnCollisionEnterDelegate = on_collision_enter_child;
                }
                if (on_trigger_enter_child != null)
                {
                    sensor.OnTriggerEnterDelegate = on_trigger_enter_child;
                }
                if (on_collision_exit_child != null)
                {
                    sensor.OnCollisionExitDelegate = on_collision_exit_child;
                }
                if (on_trigger_exit_child != null)
                {
                    sensor.OnTriggerExitDelegate = on_trigger_exit_child;
                }
                if (on_trigger_stay_child != null)
                {
                    sensor.OnTriggerStayDelegate = on_trigger_stay_child;
                }
                if (on_collision_stay_child != null)
                {
                    sensor.OnCollisionStayDelegate = on_collision_stay_child;
                }
                if (debug)
                {
                    Debug.Log(
                        caller.name
                        + " has created "
                        + sensor.name
                        + " on "
                        + child.name
                        + " under parent "
                        + parent.name);
                }
            }
        }