Advertisement
AndrewRosyaev

Inventory1

Jun 17th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5.  
  6. public class GridE : MonoBehaviour
  7. {
  8.     public Transform Element;
  9.     public Transform InstEl;
  10.     public Vector2 ElementScale = new Vector2(30,30);
  11.     public List<Transform> AllCells = new List<Transform> ();
  12.     Vector3 startpoint;
  13.     public Transform CellsParent;
  14.     public Transform GridHorizontalLine;
  15.     public Transform GridVerticalLine;
  16.     public Vector2 GridScale = new Vector2(11,4);
  17.     public List<Transform> AllItems = new List<Transform> ();
  18.     public Transform SelectedItem;
  19.     public Transform SelectedCell;
  20.  
  21.     public List<Transform> HowerCells = new List<Transform> ();
  22.  
  23.     public Transform Item1;
  24.     public Transform Item2;
  25.     public Transform Item3;
  26.     public Transform Item4;
  27.  
  28.  
  29.     public void InstLines()
  30.     {
  31.         for (int y = 0; y < 4; y++)
  32.         {
  33.             Transform HorLine = Instantiate (GridHorizontalLine, startpoint + new Vector3 (0, -y * ElementScale.y, 0), Quaternion.identity) as Transform;
  34.             HorLine.SetParent (transform);
  35.         }
  36.     }
  37.  
  38.  
  39.     public void Awake ()
  40.     {
  41.         startpoint = new Vector3 (Screen.width-400, 200, 0);
  42.         for (int x = 0; x < GridScale.x; x++)
  43.         {
  44.             Transform CellX = Instantiate (Element, startpoint+new Vector3(x*ElementScale.x,0,0), Quaternion.identity) as Transform;
  45.             CellX.SetParent (CellsParent);
  46.             AllCells.Add (CellX);
  47.             CellX.GetComponent<RectTransform> ().sizeDelta = ElementScale;
  48.             for (int y = 1; y < GridScale.y; y++)
  49.             {
  50.                 Transform CellY = Instantiate (Element, startpoint+new Vector3(x*ElementScale.x,-y*ElementScale.y,0), Quaternion.identity) as Transform;
  51.                 CellY.SetParent (CellsParent);
  52.                 AllCells.Add (CellY);
  53.                 CellY.GetComponent<RectTransform> ().sizeDelta = ElementScale;
  54.             }
  55.         }
  56.  
  57.         for (int y = 1; y < GridScale.y; y++)
  58.         {
  59.             Transform HorLine = Instantiate (GridHorizontalLine, startpoint + new Vector3 (0, -y * ElementScale.y, 0) + new Vector3((GridScale.x-1)*ElementScale.x/2,ElementScale.y/2,0), Quaternion.identity) as Transform;
  60.             HorLine.GetComponent<RectTransform> ().sizeDelta = new Vector2(GridScale.x*ElementScale.x,1);
  61.             HorLine.SetParent (CellsParent);
  62.         }
  63.  
  64.         for (int x = 1; x < GridScale.x; x++)
  65.         {
  66.             Transform VerLine = Instantiate (GridVerticalLine, startpoint+new Vector3(x*ElementScale.x,0)+new Vector3(-ElementScale.x/2,(-GridScale.y+1)*ElementScale.y/2,0),Quaternion.identity) as Transform;
  67.             VerLine.GetComponent<RectTransform> ().sizeDelta = new Vector2(1,GridScale.y*(ElementScale.y+1));
  68.             VerLine.SetParent (CellsParent);
  69.         }
  70.  
  71.  
  72.  
  73.         Transform item = Instantiate (Item1, startpoint+new Vector3(15,-15,0),Quaternion.identity) as Transform;
  74.         item.SetParent (transform);
  75.         AllItems.Add (item);
  76.  
  77.         Transform item2 = Instantiate (Item2, startpoint+new Vector3(ElementScale.x*3,-15,0),Quaternion.identity) as Transform;
  78.         item2.SetParent (transform);
  79.         AllItems.Add (item2);
  80.  
  81.         Transform item3 = Instantiate (Item3, startpoint+new Vector3(ElementScale.x*5,0,0),Quaternion.identity) as Transform;
  82.         item3.SetParent (transform);
  83.         AllItems.Add (item3);
  84.  
  85.         Transform item4 = Instantiate (Item4, startpoint+new Vector3(ElementScale.x*7-15,-30,0),Quaternion.identity) as Transform;
  86.         item4.SetParent (transform);
  87.         AllItems.Add (item4);
  88.     }
  89.  
  90.     public void DestroyGrid()
  91.     {
  92.         foreach (Transform tr in AllCells)
  93.         {
  94.             DestroyImmediate (tr.gameObject);
  95.         }
  96.         AllCells.Clear ();
  97.     }
  98.  
  99.  
  100.     void Update()
  101.     {
  102.         if (Input.GetMouseButtonDown (0))
  103.         {
  104.             Vector2 mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
  105.             foreach (Transform tr in AllItems)
  106.             {
  107.                 if (RectTransformUtility.RectangleContainsScreenPoint (tr.GetComponent<RectTransform> (), Input.mousePosition))
  108.                 {
  109.                     SelectedItem = tr;
  110.                 }
  111.             }
  112.         }
  113.         if (SelectedItem != null)
  114.         {
  115.             SelectedItem.position = Input.mousePosition;
  116.  
  117.             Vector3 MousePos = Input.mousePosition;
  118.             MousePos = new Vector3 (Mathf.Clamp(MousePos.x,
  119.                 startpoint.x+(SelectedItem.GetComponent<InventoryItem> ().Scale.x-1)*ElementScale.x/2,
  120.                 startpoint.x+(int)(GridScale.x-1)*ElementScale.x-SelectedItem.GetComponent<InventoryItem> ().Scale.x*ElementScale.x/2),
  121.                 Mathf.Clamp(MousePos.y,
  122.                     startpoint.y-(int)((GridScale.y-1)*ElementScale.y)+SelectedItem.GetComponent<InventoryItem> ().Scale.y*ElementScale.y/2,
  123.                     startpoint.y-(SelectedItem.GetComponent<InventoryItem> ().Scale.y-1)*ElementScale.y/2),
  124.                 MousePos.z);
  125.  
  126.  
  127.  
  128.             Vector3 LeftUpPoint = MousePos + new Vector3 (-SelectedItem.GetComponent<InventoryItem> ().Scale.x*ElementScale.x / 2+ElementScale.x / 2, SelectedItem.GetComponent<InventoryItem> ().Scale.y*ElementScale.y / 2-ElementScale.y / 2, 0);
  129.             foreach (Transform tr in AllCells)
  130.             {
  131.                 if (RectTransformUtility.RectangleContainsScreenPoint (tr.GetComponent<RectTransform> (), LeftUpPoint))
  132.                 {
  133.                     SelectedCell = tr;
  134.                 }
  135.             }
  136.  
  137.             if (SelectedCell != null)
  138.             {
  139.                 HowerCells.Add (SelectedCell);
  140.                 foreach (Transform tr in AllCells)
  141.                 {
  142.                     for (int x = 0; x < SelectedItem.GetComponent<InventoryItem> ().Scale.x; x++)
  143.                     {
  144.                         for (int y = 0; y < SelectedItem.GetComponent<InventoryItem> ().Scale.y; y++)
  145.                         {
  146.                             if (tr.localPosition == SelectedCell.localPosition + new Vector3(ElementScale.x*x,-ElementScale.y*y,0))
  147.                             {
  148.                                 HowerCells.Add (tr);
  149.                             }
  150.                         }
  151.                     }
  152.  
  153.                     if (HowerCells.Contains (tr))
  154.                     {
  155.                         tr.GetComponent<Image> ().color = new Color (0, 1, 0);
  156.                     } else
  157.                     {
  158.                         tr.GetComponent<Image> ().color = new Color (0.3f, 0.3f, 0.3f);
  159.                     }
  160.                 }
  161.  
  162.                 HowerCells.Clear ();
  163.             }
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement