Skip to content

Instantly share code, notes, and snippets.

@amimaro
Last active March 16, 2018 12:43
Show Gist options
  • Save amimaro/e524ceb9627efe38e3856f65cfd91802 to your computer and use it in GitHub Desktop.
Save amimaro/e524ceb9627efe38e3856f65cfd91802 to your computer and use it in GitHub Desktop.
Unity Scripts to Change GameObject Texture OnSomething
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);
}
}
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);
}
}
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;
}
}
}
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