LootPack_inspector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. [CustomEditor(typeof(LootPack))]
  8. public class LootPack_inspector : Editor
  9. {
  10. bool _itemExpand = false;
  11. bool _currencyExpand = false;
  12. Color _activeColor = new Color(0.1F, 0.3F, 0.5F);
  13. Color _disableColor = new Color(0F, 0.1F, 0.3F);
  14. Color _actionColor = new Color(0F, 1F, 0.4F);
  15. Color _titleColor = new Color(0.3F, 0.5F, 1F);
  16. Color _buttonColor = new Color(0F, 0.8F, 0.3F);
  17. GUIStyle _titleButtonStyle;
  18. ItemManager Manager;
  19. public override void OnInspectorGUI()
  20. {
  21. GUI.changed = false;
  22. bool _valueChanged = false;
  23. _titleButtonStyle = new GUIStyle(GUI.skin.button);
  24. _titleButtonStyle.alignment = TextAnchor.MiddleLeft;
  25. Color _backgroundColor = GUI.backgroundColor;
  26. var script = MonoScript.FromScriptableObject(this);
  27. LootPack myTarget = (LootPack)target;
  28. string _thePath = AssetDatabase.GetAssetPath(script);
  29. _thePath = _thePath.Replace("LootPack_inspector.cs", "");
  30. Texture logoIcon = (Texture)AssetDatabase.LoadAssetAtPath(_thePath + "Logo.png", typeof(Texture));
  31. Texture warningIcon = (Texture)AssetDatabase.LoadAssetAtPath(_thePath + "warning.png", typeof(Texture));
  32. GUILayout.BeginHorizontal();
  33. GUILayout.Box(logoIcon);
  34. GUILayout.EndHorizontal();
  35. GUILayout.BeginHorizontal();
  36. EditorGUILayout.HelpBox("You can set up different <LootPack> prefabs with their own drop settings. When a player interacts with it, call LootPack.OpenPack() to open the loot UI."
  37. , MessageType.Info, true);
  38. GUILayout.EndHorizontal();
  39. GUILayout.BeginHorizontal();
  40. GUILayout.Space(30);
  41. GUI.backgroundColor = new Color(1F, 0.8F, 0.1F, 1F);
  42. if (GUILayout.Button(new GUIContent("Help","Open [User Guide] pdf."), GUILayout.Width(120)))
  43. {
  44. string _path = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + _thePath.Replace("Editor", "Documentation") + "UserGuide.pdf";
  45. Application.OpenURL(_path);
  46. }
  47. GUI.backgroundColor = _backgroundColor;
  48. GUILayout.EndHorizontal();
  49. if (Manager == null)
  50. {
  51. Manager = FindObjectOfType<ItemManager>();
  52. }
  53. if (Manager == null)
  54. {
  55. GameObject _managerObj = AssetDatabase.LoadAssetAtPath<GameObject>(_thePath.Replace("Editor/", "Prefabs/System/InventoryEngine.prefab"));
  56. if (_managerObj != null)
  57. {
  58. Manager = _managerObj.GetComponent<ItemManager>();
  59. }
  60. else
  61. {
  62. GUILayout.BeginHorizontal();
  63. GUILayout.Space(20);
  64. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  65. GUI.color = Color.red;
  66. GUILayout.Label("You must have ItemManager prefab in the scene to modify this script.");
  67. GUI.color = Color.white;
  68. GUILayout.EndHorizontal();
  69. return;
  70. }
  71. return;
  72. }
  73. GUILayout.BeginHorizontal();
  74. GUILayout.Space(30);
  75. GUILayout.Label("UID:",GUILayout.Width(100));
  76. myTarget.UID = EditorGUILayout.TextField(myTarget.UID);
  77. GUILayout.EndHorizontal();
  78. GUILayout.BeginHorizontal();
  79. GUI.backgroundColor = _currencyExpand ? _activeColor : _disableColor;
  80. _titleButtonStyle.normal.textColor = _currencyExpand ? Color.white : new Color(0.65F, 0.65F, 0.65F);
  81. GUI.color = (myTarget.CurrencyMin.Count <= 0 || myTarget.CurrencyMax.Count <= 0) ? Color.red : Color.white;
  82. GUILayout.Label(_currencyExpand ? "[-]" : "[+]", GUILayout.Width(20));
  83. if (GUILayout.Button(new GUIContent(" Currency","The currencies this loot pack contains."), _titleButtonStyle))
  84. {
  85. _currencyExpand = !_currencyExpand;
  86. EditorGUI.FocusTextInControl(null);
  87. }
  88. GUI.color = Color.white;
  89. GUI.backgroundColor = _backgroundColor;
  90. if (Manager.currencies.Count <= 0)
  91. {
  92. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  93. }
  94. else
  95. {
  96. bool _noMatch = false;
  97. if (myTarget.CurrencyMin.Count < Manager.currencies.Count) {
  98. myTarget.CurrencyMin.Add(0);
  99. _noMatch = true;
  100. }
  101. else if (myTarget.CurrencyMin.Count > Manager.currencies.Count) {
  102. myTarget.CurrencyMin.RemoveAt(0);
  103. _noMatch = true;
  104. }
  105. if (myTarget.CurrencyMax.Count < Manager.currencies.Count)
  106. {
  107. myTarget.CurrencyMax.Add(0);
  108. _noMatch = true;
  109. }
  110. else if (myTarget.CurrencyMax.Count > Manager.currencies.Count)
  111. {
  112. myTarget.CurrencyMax.RemoveAt(0);
  113. _noMatch = true;
  114. }
  115. if (_noMatch) return;
  116. }
  117. GUILayout.EndHorizontal();
  118. if (_currencyExpand)
  119. {
  120. if (Manager.currencies.Count <= 0)
  121. {
  122. GUILayout.BeginHorizontal();
  123. GUILayout.Space(20);
  124. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  125. GUI.color = Color.red;
  126. GUILayout.Label("You must have at least one currency setup in ItemManager prefab.");
  127. GUI.color = Color.white;
  128. GUILayout.EndHorizontal();
  129. }
  130. //===Currency List
  131. for (int i = 0; i < Manager.currencies.Count; i++)
  132. {
  133. if (myTarget.CurrencyMin.Count <= i) myTarget.CurrencyMin.Add(0);
  134. if (myTarget.CurrencyMax.Count <= i) myTarget.CurrencyMax.Add(0);
  135. GUILayout.BeginHorizontal();
  136. GUILayout.Space(30);
  137. GUILayout.Label("(ID:" + i.ToString() + ")", GUILayout.Width(40), GUILayout.Height(20));
  138. GUILayout.Box(Manager.currencies[i].icon.texture, GUILayout.Width(20), GUILayout.Height(20));
  139. GUI.color = Manager.currencies[i].color;
  140. GUILayout.Label(Manager.currencies[i].name + " :", GUILayout.Width(100), GUILayout.Height(20));
  141. GUI.color = Color.white;
  142. GUI.backgroundColor = Manager.currencies[i].color;
  143. myTarget.CurrencyMin[i] = EditorGUILayout.IntField(myTarget.CurrencyMin[i], GUILayout.Width(100), GUILayout.Height(20));
  144. GUILayout.Label("~", GUILayout.Width(15), GUILayout.Height(20));
  145. myTarget.CurrencyMax[i] = EditorGUILayout.IntField(myTarget.CurrencyMax[i], GUILayout.Width(100), GUILayout.Height(20));
  146. GUI.backgroundColor = _backgroundColor;
  147. GUILayout.EndHorizontal();
  148. }
  149. //===Currency List
  150. }
  151. GUILayout.BeginHorizontal();
  152. GUI.backgroundColor = _itemExpand ? _activeColor : _disableColor;
  153. _titleButtonStyle.normal.textColor = _itemExpand ? Color.white : new Color(0.65F, 0.65F, 0.65F);
  154. GUI.color = Manager.items.Count <= 0 ? Color.red : Color.white;
  155. GUILayout.Label(_itemExpand ? "[-]" : "[+]", GUILayout.Width(20));
  156. if (GUILayout.Button(new GUIContent(" Drop Items Pool (" +myTarget.ItemPool.Count.ToString()+")","When this loot pack drops, it will random pick items from this pool."), _titleButtonStyle))
  157. {
  158. _itemExpand = !_itemExpand;
  159. EditorGUI.FocusTextInControl(null);
  160. }
  161. GUI.backgroundColor = Color.white;
  162. GUI.color = Color.white;
  163. if (Manager.items.Count <= 0)
  164. {
  165. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  166. }
  167. GUILayout.EndHorizontal();
  168. string[] _itemOption = new string[Manager.items.Count];
  169. if (_itemExpand)
  170. {
  171. for (int i = 0; i < _itemOption.Length; i++) _itemOption[i] = Manager.items[i].name;
  172. }
  173. if (_itemExpand)
  174. {
  175. if (Manager.items.Count <= 0)
  176. {
  177. GUILayout.BeginHorizontal();
  178. GUILayout.Space(20);
  179. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  180. GUI.color = Color.red;
  181. GUILayout.Label("You must have at least one item setup in ItemManager prefab.");
  182. GUI.color = Color.white;
  183. GUILayout.EndHorizontal();
  184. }
  185. else
  186. {
  187. GUILayout.BeginHorizontal();
  188. GUILayout.Space(30);
  189. GUI.backgroundColor = _buttonColor;
  190. if (GUILayout.Button(new GUIContent("Add new [Item]","Add an new item to the pool."), GUILayout.Width(150)))
  191. {
  192. myTarget.ItemPool.Add(0);
  193. _valueChanged = true;
  194. }
  195. GUILayout.EndHorizontal();
  196. for (int i = 0; i < myTarget.ItemPool.Count; i++)
  197. {
  198. GUILayout.BeginHorizontal();
  199. GUILayout.Space(30);
  200. GUILayout.Box(Manager.items[myTarget.ItemPool[i]].icon, GUILayout.Width(20), GUILayout.Height(20));
  201. int _item = myTarget.ItemPool[i];
  202. EditorGUI.BeginChangeCheck();
  203. GUI.backgroundColor = Manager.itemTypes[Manager.items[myTarget.ItemPool[i]].type].color;
  204. _item = EditorGUILayout.Popup("", _item, _itemOption, GUILayout.Width(180));
  205. if (EditorGUI.EndChangeCheck()) myTarget.ItemPool[i] = _item;
  206. GUI.backgroundColor = Color.red;
  207. if (GUILayout.Button("X", GUILayout.Width(30)))
  208. {
  209. myTarget.ItemPool.RemoveAt(i);
  210. }
  211. GUI.backgroundColor = _backgroundColor;
  212. GUILayout.EndHorizontal();
  213. }
  214. }
  215. }
  216. GUILayout.BeginHorizontal();
  217. GUILayout.Space(30);
  218. GUILayout.Label(new GUIContent("Maxmium Item Count:","When random pick items from the pool, this number will be the maxmium item count it could be"), GUILayout.Width(180));
  219. myTarget.MaxiumItemCount = EditorGUILayout.IntSlider(myTarget.MaxiumItemCount, Mathf.Min(myTarget.ItemPool.Count, 1), myTarget.ItemPool.Count);
  220. GUILayout.EndHorizontal();
  221. if (myTarget.ItemPool.Count <= 0)
  222. {
  223. GUILayout.BeginHorizontal();
  224. GUILayout.Space(30);
  225. GUILayout.Box(warningIcon, GUIStyle.none, GUILayout.Width(20));
  226. GUI.color = Color.yellow;
  227. GUILayout.Label("[Maxmium Item Count] can not be larger than the [Drop Items Pool] count");
  228. GUI.color = Color.white;
  229. GUILayout.EndHorizontal();
  230. }
  231. GUILayout.BeginHorizontal();
  232. GUILayout.Space(30);
  233. GUILayout.Label(new GUIContent("Maxmium Count Each Item:", "The count of each item stack will be random from 1 to this number."), GUILayout.Width(180));
  234. myTarget.MaxiumCountEachItem = EditorGUILayout.IntSlider(myTarget.MaxiumCountEachItem, 1,99);
  235. GUILayout.EndHorizontal();
  236. GUILayout.BeginHorizontal();
  237. GUILayout.Space(30);
  238. GUILayout.Label(new GUIContent("Drop Chance Multiplier:","The drop chance of each item will be the drop chance in the [ItemManager] setting multiply this number."), GUILayout.Width(180));
  239. myTarget.DropChanceMultiplier = EditorGUILayout.Slider(myTarget.DropChanceMultiplier, 0.5F, 5F);
  240. GUILayout.Label("x", GUILayout.Width(15));
  241. GUILayout.EndHorizontal();
  242. if (Manager.EnableEnhancing)
  243. {
  244. GUILayout.BeginHorizontal();
  245. GUILayout.Space(30);
  246. GUILayout.Label(new GUIContent("Random Upgrade Level:", "Will the items have random upgrade level?"), GUILayout.Width(180));
  247. myTarget.RandomLevel = EditorGUILayout.Toggle(myTarget.RandomLevel);
  248. GUILayout.EndHorizontal();
  249. }
  250. GUILayout.BeginHorizontal();
  251. GUILayout.Space(30);
  252. GUILayout.Label(new GUIContent("Destroy when player close loot window:","Will the loot pack be destroyed when player close the loot window?"), GUILayout.Width(250));
  253. myTarget.DestoryWhenPlayerCloseLootWindow = EditorGUILayout.Toggle(myTarget.DestoryWhenPlayerCloseLootWindow);
  254. GUILayout.EndHorizontal();
  255. if ((_valueChanged || GUI.changed) && !Application.isPlaying) myTarget.UpdatePrefab();
  256. }
  257. }
  258. }