tecd
New Member
Posts: 3
|
Post by tecd on Apr 24, 2021 12:30:09 GMT
When you play the Selection Demo Scene I would like to default to a specific selection (e.g. 1 or a stored last selection). I cant figure out how to do the default selection part.
|
|
|
Post by echo17 on Apr 24, 2021 13:01:14 GMT
EnhancedScroller is an MVC solution, meaning that the data drives the view. You would set the element data that you would like selected before calling ReloadData on the scroller. For Demo #3 specifically, you can change the Reload method in SelectionDemo.cs to this:
private void Reload() { // if the data existed previously, loop through // and remove the selection change handlers before // clearing out the data. if (_data != null) { for (var i = 0; i < _data.Count; i++) { _data[i].selectedChanged = null; } }
// set up a new inventory list _data = new SmallList<InventoryData>();
// add inventory items to the list _data.Add(new InventoryData() { itemName = "Sword", itemCost = 123, itemDamage = 50, itemDefense = 0, itemWeight = 10, spritePath = resourcePath + "/sword", itemDescription = "Broadsword with a double-edged blade" }); _data.Add(new InventoryData() { itemName = "Shield", itemCost = 80, itemDamage = 0, itemDefense = 60, itemWeight = 50, spritePath = resourcePath + "/shield", itemDescription = "Steel shield to deflect your enemy's blows" }); _data.Add(new InventoryData() { itemName = "Amulet", itemCost = 260, itemDamage = 0, itemDefense = 0, itemWeight = 1, spritePath = resourcePath + "/amulet", itemDescription = "Magic amulet restores your health points gradually over time" }); _data.Add(new InventoryData() { itemName = "Helmet", itemCost = 50, itemDamage = 0, itemDefense = 20, itemWeight = 20, spritePath = resourcePath + "/helmet", itemDescription = "Standard helm will decrease your vulnerability" }); _data.Add(new InventoryData() { itemName = "Boots", itemCost = 40, itemDamage = 0, itemDefense = 10, itemWeight = 5, spritePath = resourcePath + "/boots", itemDescription = "Boots of speed will double your movement points" }); _data.Add(new InventoryData() { itemName = "Bracers", itemCost = 30, itemDamage = 0, itemDefense = 20, itemWeight = 10, spritePath = resourcePath + "/bracers", itemDescription = "Bracers will upgrade your overall armor" }); _data.Add(new InventoryData() { itemName = "Crossbow", itemCost = 100, itemDamage = 40, itemDefense = 0, itemWeight = 30, spritePath = resourcePath + "/crossbow", itemDescription = "Crossbow can attack from long range" }); _data.Add(new InventoryData() { itemName = "Fire Ring", itemCost = 300, itemDamage = 100, itemDefense = 0, itemWeight = 1, spritePath = resourcePath + "/fireRing", itemDescription = "Fire ring gives you the magical ability to cast fireball spells" }); _data.Add(new InventoryData() { itemName = "Knapsack", itemCost = 22, itemDamage = 0, itemDefense = 0, itemWeight = 0, spritePath = resourcePath + "/knapsack", itemDescription = "Knapsack will increase your carrying capacity by twofold" });
_data[0].Selected = true;
// tell the scrollers to reload vScroller.ReloadData(); hScroller.ReloadData(); }
Note the only line that was added is
_data[0].Selected = true;
just before calling ReloadData. This sets the first element as selected. If you want a different index, then change the zero to something else for the array element.
|
|
tecd
New Member
Posts: 3
|
Post by tecd on Apr 24, 2021 14:23:00 GMT
Thanks, that part works (the scroller), however still struggling with the selection part (selected panel) - private void CellViewSelected(EnhancedScrollerCellView cellView). I can fake this, however I would just like to run the above function. I am not sure what to pass as cellview - e.g. CellViewSelected( ?);
|
|
|
Post by echo17 on Apr 25, 2021 13:33:24 GMT
It sounds like you are wanting to reverse the MVC paradigm, which I would not recommend. It is less coupled when you change the underlying data and then have the view update to reflect the changes. CellViewSelected responds to the event of the button press inside the cell view. CellViewSelected should only be called from the view itself, passing itself as the parameter.
You could create a method that mimics the CellViewSelected code, only it would be controller/data side instead of cell view side. Something like:
private void SelectIndex(int selectedDataIndex) { for (var i = 0; i < _data.Count; i++) { _data[i].Selected = (selectedDataIndex == i); }
selectedImage.gameObject.SetActive(true); selectedImage.sprite = Resources.Load<Sprite>(_data[selectedDataIndex].spritePath + "_v");
selectedImageText.text = _data[selectedDataIndex].itemName;
vScroller.ReloadData(); hScroller.ReloadData(); }
Then you call it with:
SelectIndex(0); // or whatever index you want to select
|
|
tecd
New Member
Posts: 3
|
Post by tecd on Apr 25, 2021 14:11:29 GMT
Yeah that was my way of doing it (which I labeled fake). It works well though, thx.
|
|