Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在编辑模式下更改对象颜色_C#_Unity3d - Fatal编程技术网

C# 在编辑模式下更改对象颜色

C# 在编辑模式下更改对象颜色,c#,unity3d,C#,Unity3d,也许这比看起来要简单,但我找不到正确的方法。 在我的编辑器中,我有一个使用[ExecuteInEditMode]运行的MonoBehavior。在此脚本中,我有以下代码: void OnGUI() { Event e = Event.current; Vector3 mousePosition = e.mousePosition; mousePosition.y = Screen.height - mousePosition.y; Ray ray = cam.Sc

也许这比看起来要简单,但我找不到正确的方法。 在我的编辑器中,我有一个使用[ExecuteInEditMode]运行的MonoBehavior。在此脚本中,我有以下代码:

void OnGUI() {
    Event e = Event.current;
    Vector3 mousePosition = e.mousePosition;
    mousePosition.y = Screen.height - mousePosition.y;
    Ray ray = cam.ScreenPointToRay(mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo, 100f)) {
        if (hitInfo.transform.gameObject.name.Contains("MySphere")) {
           go_highlight = GameObject.Find(hitInfo.transform.gameObject.name);
           go_highlight.GetComponent<MeshRenderer>().sharedMaterial.color = Color.yellow;
        } 
    [...]
void OnGUI(){
事件e=当前事件;
Vector3鼠标位置=e.mousePosition;
mousePosition.y=Screen.height-mousePosition.y;
光线=摄像机屏幕指针方向(鼠标位置);
RaycastHitInfo;
if(物理.光线投射(光线,输出hitInfo,100f)){
if(hitInfo.transform.gameObject.name.Contains(“MySphere”)){
go_highlight=GameObject.Find(hitInfo.transform.GameObject.name);
go_highlight.GetComponent().sharedMaterial.color=color.yellow;
} 
[...]
在代码的另一部分中,如果光标不在球体上方,我将恢复球体的颜色。 在场景中,我有几个球体,我需要高亮显示鼠标指针下方的球体。 我得到的结果是,所有的球体都高亮显示,而不仅仅是光标下的球体。我猜这是“sharedMaterial”的意思,但我不能使用“Material”,因为它会返回一个错误。 我还可以使用OnMouseEnter和OnMouseExit将脚本附加到球体上,但我必须在编辑模式下工作,而且这些方法甚至在[ExecuteInEditMode]下也无法工作


你有什么建议吗?

我建议你改变物体的材质,而不是改变颜色

例如:

  • obj有材料(1)
  • 材料(1)为红色
  • 鼠标悬停在obj上时,将更改为材质(2)
  • 材料(2)是蓝色的
  • 允许这种改变

    go_highlight.GetComponent<MeshRenderer>().sharedMaterial.color = Color.yellow;
    
    go\u highlight.GetComponent().sharedMaterial.color=color.yellow;
    

    go_highlight.GetComponent().material=material(2);
    

    这是一个简化的解决方案,以匹配您的代码,但是有更有效的方法来实现这一点。

    不必直接更改材质,因为它们共享一种材质,因此名为
    sharedMaterial
    ,然后您可以使用OnDrawGizmos方法在与悬停对象相同的位置和比例绘制一条到球体的线。Fol根据你的建议,我意识到小发明也可以被挑选出来。我正在评估我是否可以用小发明来改变球体。谢谢。谢谢,我真的没有想过。
    go_highlight.GetComponent<MeshRenderer>().material = material (2);