I'm going to design a panel (it has a scroll view that has some clickable rows) which has the following parts:
1) Home - Main Menu
2) Gifts
3) Leaderboard Table
By click on each item, a new view will be loaded inside the panel and then a connection to the server will be established to fetch the data for updating the view.
In my design I have the following rules:
1) Any above sections can be removed in the future.
2) We can add more sections in the near future.
3) UI/UX may be changed in the future (for example panel view may be replaced by a console view that user can enter the commands to get a result like Unix shell).
So I want to use MVP or MVC for meeting the conditions listed above. I've used the MVP architecture like this :
I) each view of the panel is a view class that inherits from the MonoBehavior, for example, the view for the first part is something like this :
public class HomeView : MonoBehaviour , IHomeView
{
HomePresenter homePresenter;
public Sprite Icon_1;
public Sprite Icon_2;
public Sprite Icon_3;
public Transform ScrollContent;
public GameObject Row;
void Start()
{
homePresenter = new HomePresenter(this);
homePresenter.GetMenu();
}
public void ShowMenu(IEnumerable<Row> rows)
{
foreach(Row r in rows)
{
GameObject new_row = null;
new_row = Instantiate(Row, Vector3.zero, Row.transform.rotation);
SetRowData(new_row, r.Title, r.Icon);
new_row.transform.SetParent(ScrollContent);
}
}
public void SetRowData(GameObject row, string title, Sprite icon, int badgeCount = 0)
{
row.transform.Find("Icon").gameObject.GetComponent<UnityEngine.UI.Image>().sprite = icon;
row.transform.Find("Text").GetComponent<UnityEngine.UI.Text>().text = title;
}
public Sprite GetIcon(string icon)
{
if (icon == "1")
{
return Icon_1;
}
else if (icon == "2")
{
return Icon_2;
}
else if (icon == "3")
{
return Icon_3;
}
return null;
}
}
II) Presenter class for each view talks with an interface : (Row is a simple model class)
public class HomePresenter
{
IHomeView View;
public HomePresenter(IHomeView view)
{
View = view;
}
public void GetMenu()
{
/* connect to server */
/* imagine these rows will be returned */
List<Row> rows = new List<Row>(){
new Row()
{
Title = "Main Menu",
Icon = View.GetIcon("3"),
},
new Row()
{
Title = "Gifts",
Icon = View.GetIcon("2"),
},
new Row()
{
Title = "Leaderboard",
Icon = View.GetIcon("1"),
}
};
View.ShowMenu(rows);
}
}
So I'll continue this method for other views. Now my questions are :
1) Does this method correct and can I replace the panel view by a terminal view without any or a few changes? Does it really separate the view form logic?
2) view classes should inherit from MonoBehaviors or presenter classes should inherit from MonoBehaviors?
I'll be so glad to hear from your MVP experiences in Unity3D.
Aucun commentaire:
Enregistrer un commentaire