I’m just new to Unity and for a project I’m trying to change movie textures on an object (cube) with an UI button.
I’m able to change the textures with the space bar (see example script below) but I can’t figure out how to use an UI button instead of the space bar.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CreateTexture : MonoBehaviour {
public Texture[] textures;
public int currentTexture;
void Start()
{
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.Space))
{
currentTexture++;
currentTexture %= textures.Length;
GetComponent<Renderer>().material.mainTexture = textures[currentTexture];
}
}
}
So above script does exactly what I want. I just want to use an UI button instead of the spacebar
Cool this already helped out! Now I’m able to change the materials through a UI button click Thing is, the movie (set as material) won’t play anymore. I do have a ‘play movie script’ on the object though:
using UnityEngine;
using System.Collections;
public class PlayMovie : MonoBehaviour {
// Use this for initialization
void Start () {
MovieTexture movie = GetComponent<Renderer>().material.mainTexture as MovieTexture;
movie.Play();
movie.loop = true;
}
// Update is called once per frame
void Update () {
}
}
If I just assign a material - with a movie assigned to it - to the object it works perfectly well. However when the material is changed with the UI button, the script doesn’t start the movies anymore.