Exemplo n.º 1
0
    private void ExtractViewModels()
    {
        this.viewAndViewModelTypePrefabs = new List <Twin <GameObject, Type> >();

        foreach (var viewGameObject in viewPrefabs)
        {
            var components = viewGameObject.GetComponents(typeof(Component));

            foreach (var component in components)
            {
                var interfaces = component.GetType().GetInterfaces();
                foreach (var inter in interfaces)
                {
                    // If this gameobject contains a component of type IViewModel...
                    if (inter.Equals(typeof(IViewModel)))
                    {
                        // ... add it to available Views
                        var viewModelComponent = component as IViewModel;
                        this.viewAndViewModelTypePrefabs.Add(Twin.New(viewGameObject, viewModelComponent.GetType()));
                        break;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    public GameObject ShowViewModel <T>(Type destinationViewModelType, T parameters, Transform rootView = null)
    {
        // Use default root
        if (rootView == null)
        {
            rootView = this.RootView;
        }

        foreach (var viewAndViewModelTypeGameObject in this.viewAndViewModelTypePrefabs)
        {
            // If this is the correct destinationViewModelType...
            if (viewAndViewModelTypeGameObject.Type.Equals(destinationViewModelType))
            {
                // ... create View ...
                GameObject instantiatedView = Instantiate(viewAndViewModelTypeGameObject.GameObject, rootView);
                var        components       = instantiatedView.GetComponents(typeof(Component));

                // ... and initialize its ViewModel
                foreach (var component in components)
                {
                    var interfaces = component.GetType().GetInterfaces();
                    foreach (var inter in interfaces)
                    {
                        if (inter.Equals(typeof(IViewModel)))
                        {
                            var viewModel = component as IViewModel;
                            viewModel.SetParameters(parameters);
                            break;
                        }
                    }
                }

                this.navigationStack.Add(Twin.New(instantiatedView, viewAndViewModelTypeGameObject.Type));
                return(instantiatedView);
            }
        }

        return(null);
    }