C# Collection Class II: (Practical use of Dictionary, Class, ArrayList)

In C# collection classes: (Array, Arraylist, List, Hashtable, Dictionary, Stack, Queue) , we know the basic usage of collections, but sometimes we need to record more data, we need several collections together.

I now need to make an alarm list function, each alarm code has its standard prompt language. And each alarm code can have several alarm levels.
The UI must be changed when an alarm is issued, an alarm is updated, and an alarm is cancelled. Therefore, each alarm code needs to be associated with a UI plane.

When an alarm is issued or an alarm is updated, the old plane is closed and a new plane is issued.
When the alarm is cancelled, the alarm level displayed on the plane is 0, but there are still planes with alarms to be placed on top, so it needs to be reordered.

At the beginning of the script, I wrote the Class of the alarm code.

public class AlertCode
{
    
    
    public string msg {
    
     get; set; }
    public int curRate {
    
     get; set; }
    public AlertPanelPlane curPlane {
    
     get; set; }
	//AlertPanelPlane 是每个plane自己的脚本,记录plane显示在的东西。
	
    public int positionInArray {
    
     get; set; }
}

Then create a Dictionary. I will mention Array List and positionInArray later.

Dictionary<string, AlertCode> alert_dict;

void Start()
    {
    
    
        activeAlert_array = new ArrayList();
        alert_dict = new Dictionary<string, AlertCode>();
        CreateDictionary();
    }
public void CreateDictionary()
    {
    
    
        //举几个例子
        AlertCode A03 = new AlertCode();
        A03.msg = "1#发动机失电";
        A03.curRate = 0;
        A03.positionInArray = 0;
        A03.curPlane = null;
        alert_dict.Add("A03", A03);

        AlertCode A04 = new AlertCode();
        A04.msg = "1#发动机过载";
        A04.curRate = 0;
        A04.positionInArray = 0;
        A04.curPlane = null;
        alert_dict.Add("A04", A04);
        
        AlertCode A05 = new AlertCode();
        A05.msg = "1#发动机缺相";
        A05.curRate = 0;
        A05.positionInArray = 0;
        A05.curPlane = null;
        alert_dict.Add("A05", A05);
    }

When there is a new message, the incoming data will include the alarm code (code) and level (rate).
The received Code can be used in the dictionary to find the data in its class.
If the Panel is Active, add it to activeAlert_array, and then add the corresponding activeAlert_array.Count to the positionInArray record.

switch(rate)
{
    
    
	case 1:		//1级警报
	case 2: 	//2级警报
		AlertPanelPlane newPlane = Instantiate(alertPlane, alertPlaneParent);
		newPlane.name = code.ToString();
		newPlane.gameObject.SetActive(true);
		newPlane.transform.SetAsFirstSibling();

		newPlane.OnDetailsUpdated(rate, alert_dict[code].msg);
                
        alert_dict[code].curRate = rate;
        if(alert_dict[code].curPlane != null)
        {
    
    
        	alert_dict[code].curPlane.gameObject.SetActive(false);
        }
        alert_dict[code].curPlane = newPlane;
        activeAlert_array.Add(newPlane);
        alert_dict[code].positionInArray = activeAlert_array.Count;
		break;
	case 0:		//警报取消
        AlertPanelPlane activePlane = alert_dict[code].curPlane;
        if(activePlane != null)
        {
    
    
            activePlane.OnDetailsUpdated(rate, "");
            alert_dict[code].curRate = 0;
        }
        alert_dict[code].curPlane = null;
        activeAlert_array.RemoveAt(alert_dict[code].positionInArray - 1);
        alert_dict[code].positionInArray = 0;
        ResetActiveAlertPriority();
		break;
	default:
		break;
	}
}

When canceling the alarm, remove the plane from activeAlert_array, and then put the plane in the remaining activeAlert_array on top again (SetAsFirstSibling).

void ResetActiveAlertPriority()
    {
    
    
        for(int i = 0; i != activeAlert_array.Count; i++)
        {
    
    
            AlertPanelPlane panel = (AlertPanelPlane)activeAlert_array[i];
            panel.transform.SetAsFirstSibling();
        }
    }

In this example, Dictionary is responsible for searching, Class is responsible for providing information corresponding to Code in the dictionary, and ArrayList is responsible for the order of sorting.

Guess you like

Origin blog.csdn.net/MikeW138/article/details/102956887