Hi guys, I want to show the texture when hovering a button. But when I tried below code, the texture is not appears, even though the mouse already hover the button. When I tested it on the OnGUI
function, it is appears correctly, just not show when hovering on a button.
How could I fix this?
Here is the code that I am using:
using UnityEngine;
using System.Collections;
public class MapSelection : MonoBehaviour
{
private Rect villageRect, yeinPlainRect; // Define the Rect
public GUISkin customMapButton = null; // Define for the custom map buttons
public Texture villageInformation = null; // For the information
public static bool isEnteredVillage = false; // For checking if the player entering the village from the map
private void Start()
{
// Set the Rect for the village
villageRect = GameManager.CenterOnScreen(75, 20, 500, 250);
// Set the Rect for the yein plain
yeinPlainRect = GameManager.CenterOnScreen(75, 20, 550, 125);
}
private void Update()
{
// This below code is not run
// If the mouse hovering village button
if (villageRect.Contains(Input.mousePosition))
{
// Draw the texture
GUI.DrawTexture(new Rect(villageRect.x, villageRect.y, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
}
// End of it
}
private void OnGUI()
{
// Call the SetButtons function
SetButtons();
// I have tested it, and the image show on the screen like the second image below:
// Draw the texture
GUI.DrawTexture(new Rect(villageRect.x - 100, villageRect.y - 35, 500, 325), villageInformation, ScaleMode.ScaleToFit, true);
// End of it
}
private void SetButtons()
{
// If the button been clicked
if (GUI.Button(villageRect, "Village", customMapButton.customStyles[0]))
{
// Load another level
GameManager.LoadLevel("Third Loading Scene");
}
}
}
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;
}
Here is the screenshot:
First Image (The button on hovering):
Second Image (The button is not on hovering):
[36237-capture+2.png|36237]
Thank you guys
Your answer much appreciated!