Я хочу вставить gamobject в ячейку, и я не использую Canvas. Как написать сценарий для него - PullRequest
0 голосов
/ 28 апреля 2020

The dice should set it its position to center of the cell.

using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;

public class Drag : MonoBehaviour
{
    private Vector3 screenPoint;
    private Vector3 offset;
    public bool canMove;


private void Start()
{
    canMove=true;
}

void OnMouseDown(){
    Debug.Log("can move this tile");
    screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new 
    Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

}

void OnMouseDrag()
{

    if(canMove)
    {

        Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 
        screenPoint.z);
        Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
        transform.position = cursorPosition;}
    }
private void OnMouseUp() {

    canMove=false;
  }

}

Поскольку я начинающий, пожалуйста, помогите мне в этом отношении, это очень мне поможет. Код содержит базовый c скрипт перетаскивания.

Вот мой скрипт перетаскивания и как его изменить в соответствии с требованиями. и скрипт игровой доски:

private void SetupGrid()
{
    float height=Camera.main.orthographicSize*2f;
    float width=Camera.main.aspect*height;

    Debug.Log("height:"+height);
    Debug.Log("width:"+width);

    float possibleHeight=0.5264f*height;
    float possibleWidth=0.8f*width;

    Debug.Log("possibleHeight:"+possibleHeight);
    Debug.Log("possibleWidth:"+possibleWidth);

    float eachcellWidth=possibleWidth/mWidth;
    float eachcellHeight=possibleHeight/mHeight;

    eachcellHeight=eachcellWidth;

    Debug.Log("eachcellWidth:"+eachcellWidth);
    Debug.Log("eachcellHeight:"+eachcellHeight);



    float cellCenterX=eachcellWidth/2;
    float cellCenterY=eachcellHeight/2;

    Debug.Log("cellCenterX:"+cellCenterX);
    Debug.Log("cellCenterY:"+cellCenterY);

    float offSetBetweenEachCell=eachcellWidth*0.10f;
   // offSetBetweenEachCell=0.0f;                                                

    float startXpos=-(possibleWidth/2)+cellCenterX;
    float startYpos=-(possibleHeight/2)+cellCenterY;

    Debug.Log("start X position value:"+startXpos);
    Debug.Log("start Y position value:"+startYpos);

    SpriteRenderer spriteRenderer=mTilePrefab.GetComponent<SpriteRenderer>();

    float actualSpriteWidth=spriteRenderer.sprite.bounds.size.x;
    float actualSpriteHeight=spriteRenderer.sprite.bounds.size.y;

    Debug.Log("Actual Sprite width:"+actualSpriteWidth);
    Debug.Log("Actual Sprite Height:"+actualSpriteHeight);

    float finalCellWidth=eachcellWidth-offSetBetweenEachCell;
    float finalCellHeight=eachcellHeight-offSetBetweenEachCell;

    Vector3 eachCellScaleUnit=new Vector3(finalCellWidth/actualSpriteWidth,finalCellHeight/actualSpriteHeight);

    Vector3 referenceVector=Vector3.one;
    GameObject cell=null;
    for(int i=0;i<mWidth;i++)
    {
        for(int j=0;j<mHeight;j++)
        {
            float posX=startXpos+(j*eachcellWidth);
            float posY=startYpos+(i*eachcellHeight);

            referenceVector.x=posX;
            referenceVector.y=posY;

            cell=Instantiate(mTilePrefab,referenceVector,Quaternion.identity);
            cell.transform.localScale=eachCellScaleUnit;
        }
    }
}

эта функция создает игровую доску, а игровой объект - это игральные кости, как показано на рисунке, которые каждый раз будут вставлены в слот

...