Exemplo n.º 1
0
    /// <summary>
    /// Attachs child colliders to a parent transform.  Searches all children for "childCollider" component
    /// and then sets (passed) delegate funcitons.  Returns count of child colliders set.
    /// Unity 4.0 has problems with namespaces + optional parameters so all parameters are necessary.
    /// </summary>
    private static int AttachChildColliders(Transform parent,
                                            OnTriggerEnterDelegate triggerEnterDelegate,
                                            OnTriggerStayDelegate triggerStayDelegate,
                                            OnCollisionEnterDelegate collisionEnterDelegate,
                                            OnCollisionStayDelegate collisionStayDelegate)
    {
        int childCount = 0;
        if(parent == null) Debug.LogError("Parent transform missing in AttachChildColliders");

        // look gof all children with colliders...
        Collider[] colliders = parent.gameObject.GetComponentsInChildren<Collider>();
        foreach(Collider collider in colliders)
        {
            // don't add to parent.
            if(collider != parent.GetComponent<Collider>())
            {
                GameObject go = collider.gameObject;

                // add a child collider component to them if they don't have one already
                ChildCollider childCollider = go.GetComponent<ChildCollider>() as ChildCollider;
                if(childCollider == null)
                {
                    childCollider = go.AddComponent(typeof(ChildCollider)) as ChildCollider;
                    //Debug.Assert(childCollider != null,"Failed to add child collider to " + go.name);
                }

                // setup lists.
                childCollider.Init();

                // attach delegate to child collider...
                // To Do:  Figure out a way to move this repeat code into subroutine...
                //Debug.Assert(childCollider.m_triggerEnterDelegates != null);
                if(triggerEnterDelegate != null)
                    childCollider.m_triggerEnterDelegates.Add(triggerEnterDelegate);

                //Debug.Assert(childCollider.m_triggerStayDelegates != null);
                if(triggerStayDelegate != null)
                    childCollider.m_triggerStayDelegates.Add(triggerStayDelegate);

                //Debug.Assert(childCollider.m_collisionEnterDelegates != null);
                if(collisionEnterDelegate != null)
                    childCollider.m_collisionEnterDelegates.Add(collisionEnterDelegate);

                //Debug.Assert(childCollider.m_collisionStayDelegates != null);
                if(collisionStayDelegate != null)
                    childCollider.m_collisionStayDelegates.Add(collisionStayDelegate);

                childCount += 1;
            }
        }

        return childCount;
    }
Exemplo n.º 2
0
 public static int AttachChildStayDelegate(Transform parent, OnCollisionStayDelegate collisionStayDelegate)
 {
     return AttachChildColliders(parent, null, null, null, collisionStayDelegate);
 }
Exemplo n.º 3
0
 public void RemoveFunctionFromCollisionStayDelegate(OnCollisionStayDelegate func)
 {
     onCollisionStayDelegate -= func;
 }
Exemplo n.º 4
0
 public void ClearCollisionStayDelegate()
 {
     onCollisionStayDelegate = null;
 }
Exemplo n.º 5
0
 //onCollisionStayDelegate functions
 public void AssignFunctionToCollisionStayDelegate(OnCollisionStayDelegate func)
 {
     onCollisionStayDelegate += func;
 }