Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class DragItem : MonoBehaviour, IDragHandler, IPointerDownHandler, IEndDragHandler
  7. {
  8. private Vector2 pointerOffset;
  9. private RectTransform rectTransform;
  10. private RectTransform rectTransformSlot;
  11. private CanvasGroup canvasGroup;
  12. private GameObject oldSlot;
  13. private Inventory inventory;
  14. private Transform draggedItemBox;
  15.  
  16. public delegate void ItemDelegate();
  17. public static event ItemDelegate updateInventoryList;
  18. void Start()
  19. {
  20. rectTransform = GetComponent<RectTransform>();
  21. canvasGroup = GetComponent<CanvasGroup>();
  22. rectTransformSlot = GameObject.FindGameObjectWithTag("DraggingItem").GetComponent<RectTransform>();
  23. inventory = transform.parent.parent.parent.GetComponent<Inventory>();
  24. draggedItemBox = GameObject.FindGameObjectWithTag("DraggingItem").transform;
  25. }
  26.  
  27.  
  28. public void OnDrag(PointerEventData data)
  29. {
  30. if (rectTransform == null)
  31. return;
  32.  
  33. if (data.button == PointerEventData.InputButton.Left && transform.parent.GetComponent<CraftResultSlot>() == null)
  34. {
  35. rectTransform.SetAsLastSibling();
  36. transform.SetParent(draggedItemBox);
  37. Vector2 localPointerPosition;
  38. canvasGroup.blocksRaycasts = false;
  39. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransformSlot, Input.mousePosition, data.pressEventCamera, out localPointerPosition))
  40. {
  41. rectTransform.localPosition = localPointerPosition - pointerOffset;
  42. if (transform.GetComponent<ConsumeItem>().duplication != null)
  43. Destroy(transform.GetComponent<ConsumeItem>().duplication);
  44. }
  45. }
  46.  
  47. inventory.OnUpdateItemList();
  48. }
  49.  
  50.  
  51.  
  52. public void OnPointerDown(PointerEventData data)
  53. {
  54. if (data.button == PointerEventData.InputButton.Left)
  55. {
  56. RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, data.position, data.pressEventCamera, out pointerOffset);
  57. oldSlot = transform.parent.gameObject;
  58. }
  59. if (updateInventoryList != null)
  60. updateInventoryList();
  61. }
  62.  
  63. public void createDuplication(GameObject Item)
  64. {
  65. Item item = Item.GetComponent<ItemOnObject>().item;
  66. GameObject duplication = GameObject.FindGameObjectWithTag("MainInventory").GetComponent<Inventory>().addItemToInventory(item.itemID, item.itemValue);
  67. duplication.transform.parent.parent.parent.GetComponent<Inventory>().stackableSettings();
  68. Item.GetComponent<ConsumeItem>().duplication = duplication;
  69. duplication.GetComponent<ConsumeItem>().duplication = Item;
  70. }
  71.  
  72. public void OnEndDrag(PointerEventData data)
  73. {
  74. if (data.button == PointerEventData.InputButton.Left)
  75. {
  76. canvasGroup.blocksRaycasts = true;
  77. Transform newSlot = null;
  78. if (data.pointerEnter != null)
  79. newSlot = data.pointerEnter.transform;
  80.  
  81. if (newSlot != null)
  82. {
  83. //getting the items from the slots, GameObjects and RectTransform
  84. GameObject firstItemGameObject = this.gameObject;
  85. GameObject secondItemGameObject = newSlot.parent.gameObject;
  86. RectTransform firstItemRectTransform = this.gameObject.GetComponent<RectTransform>();
  87. RectTransform secondItemRectTransform = newSlot.parent.GetComponent<RectTransform>();
  88. Item firstItem = rectTransform.GetComponent<ItemOnObject>().item;
  89. Item secondItem = new Item();
  90. if (newSlot.parent.GetComponent<ItemOnObject>() != null)
  91. secondItem = newSlot.parent.GetComponent<ItemOnObject>().item;
  92.  
  93. //get some informations about the two items
  94. bool sameItem = firstItem.itemName == secondItem.itemName;
  95. bool sameItemRerferenced = firstItem.Equals(secondItem);
  96. bool secondItemStack = false;
  97. bool firstItemStack = false;
  98. if (sameItem)
  99. {
  100. firstItemStack = firstItem.itemValue < firstItem.maxStack;
  101. secondItemStack = secondItem.itemValue < secondItem.maxStack;
  102. }
  103.  
  104. GameObject Inventory = secondItemRectTransform.parent.gameObject;
  105. if (Inventory.tag == "Slot")
  106. Inventory = secondItemRectTransform.parent.parent.parent.gameObject;
  107.  
  108. if (Inventory.tag.Equals("Slot"))
  109. Inventory = Inventory.transform.parent.parent.gameObject;
  110.  
  111. //dragging in an Inventory
  112. if (Inventory.GetComponent<Hotbar>() == null && Inventory.GetComponent<EquipmentSystem>() == null && Inventory.GetComponent<CraftSystem>() == null)
  113. {
  114. //you cannot attach items to the resultslot of the craftsystem
  115. if (newSlot.transform.parent.tag == "ResultSlot" || newSlot.transform.tag == "ResultSlot" || newSlot.transform.parent.parent.tag == "ResultSlot")
  116. {
  117. firstItemGameObject.transform.SetParent(oldSlot.transform);
  118. firstItemRectTransform.localPosition = Vector3.zero;
  119. }
  120. else
  121. {
  122. int newSlotChildCount = newSlot.transform.parent.childCount;
  123. bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  124. //dragging on a slot where allready is an item on
  125. if (newSlotChildCount != 0 && isOnSlot)
  126. {
  127. //check if the items fits into the other item
  128. bool fitsIntoStack = false;
  129. if (sameItem)
  130. fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  131. //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  132.  
  133. if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  134. {
  135. //if the item does not fit into the other item
  136. if (fitsIntoStack && !sameItemRerferenced)
  137. {
  138. secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  139. secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  140. Destroy(firstItemGameObject);
  141. secondItemRectTransform.localPosition = Vector3.zero;
  142. if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  143. {
  144. GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  145. dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  146. dup.GetComponent<SplitItem>().inv.stackableSettings();
  147. dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  148. }
  149. }
  150.  
  151. else
  152. {
  153. //creates the rest of the item
  154. int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  155.  
  156. //fill up the other stack and adds the rest to the other stack
  157. if (!fitsIntoStack && rest > 0)
  158. {
  159. firstItem.itemValue = firstItem.maxStack;
  160. secondItem.itemValue = rest;
  161.  
  162. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  163. secondItemGameObject.transform.SetParent(oldSlot.transform);
  164.  
  165. firstItemRectTransform.localPosition = Vector3.zero;
  166. secondItemRectTransform.localPosition = Vector3.zero;
  167. }
  168. }
  169.  
  170. }
  171. //if does not fit
  172. else
  173. {
  174. //creates the rest of the item
  175. int rest = 0;
  176. if (sameItem)
  177. rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  178.  
  179. //fill up the other stack and adds the rest to the other stack
  180. if (!fitsIntoStack && rest > 0)
  181. {
  182. secondItem.itemValue = firstItem.maxStack;
  183. firstItem.itemValue = rest;
  184.  
  185. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  186. secondItemGameObject.transform.SetParent(oldSlot.transform);
  187.  
  188. firstItemRectTransform.localPosition = Vector3.zero;
  189. secondItemRectTransform.localPosition = Vector3.zero;
  190. }
  191. //if they are different items or the stack is full, they get swapped
  192. else if (!fitsIntoStack && rest == 0)
  193. {
  194. //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
  195. if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
  196. {
  197. newSlot.transform.parent.parent.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  198. oldSlot.transform.parent.parent.GetComponent<Inventory>().EquiptItem(secondItem);
  199.  
  200. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  201. secondItemGameObject.transform.SetParent(oldSlot.transform);
  202. secondItemRectTransform.localPosition = Vector3.zero;
  203. firstItemRectTransform.localPosition = Vector3.zero;
  204.  
  205. if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  206. Destroy(secondItemGameObject.GetComponent<ConsumeItem>().duplication);
  207.  
  208. }
  209. //if you are dragging an item from the equipmentsystem to the inventory and they are not from the same itemtype they do not get swapped.
  210. else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
  211. {
  212. firstItemGameObject.transform.SetParent(oldSlot.transform);
  213. firstItemRectTransform.localPosition = Vector3.zero;
  214. }
  215. //swapping for the rest of the inventorys
  216. else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null)
  217. {
  218. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  219. secondItemGameObject.transform.SetParent(oldSlot.transform);
  220. secondItemRectTransform.localPosition = Vector3.zero;
  221. firstItemRectTransform.localPosition = Vector3.zero;
  222. }
  223. }
  224.  
  225. }
  226.  
  227. }
  228.  
  229. //empty slot
  230. else
  231. {
  232. if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  233. {
  234. firstItemGameObject.transform.SetParent(oldSlot.transform);
  235. firstItemRectTransform.localPosition = Vector3.zero;
  236. }
  237. else
  238. {
  239. firstItemGameObject.transform.SetParent(newSlot.transform);
  240. firstItemRectTransform.localPosition = Vector3.zero;
  241.  
  242. if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  243. oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  244. }
  245. }
  246. }
  247. }
  248.  
  249.  
  250.  
  251. //dragging into a Hotbar
  252. if (Inventory.GetComponent<Hotbar>() != null)
  253. {
  254. int newSlotChildCount = newSlot.transform.parent.childCount;
  255. bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  256. //dragging on a slot where allready is an item on
  257. if (newSlotChildCount != 0 && isOnSlot)
  258. {
  259. //check if the items fits into the other item
  260. bool fitsIntoStack = false;
  261. if (sameItem)
  262. fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  263. //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  264.  
  265. if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  266. {
  267. //if the item does not fit into the other item
  268. if (fitsIntoStack && !sameItemRerferenced)
  269. {
  270. secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  271. secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  272. Destroy(firstItemGameObject);
  273. secondItemRectTransform.localPosition = Vector3.zero;
  274. if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  275. {
  276. GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  277. dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  278. Inventory.GetComponent<Inventory>().stackableSettings();
  279. dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  280. }
  281. }
  282.  
  283. else
  284. {
  285. //creates the rest of the item
  286. int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  287.  
  288. //fill up the other stack and adds the rest to the other stack
  289. if (!fitsIntoStack && rest > 0)
  290. {
  291. firstItem.itemValue = firstItem.maxStack;
  292. secondItem.itemValue = rest;
  293.  
  294. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  295. secondItemGameObject.transform.SetParent(oldSlot.transform);
  296.  
  297. firstItemRectTransform.localPosition = Vector3.zero;
  298. secondItemRectTransform.localPosition = Vector3.zero;
  299.  
  300. createDuplication(this.gameObject);
  301. secondItemGameObject.GetComponent<ConsumeItem>().duplication.GetComponent<ItemOnObject>().item = secondItem;
  302. secondItemGameObject.GetComponent<SplitItem>().inv.stackableSettings();
  303.  
  304. }
  305. }
  306.  
  307. }
  308. //if does not fit
  309. else
  310. {
  311. //creates the rest of the item
  312. int rest = 0;
  313. if (sameItem)
  314. rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  315.  
  316. bool fromEquip = oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null;
  317.  
  318. //fill up the other stack and adds the rest to the other stack
  319. if (!fitsIntoStack && rest > 0)
  320. {
  321. secondItem.itemValue = firstItem.maxStack;
  322. firstItem.itemValue = rest;
  323.  
  324. createDuplication(this.gameObject);
  325.  
  326. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  327. secondItemGameObject.transform.SetParent(oldSlot.transform);
  328.  
  329. firstItemRectTransform.localPosition = Vector3.zero;
  330. secondItemRectTransform.localPosition = Vector3.zero;
  331.  
  332. }
  333. //if they are different items or the stack is full, they get swapped
  334. else if (!fitsIntoStack && rest == 0)
  335. {
  336. if (!fromEquip)
  337. {
  338. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  339. secondItemGameObject.transform.SetParent(oldSlot.transform);
  340. secondItemRectTransform.localPosition = Vector3.zero;
  341. firstItemRectTransform.localPosition = Vector3.zero;
  342.  
  343. if (oldSlot.transform.parent.parent.gameObject.Equals(GameObject.FindGameObjectWithTag("MainInventory")))
  344. {
  345. Destroy(secondItemGameObject.GetComponent<ConsumeItem>().duplication);
  346. createDuplication(firstItemGameObject);
  347. }
  348. else
  349. {
  350. createDuplication(firstItemGameObject);
  351. }
  352. }
  353. else
  354. {
  355. firstItemGameObject.transform.SetParent(oldSlot.transform);
  356. firstItemRectTransform.localPosition = Vector3.zero;
  357. }
  358. }
  359.  
  360. }
  361. }
  362. //empty slot
  363. else
  364. {
  365. if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  366. {
  367. firstItemGameObject.transform.SetParent(oldSlot.transform);
  368. firstItemRectTransform.localPosition = Vector3.zero;
  369. }
  370. else
  371. {
  372. firstItemGameObject.transform.SetParent(newSlot.transform);
  373. firstItemRectTransform.localPosition = Vector3.zero;
  374.  
  375. if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  376. oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  377. createDuplication(firstItemGameObject);
  378. }
  379. }
  380.  
  381. }
  382.  
  383.  
  384. //dragging into a equipmentsystem/charactersystem
  385. if (Inventory.GetComponent<EquipmentSystem>() != null)
  386. {
  387. ItemType[] itemTypeOfSlots = GameObject.FindGameObjectWithTag("EquipmentSystem").GetComponent<EquipmentSystem>().itemTypeOfSlots;
  388. int newSlotChildCount = newSlot.transform.parent.childCount;
  389. bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  390. bool sameItemType = firstItem.itemType == secondItem.itemType;
  391. bool fromHot = oldSlot.transform.parent.parent.GetComponent<Hotbar>() != null;
  392.  
  393. //dragging on a slot where allready is an item on
  394. if (newSlotChildCount != 0 && isOnSlot)
  395. {
  396. //items getting swapped if they are the same itemtype
  397. if (sameItemType && !sameItemRerferenced) //
  398. {
  399. Transform temp1 = secondItemGameObject.transform.parent.parent.parent;
  400. Transform temp2 = oldSlot.transform.parent.parent;
  401.  
  402. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  403. secondItemGameObject.transform.SetParent(oldSlot.transform);
  404. secondItemRectTransform.localPosition = Vector3.zero;
  405. firstItemRectTransform.localPosition = Vector3.zero;
  406.  
  407. if (!temp1.Equals(temp2))
  408. {
  409. if (firstItem.itemType == ItemType.UFPS_Weapon)
  410. {
  411. Inventory.GetComponent<Inventory>().UnEquipItem1(secondItem);
  412. Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  413. }
  414. else
  415. {
  416. Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  417. if (secondItem.itemType != ItemType.Backpack)
  418. Inventory.GetComponent<Inventory>().UnEquipItem1(secondItem);
  419. }
  420. }
  421.  
  422. if (fromHot)
  423. createDuplication(secondItemGameObject);
  424.  
  425. }
  426. //if they are not from the same Itemtype the dragged one getting placed back
  427. else
  428. {
  429. firstItemGameObject.transform.SetParent(oldSlot.transform);
  430. firstItemRectTransform.localPosition = Vector3.zero;
  431.  
  432. if (fromHot)
  433. createDuplication(firstItemGameObject);
  434. }
  435.  
  436. }
  437. //if the slot is empty
  438. else
  439. {
  440. for (int i = 0; i < newSlot.parent.childCount; i++)
  441. {
  442. if (newSlot.Equals(newSlot.parent.GetChild(i)))
  443. {
  444. //checking if it is the right slot for the item
  445. if (itemTypeOfSlots[i] == transform.GetComponent<ItemOnObject>().item.itemType)
  446. {
  447. transform.SetParent(newSlot);
  448. rectTransform.localPosition = Vector3.zero;
  449.  
  450. if (!oldSlot.transform.parent.parent.Equals(newSlot.transform.parent.parent))
  451. Inventory.GetComponent<Inventory>().EquiptItem(firstItem);
  452.  
  453. }
  454. //else it get back to the old slot
  455. else
  456. {
  457. transform.SetParent(oldSlot.transform);
  458. rectTransform.localPosition = Vector3.zero;
  459. if (fromHot)
  460. createDuplication(firstItemGameObject);
  461. }
  462. }
  463. }
  464. }
  465.  
  466. }
  467.  
  468. if (Inventory.GetComponent<CraftSystem>() != null)
  469. {
  470. CraftSystem cS = Inventory.GetComponent<CraftSystem>();
  471. int newSlotChildCount = newSlot.transform.parent.childCount;
  472.  
  473.  
  474. bool isOnSlot = newSlot.transform.parent.GetChild(0).tag == "ItemIcon";
  475. //dragging on a slot where allready is an item on
  476. if (newSlotChildCount != 0 && isOnSlot)
  477. {
  478. //check if the items fits into the other item
  479. bool fitsIntoStack = false;
  480. if (sameItem)
  481. fitsIntoStack = (firstItem.itemValue + secondItem.itemValue) <= firstItem.maxStack;
  482. //if the item is stackable checking if the firstitemstack and seconditemstack is not full and check if they are the same items
  483.  
  484. if (inventory.stackable && sameItem && firstItemStack && secondItemStack)
  485. {
  486. //if the item does not fit into the other item
  487. if (fitsIntoStack && !sameItemRerferenced)
  488. {
  489. secondItem.itemValue = firstItem.itemValue + secondItem.itemValue;
  490. secondItemGameObject.transform.SetParent(newSlot.parent.parent);
  491. Destroy(firstItemGameObject);
  492. secondItemRectTransform.localPosition = Vector3.zero;
  493.  
  494.  
  495. if (secondItemGameObject.GetComponent<ConsumeItem>().duplication != null)
  496. {
  497. GameObject dup = secondItemGameObject.GetComponent<ConsumeItem>().duplication;
  498. dup.GetComponent<ItemOnObject>().item.itemValue = secondItem.itemValue;
  499. dup.GetComponent<SplitItem>().inv.stackableSettings();
  500. dup.transform.parent.parent.parent.GetComponent<Inventory>().updateItemList();
  501. }
  502. cS.ListWithItem();
  503. }
  504.  
  505. else
  506. {
  507. //creates the rest of the item
  508. int rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  509.  
  510. //fill up the other stack and adds the rest to the other stack
  511. if (!fitsIntoStack && rest > 0)
  512. {
  513. firstItem.itemValue = firstItem.maxStack;
  514. secondItem.itemValue = rest;
  515.  
  516. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  517. secondItemGameObject.transform.SetParent(oldSlot.transform);
  518.  
  519. firstItemRectTransform.localPosition = Vector3.zero;
  520. secondItemRectTransform.localPosition = Vector3.zero;
  521. cS.ListWithItem();
  522.  
  523.  
  524. }
  525. }
  526.  
  527. }
  528. //if does not fit
  529. else
  530. {
  531. //creates the rest of the item
  532. int rest = 0;
  533. if (sameItem)
  534. rest = (firstItem.itemValue + secondItem.itemValue) % firstItem.maxStack;
  535.  
  536. //fill up the other stack and adds the rest to the other stack
  537. if (!fitsIntoStack && rest > 0)
  538. {
  539. secondItem.itemValue = firstItem.maxStack;
  540. firstItem.itemValue = rest;
  541.  
  542. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  543. secondItemGameObject.transform.SetParent(oldSlot.transform);
  544.  
  545. firstItemRectTransform.localPosition = Vector3.zero;
  546. secondItemRectTransform.localPosition = Vector3.zero;
  547. cS.ListWithItem();
  548.  
  549. }
  550. //if they are different items or the stack is full, they get swapped
  551. else if (!fitsIntoStack && rest == 0)
  552. {
  553. //if you are dragging an item from equipmentsystem to the inventory and try to swap it with the same itemtype
  554. if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType == secondItem.itemType)
  555. {
  556.  
  557. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  558. secondItemGameObject.transform.SetParent(oldSlot.transform);
  559. secondItemRectTransform.localPosition = Vector3.zero;
  560. firstItemRectTransform.localPosition = Vector3.zero;
  561.  
  562. oldSlot.transform.parent.parent.GetComponent<Inventory>().EquiptItem(secondItem);
  563. newSlot.transform.parent.parent.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  564. }
  565. //if you are dragging an item from the equipmentsystem to the inventory and they are not from the same itemtype they do not get swapped.
  566. else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null && firstItem.itemType != secondItem.itemType)
  567. {
  568. firstItemGameObject.transform.SetParent(oldSlot.transform);
  569. firstItemRectTransform.localPosition = Vector3.zero;
  570. }
  571. //swapping for the rest of the inventorys
  572. else if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null)
  573. {
  574. firstItemGameObject.transform.SetParent(secondItemGameObject.transform.parent);
  575. secondItemGameObject.transform.SetParent(oldSlot.transform);
  576. secondItemRectTransform.localPosition = Vector3.zero;
  577. firstItemRectTransform.localPosition = Vector3.zero;
  578. }
  579. }
  580.  
  581. }
  582. }
  583. else
  584. {
  585. if (newSlot.tag != "Slot" && newSlot.tag != "ItemIcon")
  586. {
  587. firstItemGameObject.transform.SetParent(oldSlot.transform);
  588. firstItemRectTransform.localPosition = Vector3.zero;
  589. }
  590. else
  591. {
  592. firstItemGameObject.transform.SetParent(newSlot.transform);
  593. firstItemRectTransform.localPosition = Vector3.zero;
  594.  
  595. if (newSlot.transform.parent.parent.GetComponent<EquipmentSystem>() == null && oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  596. oldSlot.transform.parent.parent.GetComponent<Inventory>().UnEquipItem1(firstItem);
  597. }
  598. }
  599.  
  600. }
  601.  
  602.  
  603. }
  604.  
  605. else
  606. {
  607. GameObject dropItem = (GameObject)Instantiate(GetComponent<ItemOnObject>().item.itemModel);
  608. dropItem.AddComponent<PickUpItem>();
  609. dropItem.GetComponent<PickUpItem>().item = this.gameObject.GetComponent<ItemOnObject>().item;
  610. dropItem.transform.localPosition = GameObject.FindGameObjectWithTag("Player").transform.localPosition + new Vector3(0,1,-1);
  611. inventory.OnUpdateItemList();
  612. if (oldSlot.transform.parent.parent.GetComponent<EquipmentSystem>() != null)
  613. inventory.GetComponent<Inventory>().UnEquipItem1(dropItem.GetComponent<PickUpItem>().item);
  614. Destroy(this.gameObject);
  615.  
  616. }
  617. }
  618. inventory.OnUpdateItemList();
  619. }
  620.  
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement