选择和取消选择游戏对象以更改材质
Posted
技术标签:
【中文标题】选择和取消选择游戏对象以更改材质【英文标题】:Selecting and deselecting gameobject to change material 【发布时间】:2021-03-10 17:15:50 【问题描述】:我的代码有一些问题。 我正在尝试创建一个突出显示功能,以便玩家可以单击游戏对象并查看他们选择的内容。
我希望它保持突出显示 直到他们选择另一个对象。 当他们选择另一个对象时,默认材质将是 应用于前一个游戏对象。它 将继续为每个对象他们 选择和取消选择。谁能帮帮我。我已经尝试了一些东西,但它只是保持颜色不变。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectionManager : MonoBehaviour
[SerializeField] private string selectableTag = "Selectable";
[SerializeField] private Material highlightMaterial;
private Material originalMaterial;
private GameObject selected;
private GameObject oldSelected;
// Start is called before the first frame update
void Start()
// Update is called once per frame
void Update()
Renderer selectionRenderer;
//change the color to the highlight
if (Input.GetMouseButtonDown(0))
//if selected set to highlight color
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 300.0f))
selected = hit.transform.gameObject;
Debug.Log("1111 new Selection --> " + selected);
if (selected.CompareTag(selectableTag))
if(selected != oldSelected || oldSelected == null)
//save the original material
selectionRenderer = selected.GetComponent<Renderer>();
originalMaterial = selectionRenderer.material;
selectionRenderer.material = highlightMaterial;
Debug.Log("Old Selection --> " + oldSelected);
Debug.Log("new Selection --> " + selected);
else
oldSelected = selected;
Debug.Log("you have selected a new building");
oldSelected.GetComponent<Renderer>().sharedMaterial = originalMaterial;
//comment 2
//comment 3
//comment1
void ClearSelection()
if (selected == null)
return;
else
selected.GetComponent<Renderer>().sharedMaterial = originalMaterial;
selected = null;
【问题讨论】:
【参考方案1】:所以问题基本上是你没有正确分配oldSelected
。
解决此问题,您的代码应如下所示:
using UnityEngine;
public class SelectionManager : MonoBehaviour
[SerializeField] private string selectableTag = "Selectable";
[SerializeField] private Material highlightMaterial;
private Material originalMaterial;
private GameObject selected = null;
private GameObject oldSelected = null;
private Renderer selectionRenderer;
private Renderer oldSelectionRenderer;
// Update is called once per frame
void Update()
//change the color to the highlight
if (Input.GetMouseButtonDown(0))
//if selected set to highlight color
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 300.0f))
selected = hit.transform.gameObject;
Debug.Log("1111 new Selection --> " + selected);
if (selected.CompareTag(selectableTag))
//newSelection
if(selected != oldSelected)
//Set original material to oldSelected if it's not the first selection
if(oldSelected != null)
oldSelectionRenderer = oldSelected.GetComponent<Renderer>();
oldSelectionRenderer.material = originalMaterial;
//Set highlighted material to selected
selectionRenderer = selected.GetComponent<Renderer>();
originalMaterial = selectionRenderer.material;
selectionRenderer.material = highlightMaterial;
//assign oldSelected GO to your current selection
oldSelected = selected;
【讨论】:
以上是关于选择和取消选择游戏对象以更改材质的主要内容,如果未能解决你的问题,请参考以下文章