Unity Comment générer des objets en un clic de souris
Dans ce didacticiel, je vais montrer comment générer un objet avec un clic de souris dans Unity.
Le script gérera la génération des Préfabriqués à la position de la souris et leur alignement sur la normale de la surface.
Pas
- Créez un nouveau script, appelez-le SC_ClickSpawner et collez-y le code ci-dessous:
SC_ClickSpawner.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SC_ClickSpawner : MonoBehaviour
{
public GameObject[] prefabs; //Prefabs to spawn
Camera c;
int selectedPrefab = 0;
int rayDistance = 300;
// Start is called before the first frame update
void Start()
{
c = GetComponent<Camera>();
if(prefabs.Length == 0)
{
Debug.LogError("You haven't assigned any Prefabs to spawn");
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectedPrefab++;
if(selectedPrefab >= prefabs.Length)
{
selectedPrefab = 0;
}
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
selectedPrefab--;
if (selectedPrefab < 0)
{
selectedPrefab = prefabs.Length - 1;
}
}
if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftShift))
{
//Remove spawned prefab when holding left shift and left clicking
Transform selectedTransform = GetObjectOnClick();
if (selectedTransform)
{
Destroy(selectedTransform.gameObject);
}
}
else if (Input.GetMouseButtonDown(0))
{
//On left click spawn selected prefab and align its rotation to a surface normal
Vector3[] spawnData = GetClickPositionAndNormal();
if(spawnData[0] != Vector3.zero)
{
GameObject go = Instantiate(prefabs[selectedPrefab], spawnData[0], Quaternion.FromToRotation(prefabs[selectedPrefab].transform.up, spawnData[1]));
go.name += " _instantiated";
}
}
}
Vector3[] GetClickPositionAndNormal()
{
Vector3[] returnData = new Vector3[] { Vector3.zero, Vector3.zero }; //0 = spawn poisiton, 1 = surface normal
Ray ray = c.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, rayDistance))
{
returnData[0] = hit.point;
returnData[1] = hit.normal;
}
return returnData;
}
Transform GetObjectOnClick()
{
Ray ray = c.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, rayDistance))
{
Transform root = hit.transform.root;
if (root.name.EndsWith("_instantiated"))
{
return root;
}
}
return null;
}
void OnGUI()
{
if(prefabs.Length > 0 && selectedPrefab >= 0 && selectedPrefab < prefabs.Length)
{
string prefabName = prefabs[selectedPrefab].name;
GUI.color = new Color(0, 0, 0, 0.84f);
GUI.Label(new Rect(5 + 1, 5 + 1, 200, 25), prefabName);
GUI.color = Color.green;
GUI.Label(new Rect(5, 5, 200, 25), prefabName);
}
}
}
- Attachez le script SC_ClickSpawner à la caméra principale
- Attribuez les Prefabs que vous avez l'intention de générer au tableau Prefabs dans SC_ClickSpawner.
- Dans mon cas, j'utiliserai des formes simples:
Astuce: Pour éviter que les préfabriqués générés ne soient "buried" dans la surface, assurez-vous que leur transformation racine (ou le pivot, s'il s'agit d'un objet unique) est en bas.
- Déplacez la caméra principale devant une surface
Maintenant, lorsque vous appuyez sur Play, vous devriez pouvoir générer un objet en cliquant avec le bouton gauche sur la surface et supprimer les objets en maintenant Maj gauche + Clic gauche.
Appuyer sur '1' sélectionnera le préfabriqué suivant dans la liste et appuyer sur '2' sélectionnera le précédent.