Saving links for list <T> without knowing T?

Clarification: The goal is to have a UIPList 1. Look to view any ObservableList where T is iUIP or one of its descendants, 2. Able to draw the UIPrefab button connected to this iUIP into a list and 3. The ability to change the list, which he browses at any time (and see only one list).

I can draw the gui buttons and get the first hours to work (with the hacker code below), but there is no way to save the link in a UIPList, so I can unsubscribe from this list when I want to see something else. I can’t do this in a “UIPList” (which allows me to keep the link) without losing the third ability mentioned above.

Now I'm trying to have a menu (who knows what UIPLists and ObservableLists are doing), take care to unsubscribe when the content has changed, but ideally I can just pass the ObservableList to a UIPList and it will know how to watch it, draw gui and unsubscribe when he receives a new list to watch himself.

Original post: I have UIPListone that should watch ObservableList<T> where T:iUIP. ( iUIPmeans a class that has a button that can be drawn in UIPList).

The problem is that I cannot save the local link to the list, as it could be ObservableList<iUIP>OR it could be ObservableList<Character>OR ObservableList<Organization>or so on (other classes inheriting iUIP).

I can not use ObservableList<iUIP>at UIPListas a reference, since I may want to see a list of which can not be considered ObservableList<iUIP>, in spite of the inheritance of characters from iUIP, so there is no possibility to save a local link to the list of UIPListmust see.

I use the method SetWatchList<T>(ObservableList<T> list)on UIPListto take the list for viewing, but since there is no link to the original list, when I want to change what is browsing UIPList, I do not have the link I need to unsubscribe from the previous list.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//using System.Collections.ObjectModel;

public class UIPList : MonoBehaviour
{
public GUIManager GUIManager;
public UILabel Title;
public UITable Contents;

//  ObservableList<T>.ListChangedEventHandler subscribedChangeEvent;

public void WatchList<T>(ObservableList<T> list) where T: iUIP
{
    this.subscribedChangeEvent = list.ListChanged;
    list.ListChanged += DrawUIPList;//TODO make always clear prev?
    DrawUIPList<T>(list);
}

//dont have a reference to the old list to know what to stop watching!
//  public void StopWatchList<T>(ObservableList<T> list) where T: iUIP
//  {
//      list.ListChanged -= DrawUIPList;
//  }

// Use this for initialization
protected void Start()
{
    this.GUIManager = GUIManager.Find;
}
// Update is called once per frame
void Update()
{
}


void DrawUIPList<T>(object source, ObservableList<T>.ListChangedEventArgs e) where T : iUIP
{
    DrawUIPList<T>(e.list);
}
void DrawUIPList<T>(ObservableList<T> list) where T:iUIP
{
    //todo AddComponentMenu/RemoveItem/adjut ReadOnlyCollectionBase changed UIPs
    ClearContents();
    this.Contents.Reposition();
    foreach (T uip in list)//TODO make this automatic, not need each one
    {
        //          Debug.Log("Adding UIP:" + child.name);
        if (uip.UIPButton is UIPCorporationButton)
            UIPCorporationButton.Create(this.Contents.gameObject, (uip as Corporation));

        if (uip.UIPButton is UIPCompanyButton)
            UIPCompanyButton.Create(this.Contents.gameObject, (uip as Company));

        if (uip.UIPButton is UIPAssetButton)
            UIPAssetButton.Create(this.Contents.gameObject, (uip as Asset));

        if (uip.UIPButton is UIPIndButton)
            UIPIndButton.Create(this.Contents.gameObject, (uip as Industry));

        if (uip.UIPButton is UIPSecButton)
            UIPSecButton.Create(this.Contents.gameObject, (uip as Sector));

        if (uip.UIPButton is UIPOperativeButton)
            UIPOperativeButton.Create(this.Contents.gameObject, (uip as Operative));

        if (uip.UIPButton is UIPAgencyButton)
            UIPAgencyButton.Create(this.Contents.gameObject, (uip as Agency));

        if (uip.UIPButton is UIPBrokerButton)
            UIPBrokerButton.Create(this.Contents.gameObject, (uip as Broker));

        if (uip.UIPButton is UIPCellButton)
            UIPCellButton.Create(this.Contents.gameObject, (uip as Cell));

        if (uip.UIPButton is UIPMissionButton)
            UIPMissionButton.Create(this.Contents.gameObject, (uip as Mission));

        if (uip.UIPButton is UIPObjectiveButton)
            UIPObjectiveButton.Create(this.Contents.gameObject, (uip as Objective));

        if (uip.UIPButton is UIPChallengeButton)
            UIPChallengeButton.Create(this.Contents.gameObject, (uip as Challenge));


    }
    //this.Contents.Reposition();
    this.Contents.repositionNow = true;
}

public void ClearContents()
{
//      this.Contents.children.children.Clear();
    int count = this.Contents.transform.childCount;     
    for (int i = count - 1; i >= 0; i--)
    {           
        GameObject.Destroy(this.Contents.transform.GetChild(i).gameObject);         
    }
}
}
+3
source share
1 answer

Make the whole general class

public class UIPList<T> : MonoBehaviour where T : iUIP

Then you can save the link to the list. It better encapsulates what the list actually represents.

Remember to also remove general restrictions from your methods.

public void WatchList(ObservableList<T> list)
+2

All Articles