版权声明:本文为博主原创文章,如需转载,请注明出处: https://blog.csdn.net/MASILEJFOAISEGJIAE/article/details/84205633
一般都是用RaycastHit从鼠标位置发射一条射线到GameObject进行选中。
前提:被选的GameObject要加上Collider,否则无法选中。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject go = hit.collider.gameObject; //获得选中物体
string goName = go.name; //获得选中物体的名字,使用hit.transform.name也可以
print(goName);
}
}
}
}
参考资料: