通过指向它获取对象名称 Unity Raycast
Getting objects name by pointing on it Unity Raycast
我想打印我指向的对象的名称 (使用 crosshari)。我写了一些脚本 允许我通过查看它来突出显示对象(包括在下面)。你能告诉我如何添加一行代码来完成这个(打印我指向的对象 的名称)吗?
1.SelectionManager
`public class SelectionManager : MonoBehaviour
{
[SerializeField] private string selectableTag = "Selectable";
private ISelectionResponse _selectionResponse;
private Transform _selection;
private void Awake()
{
_selectionResponse = GetComponent<ISelectionResponse>();
}
private void Update()
{
if (_selection != null) _selectionResponse.OnDeselect(_selection);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
_selection = null;
if (Physics.Raycast(ray, out var hit))
{
var selection = hit.transform;
if (selection.CompareTag(selectableTag) || (selection.CompareTag("Stool_square1")))
{
//string name = gameObject.name;
//print(name);
_selection = selection;
}
}
if (_selection != null) _selectionResponse.OnSelect(_selection);
}
}`
2.OutlineSelectionResponse
using UnityEngine;
public class OutlineSelectionResponse : MonoBehaviour, ISelectionResponse
{
public void OnSelect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 2;
}
public void OnDeselect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 0;
}
}
3.ISelectionResponse
using UnityEngine;
internal interface ISelectionResponse
{
void OnSelect(Transform selection);
void OnDeselect(Transform selection);
}
试试 hit.collider.name
我想打印我指向的对象的名称 (使用 crosshari)。我写了一些脚本 允许我通过查看它来突出显示对象(包括在下面)。你能告诉我如何添加一行代码来完成这个(打印我指向的对象 的名称)吗?
1.SelectionManager
`public class SelectionManager : MonoBehaviour
{
[SerializeField] private string selectableTag = "Selectable";
private ISelectionResponse _selectionResponse;
private Transform _selection;
private void Awake()
{
_selectionResponse = GetComponent<ISelectionResponse>();
}
private void Update()
{
if (_selection != null) _selectionResponse.OnDeselect(_selection);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
_selection = null;
if (Physics.Raycast(ray, out var hit))
{
var selection = hit.transform;
if (selection.CompareTag(selectableTag) || (selection.CompareTag("Stool_square1")))
{
//string name = gameObject.name;
//print(name);
_selection = selection;
}
}
if (_selection != null) _selectionResponse.OnSelect(_selection);
}
}`
2.OutlineSelectionResponse
using UnityEngine;
public class OutlineSelectionResponse : MonoBehaviour, ISelectionResponse
{
public void OnSelect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 2;
}
public void OnDeselect(Transform selection)
{
var outline = selection.GetComponent<Outline>();
if (outline != null) outline.OutlineWidth = 0;
}
}
3.ISelectionResponse
using UnityEngine;
internal interface ISelectionResponse
{
void OnSelect(Transform selection);
void OnDeselect(Transform selection);
}
试试 hit.collider.name