InvDatabase.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/Examples/Item Database")]
  6. public class InvDatabase : MonoBehaviour
  7. {
  8. public static InvDatabase[] list
  9. {
  10. get
  11. {
  12. if (InvDatabase.mIsDirty)
  13. {
  14. InvDatabase.mIsDirty = false;
  15. InvDatabase.mList = NGUITools.FindActive<InvDatabase>();
  16. }
  17. return InvDatabase.mList;
  18. }
  19. }
  20. private void OnEnable()
  21. {
  22. InvDatabase.mIsDirty = true;
  23. }
  24. private void OnDisable()
  25. {
  26. InvDatabase.mIsDirty = true;
  27. }
  28. private InvBaseItem GetItem(int id16)
  29. {
  30. int i = 0;
  31. int count = this.items.Count;
  32. while (i < count)
  33. {
  34. InvBaseItem invBaseItem = this.items[i];
  35. if (invBaseItem.id16 == id16)
  36. {
  37. return invBaseItem;
  38. }
  39. i++;
  40. }
  41. return null;
  42. }
  43. private static InvDatabase GetDatabase(int dbID)
  44. {
  45. int i = 0;
  46. int num = InvDatabase.list.Length;
  47. while (i < num)
  48. {
  49. InvDatabase invDatabase = InvDatabase.list[i];
  50. if (invDatabase.databaseID == dbID)
  51. {
  52. return invDatabase;
  53. }
  54. i++;
  55. }
  56. return null;
  57. }
  58. public static InvBaseItem FindByID(int id32)
  59. {
  60. InvDatabase database = InvDatabase.GetDatabase(id32 >> 16);
  61. return (!(database != null)) ? null : database.GetItem(id32 & 65535);
  62. }
  63. public static InvBaseItem FindByName(string exact)
  64. {
  65. int i = 0;
  66. int num = InvDatabase.list.Length;
  67. while (i < num)
  68. {
  69. InvDatabase invDatabase = InvDatabase.list[i];
  70. int j = 0;
  71. int count = invDatabase.items.Count;
  72. while (j < count)
  73. {
  74. InvBaseItem invBaseItem = invDatabase.items[j];
  75. if (invBaseItem.name == exact)
  76. {
  77. return invBaseItem;
  78. }
  79. j++;
  80. }
  81. i++;
  82. }
  83. return null;
  84. }
  85. public static int FindItemID(InvBaseItem item)
  86. {
  87. int i = 0;
  88. int num = InvDatabase.list.Length;
  89. while (i < num)
  90. {
  91. InvDatabase invDatabase = InvDatabase.list[i];
  92. if (invDatabase.items.Contains(item))
  93. {
  94. return invDatabase.databaseID << 16 | item.id16;
  95. }
  96. i++;
  97. }
  98. return -1;
  99. }
  100. private static InvDatabase[] mList;
  101. private static bool mIsDirty = true;
  102. public int databaseID;
  103. public List<InvBaseItem> items = new List<InvBaseItem>();
  104. public UIAtlas iconAtlas;
  105. }