Unity 2D,GameObject居中

时间:2020-05-03 20:22:42

标签: c# unity3d

我需要帮助,我正在做一个较小的益智游戏,但是我很难使比赛场地居中。 比赛场地仍在底部/左侧 我尝试使用论坛进行修复,但没有任何反应。 感谢您的帮助。

Problem

GameBoard.cs

using UnityEngine;
using System.Collections;

public class GameBoard : MonoBehaviour {

public int m_size;
public GameObject m_puzzlePiece;
public GameObject buttonNext;

private PuzzleSection[,] m_puzzle;
private PuzzleSection m_puzzleSelection;

public int m_randomPasses = 12;



void Start()
{
    GameObject temp;
    m_puzzle = new PuzzleSection[m_size, m_size];


    for (int i=0; i< m_size; i++)
    {
        for (int j=0; j< m_size; j++)
        {
            temp = (GameObject)Instantiate(m_puzzlePiece, 
                             new Vector2(i*700/m_size, j*700/m_size), Quaternion.identity);
            temp.transform.SetParent(transform);
            m_puzzle[i, j] = (PuzzleSection)temp.GetComponent<PuzzleSection>();
            m_puzzle[i, j].CreatePuzzlePiece(m_size);
        }
    }

    SetupBoard();

    RandomizePlacement();


}


void RandomizePlacement()
{
    VectorInt2[] puzzleLocation = new VectorInt2[2];
    Vector2[] puzzleOffset = new Vector2[2];
    do
    {
        for (int i = 0; i < m_randomPasses; i++)
        {
            puzzleLocation[0].x = Random.Range(0, m_size);
            puzzleLocation[0].y = Random.Range(0, m_size);
            puzzleLocation[1].x = Random.Range(0, m_size);
            puzzleLocation[1].y = Random.Range(0, m_size);

            puzzleOffset[0] = m_puzzle[puzzleLocation[0].x, puzzleLocation[0].y].GetImageOffset();
            puzzleOffset[1] = m_puzzle[puzzleLocation[1].x, puzzleLocation[1].y].GetImageOffset();

            m_puzzle[puzzleLocation[0].x, puzzleLocation[0].y].AssignImage(puzzleOffset[1]);
            m_puzzle[puzzleLocation[1].x, puzzleLocation[1].y].AssignImage(puzzleOffset[0]);
        }

    } while (CheckBoard() == true);


}

void SetupBoard()
{
    Vector2 offset;
    Vector2 m_scale = new Vector2(1f / m_size, 1f / m_size);
    for (int i=0; i< m_size; i++)
    {
        for (int j=0; j< m_size; j++)
        {
            offset = new Vector2(i * (1f / m_size), j * (1f / m_size));
            m_puzzle[i, j].AssignImage(m_scale, offset);
        }
    }

}

public PuzzleSection GetSelection()
{

    return m_puzzleSelection;

}

public void SetSelection(PuzzleSection selection)
{
    m_puzzleSelection = selection;
}

public bool CheckBoard()
{
    for (int i=0; i<m_size; i++)
    {
        for(int j=0; j< m_size; j++)
        {
            if (m_puzzle[i, j].CheckGoodPlacement() == false)
                return false;
        }

    }

    return true;
}

public void Win()
{
    buttonNext.SetActive(true);
}

}

PuzzleSection.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PuzzleSection : MonoBehaviour {

Vector2 m_goodOffset;
Vector2 m_offset;
Vector2 m_scale;
GameBoard m_gameBoard;


void Start()
{
    m_gameBoard = (GameBoard)GameObject.FindObjectOfType<Canvas>()
                                        .GetComponentInChildren<GameBoard>();


}



public void CreatePuzzlePiece(int size)
{
    transform.localScale = new Vector3(1.0f * transform.localScale.x / size,
                            1.0f * transform.localScale.z / size, 1);
}

public void AssignImage(Vector2 scale, Vector2 offset)
{
    m_goodOffset = offset;
    m_scale = scale;
    AssignImage(offset);

}

public void AssignImage(Vector2 offset)
{
    m_offset = offset;
    GetComponent<RawImage>().uvRect = new Rect(offset.x, offset.y, m_scale.x, m_scale.y);


}

public void OnClick()
{
    PuzzleSection previousSelection = m_gameBoard.GetSelection();
     if (previousSelection != null)
    {
        previousSelection.GetComponent<RawImage>().color = Color.white;
        Vector2 tempOffset = previousSelection.GetImageOffset();
        previousSelection.AssignImage(m_offset);
        AssignImage(tempOffset);
        m_gameBoard.SetSelection(null);
        if (m_gameBoard.CheckBoard() == true)
            m_gameBoard.Win();
    } else
    {
        GetComponent<RawImage>().color = Color.gray;
        m_gameBoard.SetSelection(this);

    }


}

public Vector2 GetImageOffset()
{
    return m_offset;

}

public bool CheckGoodPlacement()
{
    return (m_goodOffset == m_offset);
}

}

伙计们,我需要帮助,我正在做一个较小的益智游戏,但是我很难将比赛场地置于中心位置。 比赛场地仍在底部/左侧 我尝试使用论坛进行修复,但没有任何反应。 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

当我从图片中收集时,您的游戏板被绘制在Panel ui控件内。 您可以将该面板的位置设置为居中,以便其所有子代都在面板旁边居中:

MyPanel.transform.position = new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0);