如何做“突出显示"在游戏对象上 [英] How to do "highlighting" on gameObject

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

问题描述

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

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. 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;
 }

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

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