예제 #1
0
    /// Substitute a class that extends from Monobehaviour and add it to the GameObject passed in.
    /// Remember, all non-virtual members will actually be executed. Only virtual/abstract members
    /// can be recorded or have return values specified.
    public static T For <T>(GameObject gameObject)
        where T : MonoBehaviour
    {
        if (!IsUsingComponentContext())
        {
            Debug.LogError("Cannot Substitute Component, BeginComponentContext must be called first.");
        }

        System.Type                type = typeof(T);
        ISubstituteFactory         substituteFactory = SubstitutionContext.Current.SubstituteFactory;
        ComponentSubstituteFactory componentFactory  = substituteFactory as ComponentSubstituteFactory;
        object result = componentFactory.CreateComponent(type, gameObject);

        return((T)result);
    }
예제 #2
0
    private static void CreateContext()
    {
        if (componentSubstitutionContext != null)
        {
            return;
        }

        var callRouterFactory   = new CallRouterFactory();
        var dynamicProxyFactory = new ComponentDynamicProxyFactory();
        var delegateFactory     = new DelegateProxyFactory();
        var proxyFactory        = new ComponentProxyFactory(delegateFactory, dynamicProxyFactory);
        var callRouteResolver   = new CallRouterResolver();
        var substituteFactory   = new ComponentSubstituteFactory(callRouterFactory, proxyFactory, callRouteResolver);

        componentSubstitutionContext = new SubstitutionContext(substituteFactory);
        substituteFactory._context   = componentSubstitutionContext;
    }