How to do “highlighting” on gameObject

会有一股神秘感。 提交于 2019-12-08 12:46:25

问题


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

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?).


回答1:


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


来源:https://stackoverflow.com/questions/28383837/how-to-do-highlighting-on-gameobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!