public void BeginObjectCreation(GameObject decObjPrefab) { creationModeOn = true; GameObject newDecObject = Instantiate <GameObject>(decObjPrefab); currentObject = newDecObject.GetComponent <DecorativeObject>(); currentCell = null; //newDecObject.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition); //MoveCamera cameraController = Camera.main.GetComponent<MoveCamera>(); //if (cameraController != null) cameraController.blockMovement = true; }
// Use this for initialization void Start() { gameObject.SetActive(false); //Создаем кнопки по количеству префабов for (int i = 0; i < grid.decorObjectPrefabs.Length; i++) { DecorativeObject decObj = grid.decorObjectPrefabs[i].GetComponent <DecorativeObject>(); GameObject newButton = Instantiate(buttonPrefab); newButton.transform.SetParent(transform.GetChild(0)); newButton.transform.GetChild(0).GetComponent <Image>().sprite = decObj.objectSprite; newButton.transform.GetChild(1).GetComponent <Text>().text = decObj.decObjectName; newButton.transform.GetChild(2).GetComponent <Text>().text = decObj.objectSize + "x" + decObj.objectSize; newButton.GetComponent <UI_MagButton>().decObjPrefab = grid.decorObjectPrefabs[i]; } }
// Update is called once per frame void Update() { //if (creationModeOn && Input.touchCount > 0) if (creationModeOn) { RaycastHit hit; int layerMask = 1 << 9; layerMask = ~layerMask; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, layerMask)) { //if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return;//Исправление пробивания рейкаста через UI //if (Input.GetTouch(0).phase == TouchPhase.Ended) return; //Расчет координат для позиционирования текущего располагаемого объекта currentCell = hit.collider.GetComponent <GridCell>(); if (currentCell != null) { currentObject.transform.position = grid.GetMiddlePos(currentCell.i, currentCell.j, currentObject.objectSize); if (currentCell != prevCell && prevCell != null) { grid.ReDrawGridRect(prevCell.i, prevCell.j, currentObject.objectSize); //Перерисовать участо сетки при переходе на новую ячейку } grid.DrawDecObjectBordersOnGrid(currentCell.i, currentCell.j, currentObject.objectSize); //Рисуем участок сетки на которой расположен наш объект с учетом возможно его размещения prevCell = currentCell; ////Установка нового объекта if (Input.GetMouseButtonDown(0)) { if (grid.HaveAnEmptyCells(currentCell.i, currentCell.j, currentObject.objectSize)) { grid.SetCellsFlag(currentCell.i, currentCell.j, currentObject.objectSize, false); grid.DrawAllGrid();//Перерисовать с учетом нового объекта creationModeOn = false; currentObject = null; // MoveCamera cameraController = Camera.main.GetComponent <MoveCamera>(); if (cameraController != null) { cameraController.blockMovement = false; } } } } } } }
public void SetObjectButtonClick() { if (creationModeOn && currentCell != null) { if (grid.HaveAnEmptyCells(currentCell.i, currentCell.j, currentObject.objectSize)) { grid.SetCellsFlag(currentCell.i, currentCell.j, currentObject.objectSize, false); grid.DrawAllGrid();//Перерисовать с учетом нового объекта creationModeOn = false; currentObject = null; MoveCamera cameraController = Camera.main.GetComponent <MoveCamera>(); if (cameraController != null) { cameraController.blockMovement = false; } } } }
//Устанавливаем объекты по краям карты void SetDecorativeObjects() { GameObject newDecObject = null; for (int i = 0; i < iCount; i++) { for (int j = 0; j < jCount; j++) { if (i == 0 || i == iCount - 1 || j == 0 || j == jCount - 1) { //Находимся у края границы int prefabNum = Random.Range(0, decorObjectPrefabs.Length); if (newDecObject == null) { newDecObject = Instantiate <GameObject>(decorObjectPrefabs[prefabNum]); } DecorativeObject dObj = newDecObject.GetComponent <DecorativeObject>(); if (HaveAnEmptyCells(i, j, dObj.objectSize)) { newDecObject.transform.position = GetMiddlePos(i, j, dObj.objectSize); SetCellsFlag(i, j, dObj.objectSize, false);//Устанавливаем флаг (Ячейка занята) newDecObject = null; } //Debug.Log("Находимся в ячейке i = " + i + " j = " + j); } else { continue; } } } if (newDecObject != null) { Destroy(newDecObject); } }
// Use this for initialization void Start() { currentObject = null; prevCell = null; currentCell = null; }