如何进行“突出显示"?在gameObject上 [英] How to do "highlighting" on gameObject

查看:95
本文介绍了如何进行“突出显示"?在gameObject上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作类似块的游戏,现在我想在鼠标悬停在该特定块上的同时实现突出显示块(游戏对象).

I'm making an block-like game and now I want to implement highlighting block (gameobject) while there is mouseOver that specific block.

我尝试了类似的方法(我不确定这是最好的方法,但这只是我的主意):

I tried something like this (I'm not sure that this is best way to do this, but it's only one I got idea for):

#pragma strict

public class BlockSelecting extends MonoBehaviour {


public var hovering : boolean = false;

public var xpos : float;
public var ypos : float;

function Start () {

}

function Update () {

}

function OnMouseExit () {
hovering = false;
}

function OnMouseOver () 
{     
        hovering = true;
        xpos = Input.mousePosition.x;
        ypos = Input.mousePosition.y;
}
function OnGUI ()
{
GUI.DrawTexture(new Rect(xpos, xpos, 26, 26), (Resources.Load("highlight") as Texture2D));
}
}

这不起作用,因为纹理未显示鼠标指针的位置.有什么我看不见的东西吗?或者用这种错误的方式来做到这一点?我的高亮资源只是透明矩形的26x26(块为25x25)2D纹理,因此看起来像是被高亮显示了.

This is not working, since Texture is not showing where mouse pointer is. Is there something I can't see or is this wrong way to do this? My highlight resource is just an 26x26 (block is 25x25) 2D texture of transparent rectangle, so it looks like it's highlighted...

P.S.我的计划是使用悬停布尔值检查播放器是否仍在悬停,是否应删除/隐藏纹理(有关如何执行此操作的任何想法?).

P.S. My plan is to use hovering boolean to check if player is still hovering, if not texture should be deleted/hidden (any ideas on how to do this?).

推荐答案

允许您将鼠标所在的对象的颜色更改为红色,以便 您应该使用OnMouseEnter检查鼠标是否在对象上,并使用OnMouseExit检查鼠标何时退出对象区域,然后将其颜色恢复为更改之前的原始颜色

lets say you want to change the color of the object that mouse is on to red so you should use OnMouseEnter to check if mouse is on your object and OnMouseExit for when mouse exits the objects area and we set the it`s color back to its original color that was before changing it

private color tempColor;
 void OnMouseEnter()
 {
     tempColor = renderer.material.color;
     renderer.material.color = Color.red;
 }
 void OnMouseExit()
 {
     renderer.material.color = tempColor;
 }

这篇关于如何进行“突出显示"?在gameObject上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆