using System.Collections; using System.Collections.Generic; using UnityEngine; public class InventoryPanel : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Drag2D : MonoBehaviour { [SerializeField] private bool isSelected; private void onm ouseDrag() { Vector2 cursorPos = FindObjectOfType<Camera>().ScreenToWorldPoint(Input.mousePosition); Vector3 fixedPos = FindObjectOfType<RunGridManager>().grid.SnapToGrid(cursorPos); transform.position = fixedPos+new Vector3(0.5f,0.5f,0); if (Input.GetKeyDown(KeyCode.Space)) { transform.rotation = Quaternion.Euler(0, 0, transform.eulerAngles.z - 90); } } public void Update() { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NAND : MonoBehaviour { }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RunGridManager : MonoBehaviour { public GridManager grid; // Start is called before the first frame update void Start() { //transform.position = FindObjectOfType<Camera>().transform.position; transform.position = FindObjectOfType<Camera>().ScreenToWorldPoint(new Vector3(0, 0, 0)); grid = new GridManager(100, 100, 1, transform.position); grid.DrawLine(); } // Update is called once per frame void Update() { //Debug.Log(Input.mousePosition); //Debug.Log(FindObjectOfType<Camera>().ScreenToWorldPoint(Input.mousePosition)); Debug.Log(grid.GetGridPosition(FindObjectOfType<Camera>().ScreenToWorldPoint(Input.mousePosition))); grid.DrawLine(); } }
using System; using System.Collections; using System.Collections.Generic; using UnityEditor.Tilemaps; using UnityEngine; public class GridManager { private Vector3 WorldPositionOfGridOrigin; private int gridWidth; private int gridHeight; private float cellSize; private enum Element { Wire,InputPine,OutputPine,Processer}; private Transform[,] gridCells; public GridManager(int width,int height,float cellSize,Vector3 position) { gridWidth = width; gridHeight = height; this.cellSize = cellSize; WorldPositionOfGridOrigin = position; } public void GenerateGrid() { } public Vector3 GetWorldPosition(Vector2Int gridPosition) { return new Vector3(gridPosition.x * cellSize+WorldPositionOfGridOrigin.x, gridPosition.y * cellSize+WorldPositionOfGridOrigin.y,0); } public Vector2Int GetGridPosition(Vector3 worldPosition) { int x = Mathf.FloorToInt((worldPosition.x-WorldPositionOfGridOrigin.x)/cellSize); int y = Mathf.FloorToInt((worldPosition.y-WorldPositionOfGridOrigin.y) / cellSize); return new Vector2Int(x, y); } public bool IsWithinGrid(Vector2Int gridPosition) { return gridPosition.x >= 0 && gridPosition.y >= 0 && gridPosition.x < gridWidth && gridPosition.y < gridHeight; } public bool IsWithinGrid(Vector3 worldPosition) { Vector2Int gridPosition = GetGridPosition(worldPosition); return IsWithinGrid(gridPosition); } public Vector3 SnapToGrid(Vector3 worldPosition) { return GetWorldPosition(GetGridPosition(worldPosition)); } public void DrawLine() { for(int i=0;i<gridWidth; i++) { for(int j = 0; j < gridHeight; j++) { Vector3 worldPosition = GetWorldPosition(new Vector2Int(i, j)); Debug.DrawLine(worldPosition, new Vector3(worldPosition.x+cellSize,worldPosition.y,0)); Debug.DrawLine(worldPosition, new Vector3(worldPosition.x, worldPosition.y + cellSize,0)); } } } }
标签:1.0,游戏,代码,Vector3,System,gridPosition,Collections,using,public From: https://www.cnblogs.com/oucrjgc/p/18509215