I want to show the texture when user hovering a button, I already could do it. But, the problem is, when I hover the button, the texture is not appearing, but when I hover somewhere else, it is just like 250 pixels of x position from the x position of the button, the texture appears. How is it like that?
The picture below shows it (The yellow rectangle is the mouse position, when I hover to that rectangle, the texture appears beside the button, but the texture supposed to be showing when hovering exact same position as the button):
![alt text][1]
Here is the code that I am using:
using UnityEngine;
using System.Collections;
public class MapSelection : MonoBehaviour
{
private Rect villageRect = default(Rect); // Define the Rect
public GUISkin customMapButton = null; // Define for the custom map buttons
public Texture villageInformation = null; // For the map information
private bool isHoveringVillageButton = false; // Determine if the mouse hover the button
private void Start()
{
// Set the Rect for the village
villageRect = GameManager.CenterOnScreen(75, 20, 500, 250);
}
private void Update()
{
// If the mouse hovering village button
if (villageRect.Contains(Input.mousePosition))
{
// Set the isHoveringVillageButton to true
isHoveringVillageButton = true;
}
// Otherwise
else
{
// Set the isHoveringVillageButton to false
isHoveringVillageButton = false;
}
}
private void OnGUI()
{
// Call the SetButtons function
SetButtons();
// Call the HoverButton function
HoverButton();
}
private void SetButtons()
{
// If the button been clicked
if (GUI.Button(villageRect, "Village", customMapButton.customStyles[0]))
{
// Load another level
GameManager.LoadLevel("Third Loading Scene");
}
}
private void HoverButton()
{
// If the isHoveringVillageButton is true
if (isHoveringVillageButton)
{
// Draw the texture
GUI.DrawTexture(new Rect(villageRect.x - 100, villageRect.y - 35, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
}
}
}
**GameManager class:**
public static Rect CenterOnScreen(int width, int height, int minusWidth, int minusHeight)
{
Rect _center = new Rect(Screen.width - (Screen.width / 2) - minusWidth, Screen.height - (Screen.height / 2) - minusHeight, width, height);
return _center;
}
Thank you!
Your answer much appreciated!
[1]: http://i.stack.imgur.com/uSPtf.jpg
↧