简体   繁体   English

Unity 2D,游戏对象到中心

[英]Unity 2D, GameObject to the center

I need Help, I'm doing a smaller puzzle game but I have trouble getting the playing field to the center.我需要帮助,我正在做一个较小的益智游戏,但我无法将比赛场地带到中心。 Playing field is still on the bottom/left I tried to fix it using the forum but nothing happened.比赛场地仍在底部/左侧我试图使用论坛修复它,但没有任何反应。 Thanks for your help.谢谢你的帮助。

Problem问题

GameBoard.cs游戏板.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 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);
}

} }

Hey guys, I need Help, I'm doing a smaller puzzle game but I have trouble getting the playing field to the center.嘿伙计们,我需要帮助,我正在做一个较小的益智游戏,但我无法将比赛场地带到中心。 Playing field is still on the bottom/left I tried to fix it using the forum but nothing happened.比赛场地仍在底部/左侧我试图使用论坛修复它,但没有任何反应。 Thanks for your help.谢谢你的帮助。

As i gather from the picture your gameboard gets drawn inside a Panel ui control.正如我从图片中收集的那样,您的游戏板被绘制在Panel ui 控件中。 You could set the position of this panel to the centre, so that all its children will be centred alongside it:您可以将此面板的 position 设置为中心,这样它的所有子项都将集中在它旁边:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM