Last active
March 16, 2018 12:43
-
-
Save amimaro/e524ceb9627efe38e3856f65cfd91802 to your computer and use it in GitHub Desktop.
Unity Scripts to Change GameObject Texture OnSomething
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class HoverColor : MonoBehaviour | |
{ | |
Renderer m_renderer; | |
public Texture textureNotPressed, texturePressed; | |
void Start () | |
{ | |
m_renderer = GetComponent<Renderer> (); | |
} | |
void Update () | |
{ | |
} | |
void OnMouseOver () | |
{ | |
m_renderer.material.SetTexture ("_MainTex", texturePressed); | |
} | |
void OnMouseExit () | |
{ | |
m_renderer.material.SetTexture ("_MainTex", textureNotPressed); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PressedButton : MonoBehaviour | |
{ | |
Renderer m_renderer; | |
public Texture textureNotPressed, texturePressed; | |
void Start () | |
{ | |
m_renderer = GetComponent<Renderer> (); | |
} | |
void Update () | |
{ | |
} | |
void OnMouseDown () | |
{ | |
m_renderer.material.SetTexture ("_MainTex", texturePressed); | |
} | |
void OnMouseUp () | |
{ | |
m_renderer.material.SetTexture ("_MainTex", textureNotPressed); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ToggleColor : MonoBehaviour | |
{ | |
bool isPressed = false; | |
Renderer m_renderer; | |
public Texture textureNotPressed, texturePressed; | |
void Start () | |
{ | |
m_renderer = GetComponent<Renderer> (); | |
} | |
void Update () | |
{ | |
} | |
void OnMouseUp () | |
{ | |
if (isPressed) { | |
m_renderer.material.SetTexture ("_MainTex", textureNotPressed); | |
isPressed = false; | |
} else { | |
m_renderer.material.SetTexture ("_MainTex", texturePressed); | |
isPressed = true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Version 5.6 | |
* Pressed | |
Changes GameObject texture when clicked/pressed. OnMouseUp the texture returns to it's original state. | |
* Toggle | |
Changes GameObject texture when clicked/pressed and saves it state. The texture only change when pressed again. | |
* Hover | |
Changes texture when the mouse cursor are over the GameObject. The texture returns to it's original state when mouse exit the GameObject. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment