Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- public class GridE : MonoBehaviour
- {
- public Transform Element;
- public Transform InstEl;
- public Vector2 ElementScale = new Vector2(30,30);
- public List<Transform> AllCells = new List<Transform> ();
- Vector3 startpoint;
- public Transform CellsParent;
- public Transform GridHorizontalLine;
- public Transform GridVerticalLine;
- public Vector2 GridScale = new Vector2(11,4);
- public List<Transform> AllItems = new List<Transform> ();
- public Transform SelectedItem;
- public Transform SelectedCell;
- public List<Transform> HowerCells = new List<Transform> ();
- public Transform Item1;
- public Transform Item2;
- public Transform Item3;
- public Transform Item4;
- public void InstLines()
- {
- for (int y = 0; y < 4; y++)
- {
- Transform HorLine = Instantiate (GridHorizontalLine, startpoint + new Vector3 (0, -y * ElementScale.y, 0), Quaternion.identity) as Transform;
- HorLine.SetParent (transform);
- }
- }
- public void Awake ()
- {
- startpoint = new Vector3 (Screen.width-400, 200, 0);
- for (int x = 0; x < GridScale.x; x++)
- {
- Transform CellX = Instantiate (Element, startpoint+new Vector3(x*ElementScale.x,0,0), Quaternion.identity) as Transform;
- CellX.SetParent (CellsParent);
- AllCells.Add (CellX);
- CellX.GetComponent<RectTransform> ().sizeDelta = ElementScale;
- for (int y = 1; y < GridScale.y; y++)
- {
- Transform CellY = Instantiate (Element, startpoint+new Vector3(x*ElementScale.x,-y*ElementScale.y,0), Quaternion.identity) as Transform;
- CellY.SetParent (CellsParent);
- AllCells.Add (CellY);
- CellY.GetComponent<RectTransform> ().sizeDelta = ElementScale;
- }
- }
- for (int y = 1; y < GridScale.y; y++)
- {
- 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;
- HorLine.GetComponent<RectTransform> ().sizeDelta = new Vector2(GridScale.x*ElementScale.x,1);
- HorLine.SetParent (CellsParent);
- }
- for (int x = 1; x < GridScale.x; x++)
- {
- 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;
- VerLine.GetComponent<RectTransform> ().sizeDelta = new Vector2(1,GridScale.y*(ElementScale.y+1));
- VerLine.SetParent (CellsParent);
- }
- Transform item = Instantiate (Item1, startpoint+new Vector3(15,-15,0),Quaternion.identity) as Transform;
- item.SetParent (transform);
- AllItems.Add (item);
- Transform item2 = Instantiate (Item2, startpoint+new Vector3(ElementScale.x*3,-15,0),Quaternion.identity) as Transform;
- item2.SetParent (transform);
- AllItems.Add (item2);
- Transform item3 = Instantiate (Item3, startpoint+new Vector3(ElementScale.x*5,0,0),Quaternion.identity) as Transform;
- item3.SetParent (transform);
- AllItems.Add (item3);
- Transform item4 = Instantiate (Item4, startpoint+new Vector3(ElementScale.x*7-15,-30,0),Quaternion.identity) as Transform;
- item4.SetParent (transform);
- AllItems.Add (item4);
- }
- public void DestroyGrid()
- {
- foreach (Transform tr in AllCells)
- {
- DestroyImmediate (tr.gameObject);
- }
- AllCells.Clear ();
- }
- void Update()
- {
- if (Input.GetMouseButtonDown (0))
- {
- Vector2 mouse = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
- foreach (Transform tr in AllItems)
- {
- if (RectTransformUtility.RectangleContainsScreenPoint (tr.GetComponent<RectTransform> (), Input.mousePosition))
- {
- SelectedItem = tr;
- }
- }
- }
- if (SelectedItem != null)
- {
- SelectedItem.position = Input.mousePosition;
- Vector3 MousePos = Input.mousePosition;
- MousePos = new Vector3 (Mathf.Clamp(MousePos.x,
- startpoint.x+(SelectedItem.GetComponent<InventoryItem> ().Scale.x-1)*ElementScale.x/2,
- startpoint.x+(int)(GridScale.x-1)*ElementScale.x-SelectedItem.GetComponent<InventoryItem> ().Scale.x*ElementScale.x/2),
- Mathf.Clamp(MousePos.y,
- startpoint.y-(int)((GridScale.y-1)*ElementScale.y)+SelectedItem.GetComponent<InventoryItem> ().Scale.y*ElementScale.y/2,
- startpoint.y-(SelectedItem.GetComponent<InventoryItem> ().Scale.y-1)*ElementScale.y/2),
- MousePos.z);
- 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);
- foreach (Transform tr in AllCells)
- {
- if (RectTransformUtility.RectangleContainsScreenPoint (tr.GetComponent<RectTransform> (), LeftUpPoint))
- {
- SelectedCell = tr;
- }
- }
- if (SelectedCell != null)
- {
- HowerCells.Add (SelectedCell);
- foreach (Transform tr in AllCells)
- {
- for (int x = 0; x < SelectedItem.GetComponent<InventoryItem> ().Scale.x; x++)
- {
- for (int y = 0; y < SelectedItem.GetComponent<InventoryItem> ().Scale.y; y++)
- {
- if (tr.localPosition == SelectedCell.localPosition + new Vector3(ElementScale.x*x,-ElementScale.y*y,0))
- {
- HowerCells.Add (tr);
- }
- }
- }
- if (HowerCells.Contains (tr))
- {
- tr.GetComponent<Image> ().color = new Color (0, 1, 0);
- } else
- {
- tr.GetComponent<Image> ().color = new Color (0.3f, 0.3f, 0.3f);
- }
- }
- HowerCells.Clear ();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement