Arrastre el objeto del inventario e instálelo en la ubicación especificada
1. Crear una lista deslizante vertical - barra de elementos
2. Arrastra el objeto desde la barra de inventario
Cree una carpeta de recursos y cree una carpeta de prefabricados en la carpeta de recursos
Coloque el objeto de instancia que se generará en la carpeta Prefabs
Añadir un botón y una imagen
Agregue el código debajo del botón, escriba el nombre de la instancia en str y seleccione la imagen adecuada para la imagen
el código se muestra a continuación
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
using UnityEngine. EventSystems ;
using UnityEngine. UI ;
public class DragSpawn : MonoBehaviour , IPointerDownHandler
{
public string str;
public Image image;
private GameObject objDragSpawning;
private bool isDragSpawning = false ;
private void Start ( )
{
image. enabled = false ;
}
void Update ( )
{
if ( isDragSpawning)
{
Ray ray = Camera. main. ScreenPointToRay ( Input. mousePosition) ;
objDragSpawning. transform. position = ray. GetPoint ( 10 ) ;
image. enabled = true ;
image. transform. position = Input. mousePosition;
objDragSpawning. SetActive ( false ) ;
if ( Input. GetMouseButtonUp ( 0 ) )
{
objDragSpawning. SetActive ( true ) ;
isDragSpawning = false ;
objDragSpawning = null ;
image. enabled = false ;
}
}
}
public void OnPointerDown ( PointerEventData eventData)
{
str = "Prefabs/" + str;
GameObject prefab = Resources. Load < GameObject> ( str) ;
if ( prefab != null )
{
objDragSpawning = Instantiate ( prefab) ;
isDragSpawning = true ;
}
}
}
3. Botón izquierdo para mover el objeto + instalación de corto alcance
Coloque el objeto de destino en la escena y cambie el material al color de resaltado
Al arrastrar un objeto desde la barra de elementos e instalarlo en el objeto de destino, el objeto arrastrado desaparece, el material del objeto de destino cambia al material original y el objeto arrastrado se destruye
el código
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class ControlMove : MonoBehaviour
{
public string str;
public Material originMeterial;
private Transform target;
private Vector3 cubeScreenPos;
private Vector3 offset;
IEnumerator ie;
void Start ( )
{
target= GameObject. Find ( str) . transform;
ie = OnMouseDown ( ) ;
StartCoroutine ( ie) ;
}
IEnumerator OnMouseDown ( )
{
cubeScreenPos = Camera. main. WorldToScreenPoint ( transform. position) ;
Vector3 mousePos = new Vector3 ( Input. mousePosition. x, Input. mousePosition. y, cubeScreenPos. z) ;
mousePos = Camera. main. ScreenToWorldPoint ( mousePos) ;
offset = transform. position - mousePos;
while ( Input. GetMouseButton ( 0 ) )
{
Vector3 curMousePos = new Vector3 ( Input. mousePosition. x, Input. mousePosition. y, cubeScreenPos. z) ;
curMousePos = Camera. main. ScreenToWorldPoint ( curMousePos) ;
if ( Vector3. Distance ( transform. position, target. position) < 2 )
{
transform. position = Vector3. MoveTowards ( transform. position, target. position, 5 * Time. deltaTime) ;
StopCoroutine ( ie) ;
if ( transform. position== target. position)
{
target. GetComponent < MeshRenderer> ( ) . material = originMeterial;
Destroy ( gameObject) ;
}
}
else
{
transform. position = curMousePos + offset;
}
yield return new WaitForFixedUpdate ( ) ;
}
}
}