private void ChangingPositions(GameObject swapObject, SwapCell swapCell) { if (swapCell.GetIsPositionTrue().Equals(true)) { SwapPuzzle.shiftedCellCount++; swapCell.SetIsPositionTrue(false); if (isPositionTrue.Equals(true)) { SwapPuzzle.shiftedCellCount++; SetIsPositionTrue(false); } } else { if (isPositionTrue.Equals(true)) { SwapPuzzle.shiftedCellCount++; SetIsPositionTrue(false); } else { if (truePosition.Equals(swapObject.GetComponent <RectTransform>().anchoredPosition)) { SwapPuzzle.shiftedCellCount--; SetIsPositionTrue(true); if (swapCell.GetTruePosition().Equals(gameObject.GetComponent <RectTransform>().anchoredPosition)) { SwapPuzzle.shiftedCellCount--; swapCell.SetIsPositionTrue(true); } } else { if (swapCell.GetTruePosition().Equals(gameObject.GetComponent <RectTransform>().anchoredPosition)) { SwapPuzzle.shiftedCellCount--; swapCell.SetIsPositionTrue(true); } } } } Vector2 tempPosition = gameObject.GetComponent <RectTransform>().anchoredPosition; gameObject.GetComponent <RectTransform>().anchoredPosition = swapObject.GetComponent <RectTransform>().anchoredPosition; swapObject.GetComponent <RectTransform>().anchoredPosition = tempPosition; }
private void BuilCell(Vector2 randomCellPosition, Quaternion rotation, int index, float i, float j, Vector2 pivot, Rect rec, int maxGlowWidth) { //Cordinates for cell float xCordinate = (pixel * i) + (pixel / 2f); float yCordinate = (pixel * j) + (pixel / 2f); Debug.Log("index: " + index.ToString()); Debug.Log("truePosition: " + new Vector2(xCordinate, yCordinate).ToString()); Debug.Log("randomPosition: " + randomCellPosition.ToString()); Debug.Log("rotation: " + rotation.ToString()); //Creating cells GameObject cell = new GameObject(); //Bounding with script cell.AddComponent <TouchSwapCell>(); //--------------------- RectTransform rectTransform = cell.AddComponent <RectTransform>() as RectTransform; BoxCollider2D box2D = cell.AddComponent <BoxCollider2D>() as BoxCollider2D; //Creating with z: +90 position, because default z position is -180 and we want it to be 0 if (isRotateEnabled) { cell.transform.SetPositionAndRotation(randomCellPosition, rotation); } else { cell.transform.SetPositionAndRotation(randomCellPosition, Quaternion.Euler(0, 0, 0)); } //Naming Cell and Setting his parent cell.name = "cell_" + xCordinate.ToString() + "x" + yCordinate.ToString(); cellsParent.name = "Parent_Of_Cells\\" + pieceCount.ToString() + "-Children"; cell.transform.SetParent(cellsParent.transform); //Scalling rectTransform.sizeDelta = new Vector2(pixel, pixel); box2D.size = new Vector2(pixel, pixel); cell.transform.localScale = new Vector3(1, 1, 1); //Positioning; anchorMin and anchorMax are for starting position bottom left corner of parent. rectTransform.anchorMin = new Vector2(0f, 0f); rectTransform.anchorMax = new Vector2(0f, 0f); rectTransform.anchoredPosition = new Vector3(x: randomCellPosition.x, y: randomCellPosition.y); //Creating Sprite Texture--------------------------------------------------------------------- Texture2D spriteTexture = new Texture2D((int)(pixel * 100f), (int)(pixel * 100f)); var pixels = painting.texture.GetPixels((int)((pixel * 100) * i), (int)((pixel * 100) * j), (int)(pixel * 100f), (int)(pixel * 100f)); spriteTexture.SetPixels(pixels); spriteTexture.Apply(); //------------------------------------------------------------------------------------ //Setting sprite to Cell SpriteRenderer spriteRenderer = cell.AddComponent <SpriteRenderer>() as SpriteRenderer; spriteRenderer.sprite = Sprite.Create(spriteTexture, rec, pivot); spriteRenderer.sortingLayerName = "Cells"; spriteRenderer.material.shader = glowShader; //GlowEffect---------------- SpriteGlowEffect glowEffect = cell.AddComponent <SpriteGlowEffect>(); glowEffect.OutlineWidth = 0; glowEffect.AlphaThreshold = 0.01f; //------------------------- //-----SwapCell Values--------- SwapCell swapCell = cell.AddComponent <SwapCell>(); swapCell.SetTruePosition(new Vector2(xCordinate, yCordinate)); swapCell.SetIsPositionTrue(swapCell.GetTruePosition().Equals(randomCellPosition)); swapCell.SetMaxGlowWidth(maxGlowWidth); swapCell.SetForward(null); swapCell.SetBackward(null); swapCell.SetLeft(null); swapCell.SetRight(null); //----------------------- //Finally, adding our cell to the list. cells.SetValue(cell, index); }