I have been having difficulty getting my movie to start playing once a certain button is hit. I can get it to work with void Start() but then the video starts playing as soon as the scene is loaded which I don't want. I have tried this for when the button is pressed to start playing the movie.
public MovieTexture myMovie;
public RawImage myLayer;
private new AudioSource audio;
public void onClick()
{
GetComponent<RawImage>().texture = myMovie as MovieTexture;
audio = GetComponent<AudioSource> ();
audio.clip = myMovie.audioClip;
myMovie.Play ();
audio.Play ();
}
The movie will show but not actually play. I'm sure I'm missing something obvious but as I am a noob at coding I can't figure out what it is. Any help would be greatly appreciated!
Make a custom method which will contain
void Play()
{
myMovie.Play ();
audio.Play ();
}
and assign it to the button component in the inspector
EDIT:
I meant, don't use onClick as you use Start etc, it's not executable like that. There are 2 ways:
1) You make a custom method (called whetever you want) e.g as I listed above and assign it to the onClick field in the inspector, press +, add a script and select a method from that script;
2)
public Button yourButton;
void Start () {
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
Debug.Log ("You have clicked the button!");
}
like in the reference: docs.unity3d.com/ScriptReference/UI.Button-onClick.html
You can add a Video Player using Create -> Video -> Video Player
Set video player video clip, Render Mode as Camera Far Plane and Select camera as Main Camera
Create script as given below. Just call the playMovie() method in any button event
Related
Im making my first 2D game in unity and I have successfully made it. However I want a start button.I have looked up videos on how to make a start button and they all seem to have a start button on a different screen and once clicked it goes to another scene. Where as I would like my button to be on the same screen as my game and once clicked it starts the game, while having the button disappear. Can the code also be in c# thank you.
If you don't want to have different scenes (which, by the way, it's the recommended pattern) you can pause the game until the player presses the button. More informations can be found here.
using UnityEngine;
using UnityEngine.UI;
class GameManager: MonoBehaviour
{
public Button startButton;
private void Awake()
{
Time.timeScale = 0f;
}
private void OnEnable()
{
startButton.onClick.AddListener(StartGame);
}
private void OnDisable()
{
startButton.onClick.RemoveListener(StartGame);
}
private void StartGame()
{
Time.timeScale = 1f;
// Hides the button
startButton.gameObject.SetActive(false);
}
}
Put your button on your main scene that's it.
I want to do menu animation. In the animator, its contain 2 animations. One is opening animation, the other is closing animation. I set the opening animation as default state and added condition between of them. The condition is a bool type parameter. I drag the script which controls they behaviours and animator component on gameobject, but when the opening animation plays and player clicks the play button the parameter turns into true but it doesn't play.
Animator canvasAnim;
public Button lvlSelector;
Button lvlSelector_A;
// Use this for initialization
void Start () {
canvasAnim = GetComponent<Animator>();
lvlSelector_A = lvlSelector.GetComponent<Button>();
lvlSelector_A.onClick.AddListener(LevelSelector);
}
IEnumerator SlideLevelSelectMenu()
{
yield return new WaitForSeconds(1f);
SceneManager.LoadScene("LevelSelectMenu");
}
void LevelSelector()
{
canvasAnim.SetBool("clickedclose", true);
StartCoroutine(SlideLevelSelectMenu());
}
Animator:
Gameobject:
This problem occurs because of your Animator. You should call Animator on Canvas game object (Not on Menu Manager). Just change your script to this:
void Start () {
canvasAnim = GameObject.Find("Canvas").GetComponent<Animator>();
...
}
I hope it helps you.
I have a gameObject with attached animation component, and have added animationClip1.anim with it.
I have the following code
public AnimationClip anim;
void Update () {
if (Input.GetButtonUp("Fire1"))
{
GetComponent<Animation>().Play(anim.name);
}
}
from interface, i have drag and dropped animationClip1.anim to the (above public variable) of the script..
Script is attached to the object.
So when i run the game, i get this error
The animation state 'animationClip1.anim' could not be played because
it couldn't be found!
Please attach an animation clip with the name 'animationClip1.anim' or call this function only for existing
animations.
Where is the problem, why on mouse click i am getting this error.. Any Idea please ? Thanks
You can try this out
void Update () {
if (Input.GetButtonUp(0))
{
GetComponent<Animation>().Play("Animation name");
}
}
Here's the code I'm using :
[RequireComponent (typeof(AudioSource))]
public class video : MonoBehaviour {
public MovieTexture movie;
public bool loop;
private AudioSource audio;
// Use this for initialization
void Start () {
GetComponent<RawImage> ().texture = movie as MovieTexture;
audio = GetComponent<AudioSource> ();
audio.clip = movie.audioClip;
movie.Play ();enter code here
audio.Play ();
movie.loop = true ();
}
// Update is called once per frame
void Update () {
}
void onMouseDown (){
if (Input.GetKeyDown(KeyCode.Mouse0)&& !movie.isPlaying)
movie.Play ();
else if (Input.GetKeyDown(KeyCode.mouse) && movie.isPlaying)
movie.Pause ();
}
}
What I want is when I click the video, the video plays, finishes, and if I click the video again, it restarts the video.
movie.loop = true;
Just Google next time: http://docs.unity3d.com/ScriptReference/MovieTexture-loop.html
I think what is needed is a check to see if the movie is finished playing or not.
I've not done any work with movies in Unity, so this is hypothetical, but something like this might work:
if (Input.GetKeyDown(KeyCode.Mouse0) && movie.hasFinishedPlaying)
{
movie.Play ();
}
What this sort of pseudocode is saying is that, if the movie has finished, and we click the mouse down, then we play the movie again.
I've just glanced at your code again. What you seem to be doing is doing a pause method in the OnMouseDown function, and setting the movie.loop to true.
However, setting something to true should be like this:
movie.loop = true;
And not like this:
movie.loop = true();
Unless it is different for using movies in Unity, but I don't know :) Give it a try!
I need to have an object in my scene change between two different materials at run time, when ever a button is pressed in my Unity project. However, I have never done this before and I'm having an issue getting my head around how to do this.
In my scene I have one game object I've called my controller. This script holds my material switching class and is looking like this:
public GameObject cupMesh;
bool isOn = true;
// Use this for initialization
void Start ()
{
cupMesh = GameObject.Find("CupMesh");
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
if(GUI.Button(new Rect(10,10, 100, 40), "Show mesh"))
{
renderer.enabled = false;
}
}
I know this doesn't change the material, but the above code does nothing. I've never modified anything on the mesh renderer before but I know there is a list of materials on it.
How can I access that list so I can have my program switch between the two materials found there?
To show or hide a gameObject, rather than using render.enabled property, you should use this:
// Unactivates the game object.
gameObject.SetActive (false);
However it's not clear from the code if you want to adjust the material of the object the script is attached to, or the cupMesh game object.
If you wanted to make the cupMesh disappear, you would use:
cupMesh.SetActive (false);
Or if you wanted to access the material component of the cupMesh, this is
cupMesh.renderer.material