Код: Выделить всё
originPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Ниже я присылаю полный код этого файла:
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectScript : MonoBehaviour
{
private Vector3 originPoint, endPoint;
public Sprite selectAreaSprite;
private GameObject area;
private BoxCollider2D areaCollider;
public List SelectedGameObjects;
[SerializeField] private GameObject menu;
private bool wasClicked;
public Vector3 connection;
private void Update()
{
if(Input.GetMouseButton(0) == false)
{
originPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.GetMouseButtonDown(0))
{
CreateSelectArea();
}
if (Input.GetMouseButton(0))
{
endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
area.transform.position = (originPoint + endPoint) / 2;
area.transform.localScale = new Vector3(originPoint.x - endPoint.x, originPoint.y - endPoint.y, 0);
}
else if (Input.GetMouseButtonUp(0))
{
GameObject areaToDestroy = GameObject.Find("SelectArea");
if(areaToDestroy != null)
{
Destroy(areaToDestroy);
}
}
if (Input.GetMouseButtonDown(1))
{
menu.transform.position = Input.mousePosition + new Vector3(0, 0, 10);
menu.SetActive(true);
}
}
private void CreateSelectArea()
{
area = new GameObject();
area.name = "SelectArea";
area.transform.position = originPoint - endPoint;
var spriteRenderer = area.AddComponent();
spriteRenderer.sprite = selectAreaSprite;
spriteRenderer.color = new Color(0, 0.8f, 0, 1f);
spriteRenderer.sortingOrder = -2;
areaCollider = area.AddComponent();
areaCollider.isTrigger = true;
}
public void Move()
{
wasClicked = false;
StartCoroutine("waitForMouseClick");
}
private IEnumerator waitForMouseClick()
{
yield return new WaitForEndOfFrame();
wasClicked = Input.GetMouseButton(0);
if (!wasClicked)
{
StartCoroutine("waitForMouseClick");
}
else
{
foreach(GameObject g in SelectedGameObjects)
{
g.GetComponent().isSelected = false;
g.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10));
if(g.GetComponent() != null)
{
g.GetComponent().SetPosition(0, transform.position);
}
}
Unselect();
}
}
public void AddConnection()
{
StartCoroutine("waitForConnectionChange");
}
private IEnumerator waitForConnectionChange()
{
yield return new WaitForEndOfFrame();
foreach (GameObject g in SelectedGameObjects)
{
g.GetComponent().AddConnection(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
if (!Input.GetMouseButton(0))
{
StartCoroutine("waitForConnectionChange");
}
else if(Input.GetMouseButton(0))
{
var mousePos = new GameObject();
mousePos.name = "MousePosForConnection";
mousePos.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.AddComponent();
mousePos.GetComponent().radius = 0.1f;
mousePos.GetComponent().isTrigger = true;
Unselect();
}
else if(connection != null)
{
foreach (GameObject g in SelectedGameObjects)
{
g.GetComponent().AddConnection(connection);
}
connection = new Vector3(0,0,0);
Unselect();
}
}
public void DeleteConnection()
{
foreach(GameObject g in SelectedGameObjects)
{
g.GetComponent().DeleteConnection();
}
}
public void Unselect()
{
foreach(GameObject g in SelectedGameObjects)
{
g.GetComponent().isSelected = false;
}
SelectedGameObjects.Clear();
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... tinput-mou