Exemple #1
0
    /// Recurses through Transform.parent to find the GameObject to which ContextView is attached
    /// Has a loop limit of 100 levels.
    /// By default, raises an Exception if no Context is found.
    virtual protected void bubbleToContext(MonoBehaviour view, bool toAdd, bool finalTry)
    {
        /*
         * if (toAdd && registeredWithContext)
         * {
         *  Debug.LogWarning("Already registered:" + view.name, view);
         *  return;
         * }*/

        const int LOOP_MAX    = 100;
        int       loopLimiter = 0;
        Transform trans       = view.gameObject.transform;

        while (trans.parent != null && loopLimiter < LOOP_MAX)
        {
            loopLimiter++;
            trans = trans.parent;
            if (trans.gameObject.GetComponent <SimpleBootstrap>() != null)
            {
                SimpleBootstrap contextView = trans.gameObject.GetComponent <SimpleBootstrap>() as SimpleBootstrap;
                if (contextView.context != null)
                {
                    BindToContext(contextView.SimpleContext);
                    IContext context = contextView.context;
                    if (toAdd)
                    {
                        context.AddView(view);
                        registeredWithContext = true;
                    }
                    else
                    {
                        context.RemoveView(view);
                    }
                    return;
                }
            }
        }
        if (requiresContext && finalTry)
        {
            //last ditch. If there's a Context anywhere, we'll use it!
            if (SimpleContext.Context != null)
            {
                BindToContext(SimpleContext.Context);
                SimpleContext.Context.AddView(view);
                registeredWithContext = true;
                return;
            }

            string msg = (loopLimiter == LOOP_MAX) ?
                         msg             = "A view couldn't find a context. Loop limit reached." :
                                     msg = "A view was added with no context. Views must be added into the hierarchy of their ContextView lest all hell break loose.";
            msg += "\nView: " + view.ToString();
            throw new MediationException(msg,
                                         MediationExceptionType.NO_CONTEXT);
        }
    }
Exemple #2
0
 protected virtual void Awake()
 {
     Instance      = this;
     simpleContext = new SimpleContext(this);
     context       = simpleContext;
 }