Control video player with button and change scene - c#

I'm trying to create a FMV game and I would like to figure out a way to control the video player with a UI button to change scene but only after the clip has finished playing.
If the user doesn't pick anything it will automatically be a default pathway and I have managed to use loopPoinReached and it has worked.
I can figure out how to use the same loopPointReached with a button.
Here is the video player code and it will auto load the next scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadBaseCampSeq : MonoBehaviour
{
void Start()
{
GameObject camera = GameObject.Find("Main Camera");
var videoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
videoPlayer.url = "Assets/VideoAssets/intro.mp4";
videoPlayer.isLooping = false;
videoPlayer.Play();
videoPlayer.loopPointReached += EndReached;
}
void EndReached(UnityEngine.Video.VideoPlayer vp)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("baseCampSeq");
}
}
Here is the basic code that I have assigned to the UI Button On Click ()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadTruckButton : MonoBehaviour
{
public void truckSeq()
{
SceneManager.LoadScene("truckSeq");
}
}
I have been trying to reference the "EndReached" from the other script in the button but I cannot figure it out.
Any help would be awesome!
Thanks

You'll have to segregate the videoplayer functionality from the loadBaseCampSeq class into a new class. In the loadBaseCampSeq class create a videoplayer (instance) field and an abstract EndReached method:
public abstract class loadBaseCampSeq
{
public VideoPlayer videoPlayer;
public abstract void EndReached();
}
Create a button class which inherits from the loadBaseCampSeq base class. In the button class override the EndReached method that uses the videoplayer instance from it's base class:
public class Button : loadBaseCampSeq
{
public override class EndReached()
{
videoPlayer.EndReached();
}
}

Related

Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity

I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}

How can we display an image when the player collide with an object?

I'm working on 2D Unity game and I would like to know how we can display an image on my inventory when the player collide with an object. For exemple, if the player collide with a drumstick, the drumstick image appear on the inventory menu.
Best Regards.
Yacine TAZDAIT.
Method #1
There are many different ways to display an image. For example, you can have an image with an image component, and you switch the component on and off whenever you want the image to appear/disappear. You can do this using code similar to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Image drumstick;
public void Start()
{
toggleDrumstick(); // This will toggle the drumstick. For example, if the drumstick is not being shown at the time, the drumstick will show on the screen. The opposite is true.
}
public void toggleDrumstick() {
drumstick.enabled = !drumstick.enabled;
}
}
Method #2
The code above is a great solution, but there is a more modular way to do it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Drumstick : MonoBehaviour
{
public static bool enabled = this.image.enabled;
}
I recommend the method above. The reason for this is because every script can now access the drumstick's status. For example, your player script can do this.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void doSomething () {
Drumstick.enabled = true; // make the image appear.
}
}
For any of these methods to work, make sure your drumstick uses the image component.
EDIT:
To answer your question further, here is a way to implement method #2 in your code. In your player script, you can use OnCollisionEnter and the method above to make the drumstick appear.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "drumstick") Drumstick.enabled = false;
}
}
For this to work, make sure that the drumstick has the tag "drumstick".

Unity open external link

By placing the unity file on the page I can not redirect the action to another page. What happens is that the new page is opened inside the frame intended for the unity file.
using UnityEngine;
using System.Collections;
public class Urlazerecovelo : MonoBehaviour {
void OnMouseDown ()
{
Application.OpenURL("https://www.google.pt");
}
}
public class OpenWebPage : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Application.ExternalEval("window.open('www.google.pl', '_blank');");
}
}
I assume from what you're saying you use WebGL as target platform. More about here

Unity 3d script adding functions as trigger on Inspector window

I have defined three functions (OnReticleEnter(),Exit() and hover()) in the script and attached with a game object:
using UnityEngine;
using System.Collections;
using TMPro;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
void OnReticleEnter()
{
gOBJ.SetActive(true);
}
void OnReticleExit()
{
gOBJ.SetActive(false);
}
void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
My problem is I want these functions in the inspector window so that I can define multiple actions like event trigger's pointer enter.
Is there any way I can make these functions available as event trigger on inspector window?

Mimicking custom function

I have defined some custom functions in the following script and attached with my gameobject. I want to declare my function OnReticleEnter() to mimick OnPointerEnter(). Is it possible? I want this because I want to access my function OnReticleEnter() in the inspector so that I can define multiple actions like EventTrigger's Point Enter, Exit etc.
Is it possible to make the function OnReticleEnter() to behave as OnPointerEnter() programmatically?
using UnityEngine;
using System.Collections;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
public void OnReticleEnter()
{
gOBJ.SetActive(true);
}
public void OnReticleExit()
{
gOBJ.SetActive(false);
}
public void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
You need to inherit the interfaces for the pointerEnter and PointerExit events in your class to be able to implement them. Your code might look something like this :
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class MyObject : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
{
// Custom reticle events
public GameObject gOBJ;
public void OnPointerEnter(PointerEventData eventData){
gOBJ.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData){
gOBJ.SetActive(false);
}
}

Categories

Resources