Inventory.cs script controls the inventory open and close and sorting of the cards in the inventory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | using System.Collections; using System.Collections.Generic; using UnityEngine.EventSystems; using UnityEngine; using UnityEngine.UI; // http://trinary.tech/category/mec/ more efficient coroutines documentation using MEC; // to use .ToList or .ToArray using System.Linq; // Eric's Inventory script, controls opening and closing of items and collectibles so far public class Inventory : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler // IDragHandler, IPointerUpHandler { #region open/close inventory variables [Header("inventory open/close animation")] public RectTransform itemsRectTransform; public RectTransform itemsBackpack; public RectTransform collectiblesRectTransform; public RectTransform collectiblesBackpack; private CoroutineHandle openHandle; private CoroutineHandle closeHandle; private bool waiting = false; private bool itemsBackpackUsed = false; private bool itemsOpening = false; private bool itemsClosing = false; private bool itemsOpen = false; private bool itemsClosed = true; private bool collectiblesBackpackUsed = false; private bool collectiblesOpening = false; private bool collectiblesClosing = false; private bool collectiblesOpen = false; private bool collectiblesClosed = true; // Doesn't matter where it's at, always requires tiny bit of maths... private Vector3 openInventoryPosition = new Vector3(0, 100, 0); private Vector3 closeInventoryPosition = new Vector3(0, -100, 0); private float invAnimDistance; // inventory aniamtion distance private float openIncrement = 0f; //debug variable don't need in final private float closeIncrement = 0f; //debug veriable don't need in final public float speed = 1f; #endregion #region raycast open/close testing variables private GraphicRaycaster myGraphRaycast; private PointerEventData myPointerEventData; private EventSystem myEventSystem; [HideInInspector] public bool canvasRaycastRunning = false; [HideInInspector] public GameObject lastRaycastHit; #endregion #region drag item variables [Header("card storage areas")] public Transform itemsCards; // itemsPanel collectiblesPanel could probably be removed and use itemsRectTransform.gameObject collectiblesRectTransform.gameObject instead public GameObject collectiblesCards; public GameObject deck; //private List<Transform> itemSlots; private Transform[] itemSlots; private GameObject[] collectibleSlots; private InventoryItem inventoryItem; private Vector3 itemZeroSlot = new Vector3(0, 1600 - 64, 0); //height of slot minus half the height of card private Vector3 itemZeroDeck = new Vector3(0, 0, 0); //height of slot minus half the height of card #endregion private void Awake() // trying on awake as it's on this item only { // start items and collectibles closed itemsRectTransform.localPosition = closeInventoryPosition; collectiblesRectTransform.localPosition = closeInventoryPosition; invAnimDistance = openInventoryPosition.y - closeInventoryPosition.y; // animation distance of items and collectibles // graphics raycaster setup myGraphRaycast = GetComponent<GraphicRaycaster>(); myEventSystem = GetComponent<EventSystem>(); // add slots to slot list, .ToList or .ToArray requires using System.Linq itemSlots = itemsCards.GetComponentsInChildren<Transform>();// .ToList(); itemSlots = itemSlots.Skip(1).ToArray(); // skips the first transform which is the parent and recreates the array // moved to proper method //for (int i = 0; i < itemSlots.Length; i++) //{ // Debug.Log("This is Stuipd!" + itemSlots[i]); //} } // gui debugging stuff private void OnGUI() { // trying to sort out these booleans GUI.Label(new Rect(10, 10, 300, 20), "itemsClosed = " + itemsClosed); GUI.Label(new Rect(10, 30, 300, 20), "itemsClosing = " + itemsClosing); GUI.Label(new Rect(10, 50, 300, 20), "itemsOpen = " + itemsOpen); GUI.Label(new Rect(10, 70, 300, 20), "itemsOpening = " + itemsOpening); GUI.Label(new Rect(10, 110, 300, 20), "collectiblesClosed = " + collectiblesClosed); GUI.Label(new Rect(10, 130, 300, 20), "collectiblesClosing = " + collectiblesClosing); GUI.Label(new Rect(10, 150, 300, 20), "collectiblesOpen = " + collectiblesOpen); GUI.Label(new Rect(10, 170, 300, 20), "collectiblesOpening = " + collectiblesOpening); GUI.Label(new Rect(10, 210, 300, 20), "closeIncrement = " + closeIncrement); GUI.Label(new Rect(10, 230, 300, 20), "openIncrement = " + openIncrement); } #region Coroutine canvas raycast start/stop public void OnPointerEnter(PointerEventData pointerEventData) { if (!canvasRaycastRunning) Timing.RunCoroutine(_CanvasRaycast());// start canvas raycasting if it isn't running, should stop physics raycasting CancelInvoke(); // prevent onpointerexit invokes from building up } IEnumerator<float> _CanvasRaycast() { canvasRaycastRunning = true; while (canvasRaycastRunning) // onPointerExit switches this to false { myPointerEventData = new PointerEventData(myEventSystem); myPointerEventData.position = Input.mousePosition; List<RaycastResult> results = new List<RaycastResult>(); myGraphRaycast.Raycast(myPointerEventData, results); // requires graphicraycaster component added to the object, event data doesn't seem to need to be added? MyRaycastResults(results); // this function is probably not necessary yield return 0; } } private void MyRaycastResults(List<RaycastResult> results) { foreach (RaycastResult result in results) { //Debug.Log("Hit " + result.gameObject.name); lastRaycastHit = result.gameObject; if (lastRaycastHit.name == itemsBackpack.name || lastRaycastHit.name == collectiblesBackpack.name || lastRaycastHit.name == itemsRectTransform.name || lastRaycastHit.name == collectiblesRectTransform.name) { OpenInventory(); } // yield return 0; // would use this if the for each were in a coroutine, this would also cause the items to cycle 60 items per second } } #endregion #region open/close inventory private void OpenInventory()// public void OnPointerEnter(PointerEventData pointerEventData) { // set this condition to raycast instead of pointer enter, that way if the backpacks are overlaying other interface components this condition can still resolve to true if (lastRaycastHit.name == itemsBackpack.name) // old pointerEventData.pointerEnter.name == itemsBackpack.name { collectiblesBackpackUsed = false; if (!itemsBackpackUsed && !itemsOpening && !itemsOpen) { itemsBackpackUsed = true; Timing.KillCoroutines(openHandle); if (itemsClosing) { waiting = false; Timing.KillCoroutines(closeHandle); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(itemsRectTransform)); } else if (itemsClosed && collectiblesClosed) { openHandle = Timing.RunCoroutine(_LerpInventoryOpen(itemsRectTransform)); } else if (collectiblesOpen) { waiting = true; closeHandle = Timing.RunCoroutine(_LerpInventoryClose(collectiblesRectTransform)); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(itemsRectTransform)); } else if (collectiblesOpening) { waiting = true; closeHandle = Timing.RunCoroutine(_LerpInventoryClose(collectiblesRectTransform)); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(itemsRectTransform)); } // at face value it would seem this isn't need but if it's closing after invoke time period then still need this else if (collectiblesClosing) { waiting = true; openHandle = Timing.RunCoroutine(_LerpInventoryOpen(itemsRectTransform)); } } } if (lastRaycastHit.name == collectiblesBackpack.name) // || { itemsBackpackUsed = false; if (!collectiblesBackpackUsed && !collectiblesOpening && !collectiblesOpen) { collectiblesBackpackUsed = true; Timing.KillCoroutines(openHandle); if (collectiblesClosing) { waiting = false; Timing.KillCoroutines(closeHandle); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(collectiblesRectTransform)); } // set bools, start opening items else if (itemsClosed && collectiblesClosed) { openHandle = Timing.RunCoroutine(_LerpInventoryOpen(collectiblesRectTransform)); } else if (itemsOpen) { waiting = true; closeHandle = Timing.RunCoroutine(_LerpInventoryClose(itemsRectTransform)); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(collectiblesRectTransform)); } else if (itemsOpening) { waiting = true; closeHandle = Timing.RunCoroutine(_LerpInventoryClose(itemsRectTransform)); openHandle = Timing.RunCoroutine(_LerpInventoryOpen(collectiblesRectTransform)); } // at face value it would seem this isn't need but if it's closing after invoke time period then still need this else if (itemsClosing) { waiting = true; openHandle = Timing.RunCoroutine(_LerpInventoryOpen(collectiblesRectTransform)); } } } } public void OnPointerExit(PointerEventData pointerEventData) { // stop canvas raycast, also stop when F key is released canvasRaycastRunning = false; lastRaycastHit = null; //Debug.Log(lastRaycastHit.name); //Debug.Log(itemsRectTransform.name); Invoke("CloseInventory", 5); } private void CloseInventory() { itemsBackpackUsed = false; collectiblesBackpackUsed = false; if (itemsOpen) closeHandle = Timing.RunCoroutine(_LerpInventoryClose(itemsRectTransform)); if (collectiblesOpen) closeHandle = Timing.RunCoroutine(_LerpInventoryClose(collectiblesRectTransform)); } #endregion #region coroutine lerp items collectibles panels IEnumerator<float> _LerpInventoryClose(RectTransform currentRectTransform) // send parameters in for whatever needs to be closed { if (currentRectTransform.name == itemsRectTransform.name) { itemsClosing = true; itemsClosed = false; itemsOpening = false; itemsOpen = false; } if (currentRectTransform.name == collectiblesRectTransform.name) { collectiblesClosing = true; collectiblesClosed = false; collectiblesOpening = false; collectiblesOpen = false; } float percentClosed = 0f; float percentClosedSqr; while (percentClosed < 1) // While not closed { openIncrement = invAnimDistance - closeIncrement; percentClosed = (closeIncrement / invAnimDistance); // https://stackoverflow.com/questions/13462001/ease-in-and-ease-out-animation-formula, parametric function percentClosedSqr = percentClosed * percentClosed; percentClosed = percentClosedSqr / (2f * (percentClosedSqr - percentClosed) + 1f); currentRectTransform.localPosition = Vector3.Lerp(openInventoryPosition, closeInventoryPosition, percentClosed); closeIncrement = Time.deltaTime * speed + closeIncrement; // put this at the end so that the first frame is 0 lerp if (closeIncrement >= invAnimDistance) { openIncrement = 0f; percentClosed = 1f; // prevents bounceback waiting = false; if (currentRectTransform.name == itemsRectTransform.name) { itemsClosing = false; itemsClosed = true; itemsOpening = false; itemsOpen = false; } if (currentRectTransform.name == collectiblesRectTransform.name) { collectiblesClosing = false; collectiblesClosed = true; collectiblesOpening = false; collectiblesOpen = false; } } yield return 0; } } IEnumerator<float> _LerpInventoryOpen(RectTransform currentRectTransform) { // wait until other is closed while (waiting) yield return Timing.WaitUntilDone(closeHandle); if (currentRectTransform.name == itemsRectTransform.name) { itemsClosing = false; itemsClosed = false; itemsOpening = true; itemsOpen = false; } if (currentRectTransform.name == collectiblesRectTransform.name) { collectiblesClosing = false; collectiblesClosed = false; collectiblesOpening = true; collectiblesOpen = false; } float percentOpen = 0f; float percentOpenSqr; while (percentOpen < 1) // While not closed { closeIncrement = invAnimDistance - openIncrement; percentOpen = (openIncrement / invAnimDistance); // https://stackoverflow.com/questions/13462001/ease-in-and-ease-out-animation-formula, parametric function percentOpenSqr = percentOpen * percentOpen; percentOpen = percentOpenSqr / (2f * (percentOpenSqr - percentOpen) + 1f); currentRectTransform.localPosition = Vector3.Lerp(closeInventoryPosition, openInventoryPosition, percentOpen); openIncrement = Time.deltaTime * speed + openIncrement; // put this at the end so that the first frame is 0 lerp if (openIncrement >= invAnimDistance) { closeIncrement = 0f; percentOpen = 1f; // prevents bounceback if (currentRectTransform.name == itemsRectTransform.name) { itemsClosing = false; itemsClosed = false; itemsOpening = false; itemsOpen = true; } if (currentRectTransform.name == collectiblesRectTransform.name) { collectiblesClosing = false; collectiblesClosed = false; collectiblesOpening = false; collectiblesOpen = true; } } yield return 0; } } IEnumerator<float> _WaitUntilDone() // not sure how to do this yet { yield return 0; } #endregion #region start item drag and slot sorting public void OnPointerDown(PointerEventData pointerEventData) // OnPointerDown over OnDrag so only send one start item drag { //Debug.Log(pointerEventData.pointerEnter.name + " is being dragged by OnPointerDown"); inventoryItem = pointerEventData.pointerEnter.GetComponent<InventoryItem>(); if (inventoryItem != null) { //Debug.Log("found InvenotryItem script"); inventoryItem.StartItemDrag(inventoryItem.gameObject); } } public void SlotSort(GameObject item) { // sort slots } #endregion #region world, hand, deck public void InWorld() { // objects in world } public void InHand(GameObject item, RectTransform itemRectTransform) { //item.transform.SetParent(slot01.transform); for (int i = 0; i < itemSlots.Length; i++) { Debug.Log("This is Stuipd!" + itemSlots[i]); if (itemSlots[i].childCount == 0) { item.transform.SetParent(itemSlots[i]); break; } } itemRectTransform.localPosition = itemZeroSlot; } public void InDeck(GameObject item, RectTransform itemRectTransform) { item.transform.SetParent(deck.transform); itemRectTransform.localPosition = itemZeroDeck; } #endregion } |