2016年6月7日 星期二

C#的尋找在List內之Structure資料

在Unity內、使用C#時,創造包含Structure的List,然後用條件尋找的方式。
先在List內找到符合條件的該筆資料Index,再用Index去調出要尋找的該筆資料其他部分:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MainProcess : MonoBehaviour {
  6.  
  7.     private List<tempStructure> tempStructureList = new List<tempStructure>();
  8.  
  9.     private struct tempStructure
  10.     {
  11.         public int tempNumber;
  12.         public string tempName;
  13.     }
  14.  
  15.     void Start ()
  16.     {
  17.         tempStructure xxx = new tempStructure();
  18.         tempStructure yyy = new tempStructure();
  19.  
  20.         xxx.tempNumber = 1;
  21.         xxx.tempName = "XXX";
  22.         yyy.tempNumber = 2;
  23.         yyy.tempName = "YYY";
  24.  
  25.         tempStructureList.Add( xxx );
  26.         tempStructureList.Add( yyy );
  27.  
  28.         tempIndex = tempStructureList.FindIndex( z => z.tempName == "XXX" );
  29.  
  30.         Debug.log( tempStructureList[tempIndex].tempNumber );
  31.     }
  32. }

沒有留言:

張貼留言