正则表达式
1 public class RGX : MonoBehaviour
2 {
3 //Regular Expression Search Pattern
4 string search = "[dw]ay";
5
6 //Larger string to search
7 string txt = "hello, today is a good day to do things my way";
8
9 // Use this for initialization
10 void Start ()
11 {
12 //Perform search and get first result in m
13 Match m = Regex.Match(txt, search);
14
15 //While m refers to a search result, loop
16 while(m.Success)
17 {
18 //Print result to console
19 Debug.Log (m.Value);
20
21 //Get next result, if any
22 m = m.NextMatch();
23 }
24 }
25 }
1 using UnityEngine;
2 using System.Collections;
3 using System.Text.RegularExpressions;
4 using System.Linq;
5 //----------------------------------------------------------
6 public class LINQCSV : MonoBehaviour
7 {
8 //----------------------------------------------------------
9 // Use this for initialization
10 void Start ()
11 {
12 //Generate female name
13 //Regular Expression Search Pattern
14 //Find all names prefixed with 'female:' but do not include the prefix in the results
15 string search = @"(?<=\bfemale:)\w+\b";
16
17 //CSV Data - names of characters
18 string CSVData = "male:john,male:tom,male:bob,female:betty,female:jessica,male:dirk";
19
20 //Perform regular expression to retrieve all names prefixed with female. Do not include the prefix 'female:' within the results
21 string[] FemaleNames = (from Match m in Regex.Matches(CSVData, search)
22 select m.Groups[0].Value).ToArray();
23
24 //Print all female names in results
25 foreach(string S in FemaleNames)
26 Debug.Log (S);
27
28 //Now pick a random female name from collection
29 string RandomFemaleName = FemaleNames[Random.Range(0, FemaleNames.Length)];
30 }
31 //----------------------------------------------------------
32 }
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 //-------------------------------------------------
6 public class EnemyQuery : MonoBehaviour
7 {
8 //-------------------------------------------------
9 //Get list of enemies matching search criteria
10 public void FindEnemiesOldWay()
11 {
12 //Get all enemies in scene
13 Enemy[] Enemies = Object.FindObjectsOfType<Enemy>();
14
15 //Filtered Enemies
16 List<Enemy> FilteredData = new List<Enemy>();
17
18 //Loop through enemies and check
19 foreach(Enemy E in Enemies)
20 {
21 if(E.Health <= 50 && E.Defense < 5)
22 {
23 //Found appropriate enemy
24 FilteredData.Add (E);
25 }
26 }
27
28 //Now we can process filtered data
29 //All items in FilteredData match search criteria
30 foreach(Enemy E in FilteredData)
31 {
32 //Process Enemy E
33 Debug.Log (E.name);
34 }
35 }
36 //-------------------------------------------------
37 public void FindEnemiesLinqWay()
38 {
39 //Get all enemies in scene
40 Enemy[] Enemies = Object.FindObjectsOfType<Enemy>();
41
42 //Perform search
43 Enemy[] FilteredData = (from EnemyChar in Enemies
44 where EnemyChar.Health <= 50 && EnemyChar.Defense < 5
45 select EnemyChar).ToArray();
46
47 //Now we can process filtered data
48 //All items in FilteredData match search criteria
49 foreach(Enemy E in FilteredData)
50 {
51 //Process Enemy E
52 Debug.Log (E.name);
53 }
54 }
55 //-------------------------------------------------
56 }