void Start() { data.Reset(Enumerable.Range(0, 5).Select(_ => Random.Range(0, 20))); insertButton.ClickStream().Listen(() => data.Insert(Random.Range(0, data.current.Count), Random.Range(0, 20))); removeButton.ClickStream().Listen(() => { if (data.current.Count > 0) { data.RemoveAt(Random.Range(0, data.current.Count)); } }); TableLayoutSettings settings = new TableLayoutSettings { direction = LayoutDirection.Horizontal, margin = 30, topShift = 30, bottomShift = 30, }; Action <int, SimpleView> factory = (i, view) => { view.name.text = i.ToString(); }; addConnection = Rui.PresentInScrollWithReusableViews(initial, data, prefab, settings, factory, delegates: Animations.Default <SimpleView>()); var mappedData = data.Map(val => (val * 3 + 5) % 10); addConnection = Rui.PresentInScrollWithReusableViews(mapped, mappedData, prefab, settings, factory, delegates: Animations.Default <SimpleView>()); var filteredData = mappedData.Filter(val => val % 2 == 0); addConnection = Rui.PresentInScrollWithReusableViews(filtered, filteredData, prefab, settings, factory, delegates: Animations.Default <SimpleView>()); }
void Start() { var data = TestData.instance.data; Action <Cell <int>, SimpleView> factory = (i, view) => { view.addConnection = i.Bind(val => view.name.text = val.ToString()); }; // Linear layout addConnection = Rui.PresentInScrollWithReusableViews(vertical, data, prefab, settingsVertical, factory, delegates: Animations.Default <SimpleView>()); addConnection = Rui.PresentInScrollWithReusableViews(horizontal, data, prefab, settingsHorizontal, factory, delegates: Animations.Default <SimpleView>()); // Grid layout addConnection = Rui.PresentInScrollWithReusableViews(gridHorizontal, data, prefab, settingsGridHorizontal, factory, layout: Rui.GridTableLayout(data.CountCell(), settingsGridHorizontal, 3), delegates: Animations.Default <SimpleView>() ); addConnection = Rui.PresentInScrollWithReusableViews(gridVertical, data, prefab, settingsGridVertical, factory, layout: Rui.GridTableLayout(data.CountCell(), settingsGridVertical, 6), delegates: Animations.Default <SimpleView>() ); // Variable view size layout Func <Cell <int>, float> sizeFactory = item => 100 + item.value * 10; Action <Cell <int>, SimpleView> variableSizeViewFactory = (i, view) => { var size = sizeFactory(i); view.rectTransform.sizeDelta = new Vector2(size, size); factory(i, view); }; addConnection = Rui.PresentInScrollWithReusableViews(variableHorizontal, data, prefab, settingsHorizontal, variableSizeViewFactory, layout: Rui.VariableViewSizeLayout(data, sizeFactory, settingsHorizontal, connectionSink), delegates: Animations.Default <SimpleView>() ); addConnection = Rui.PresentInScrollWithReusableViews(variableVertical, data, prefab, settingsGridVertical, variableSizeViewFactory, layout: Rui.VariableViewSizeLayout(data, sizeFactory, settingsVertical, connectionSink), delegates: Animations.Default <SimpleView>() ); }
void Start() { gameData = new GameData(); uiState = new UiState(); // Unit table addConnection = Rui.PresentInScrollWithReusableViews(unitTable, gameData.unitsAvailable, unitViewPrefab, new TableLayoutSettings { direction = LayoutDirection.Horizontal, margin = 30, topShift = 30, bottomShift = 30 }, (unit, view) => { // Show dynamic data view.addConnection = view.attack.SetTextContent(unit.attack); view.addConnection = view.defence.SetTextContent(unit.defence); view.addConnection = view.hp.SetTextContent(unit.hp); // Show checkbox when current unit is selected view.addConnection = view.selectedCheckbox.SetVisibility(uiState.selectedUnit.Is(unit)); // Command to upgrade unit view.addConnection = view.upgradeButton.ClickStream().Listen(unit.Upgrade); // Command to change ui state view.addConnection = view.viewClickButton.ClickStream().Listen(() => uiState.SetUnitSelection(unit)); }); // Equipment table addConnection = Rui.PresentInScrollWithReusableViews(equipmentTable, gameData.equipmentAvailable, eqViewPrefab, new TableLayoutSettings { direction = LayoutDirection.Horizontal, margin = 30, topShift = 30, bottomShift = 30 }, (equipment, view) => { // Show dynamic data view.addConnection = view.buff.SetTextContent(equipment.buff.Select(value => string.Format("{0}:+{1}", equipment.type, value))); // Show checkbox when current equipment is selected view.addConnection = view.selectedCheckbox.SetVisibility(uiState.selectedEquipment.Is(equipment)); // Command to upgrade equipment view.addConnection = view.upgradeButton.ClickStream().Listen(equipment.Upgrade); // Command to change ui equipment selected state view.addConnection = view.viewClickButton.ClickStream().Listen(() => uiState.SetEquipmentSelection(equipment)); }); // Selected equipment view uiState.selectedEquipment.Bind(equipment => { var view = selectedEquipmentView; // Remove connections from previous selected item. view.DisconnectAll(); if (equipment == null) { view.buff.text = "X"; return; } view.addConnection = view.buff.SetTextContent(equipment.buff.Select(value => string.Format("{0}:+{1}", equipment.type, value))); view.addConnection = view.upgradeButton.ClickStream().Listen(equipment.Upgrade); }); addConnection = selectedEquipmentView.upgradeButton.SetVisibility(uiState.selectedEquipment.IsNot(null)); // Final selected unit + equpment buff foreach (var buffTypeIt in Utils.GetEnumValues <UnitBuffType>()) { // Copy var to prevent c# capturing iterator var buffType = buffTypeIt; // Compose final stat value from dynamic data // This is an example of how use linq api to manage complex data dependancies. var finalStat = from su in uiState.selectedUnit from se in uiState.selectedEquipment from stat in FinalStat(su, se, buffType) select stat; // Alternative (and slightly more ellegant and performant) to achieve same result. finalStat = uiState.selectedUnit .Merge(uiState.selectedEquipment, (su, se) => FinalStat(su, se, buffType)) .Join(); addConnection = TextForBuffType(finalUnitView, buffType).SetTextContent(finalStat); } addConnection = finalUnitView.upgradeButton.SetVisibility(uiState.selectedUnit.IsNot(null)); // Command to upgrade unit from final unit view addConnection = finalUnitView.upgradeButton.ClickStream().Listen(() => uiState.selectedUnit.value.Upgrade()); }