How do i create a time delay before the button appears unity [duplicate] - unity3d

This question already has answers here:
How to make the script wait/sleep in a simple way in unity
(7 answers)
Closed 1 year ago.
I am creating an interactive video using Unity/C#. I am doing this using scenes and buttons that when clicked, go to the next scene or back. However, I want the buttons to appear once the video is finished. Is there any way I can add a delay to the buttons before they appear in the scene when playing?

For time delay you could use coroutines. Check it out here There is options like waitforseconds so you can delay your button apperance. here is sample code
private IEnumerator ShowBtn()
{
yield return new WaitWhile(() => videoplayer.isPlaying);
// show visibility of your button
}
And when you play video call this function like this
StartCoroutine(ShowBtn());

This should do the trick. isPlaying.
A simple script would look like this.
if(videoplayer.isPlaying == false && videoWasPlayed == true){
btn.active = true;
videoWasPlayed = false;
}
videoWasPlayed is used to check if the video was ever played. This would need to be set to true when the video is played.

Related

Audio volume change is lagging

Im playing two tracks in sync. This works well, they sound like one song.
What im trying to do is allow player to switch one track on and of by toggling mute or setting volume to 0 or 1. The muted track continues playing in sync with other track but cant be heard.
But the volume change /muting is lagging. So even if you switch the sound on exactly on beat you wont hear that beat cause it takes a split second for the code to react. There must be a way to make the change instant?
Below is a part of the music manager that handles this. The full code also includes how sound is loaded and starts playing and can be seen here:
https://github.com/Lautaro/AudioTest/blob/master/Assets/MusicManager.cs
// Update is called once per frame
void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Toggle();
}
}
}
void Toggle()
{
var clip = Sources.First(s => s.clip.name == "ExtraBeat");
// Same result if setting clip.volume instead of mute
clip.mute = !clip.mute;
}
The complete project with audio tracks can be cloned here. Use space to toggle sound.
https://github.com/Lautaro/AudioTest.git

Manually programming the splash screen to appear at arbitrary times

In our app, the splash screen appears when we start the app (or build to Android device) as the app is loading, which is done through the Unity's Edit -> Project Settings -> Player but we now have a feature that sometimes in the middle of the app also re-starts the app, so we would like to code this behavior, so that we can show a different splash screen if the app re-starts mid-usage.
We cannot seem to be able to figure out how to do this programmatically, or where exactly in the app code, so we would appreictae any help.
What we do know (and have already implemented) is through:
PlayerPrefs.SetString("LastShownComponent", menuId);
PlayerPrefs.Save();
which remembers if this is the first time the app is starting (original splash image) or whether the user is mid-usage, but how do we specify another image to be loaded as splash screen when the app is reloading mid-usage?
EDIT: more details...
Previously, we only had the following code:
if (_callbackUri == null)
{
SceneManager.LoadScene("ReloadScene");
}
Now, we force the app to re-start mid-usage (for a specific reason) by:
if (_callbackUri == null)
{
PlayerPrefs.SetInt("Restarting", 1);
PlayerPrefs.Save();
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidPlugin.Restart();
#else
SceneManager.LoadScene("ReloadScene");
#endif
}
However, when it restarts, obviously it re-loads the same splash screen image that is in the player settings.
We probably need to add code just below AndroidPlugin.Restart(); to load a new splash screen image, but how do we do that? Do we need a new scene for that?
Per the comments:
Quickest way to test this is to create a blank scene, add a gameobject to that scene name it something like SplashLoader and give it a script.
the only thing you need in that script is the start method,
void Start()
{
// Default to 0 incase this value isn't stored
int reloaded = PlayerPrefs.GetInt("Restarting", 0);
// Reset to 0 so if the game is closed without restarting it will display the correct scene
PlayerPrefs.SetInt("Restarting", 0);
PlayerPrefs.Save();
if(reloaded == 1)
{
SceneManager.LoadScene("ReloadScene");
}
else
{
SceneManager.LoadScene("SplashScene");
}
}
From here you would create 2 scenes, one for your normal SplashScene and 1 for your ReloadScene. In those scene you can create a canvas object and add an image to it then change the image depending which scene it is.
Another Option if you want to use a single scene and keep the same animation say you have an effect where your logo fades in, you can do this:
Create a scene for your splash screen, make that your 0 index scene, add a canvas to it and an image, then use an animator to get your splash screen effect going.
Add a script to your image object, to change the picture depending on what the preference stores, you can use almost the same code as the start method above, instead of calling the SceneManager to load the scene you would just update the image.
Short answer: you can't.
The splash screen (and everything associated with it) can only be changed/modified inside the editor (via Inspector or via code using the UnityEditor.PlayerSettings.SplashScreen class).
To add insult to injury, on Android you MUST have a splash screen: if you try to disable it via the Player Settings, the Android app won't work.
So, the only solution you have is to use a blank splash screen, and then two different starting scenes (with different backgrounds) which are loaded, mutually exclusively, if you are launching or restarting the app (by using a field in PlayerPrefs as you correctly thought).
EDIT
Actually, you don't even need multiple scenes to accomplish what you want to do.
Create just one scene (let's call it SplashScene), with just the Main Camera and a Canvas with an Image.
Attach this script to the Main Camera:
using UnityEngine;
using UnityEngine.UI;
using System;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField]
Image backgroundImage;
[SerializeField]
Sprite launchBackground, restartBackground;
private void Awake() {
if (!PlayerPrefs.HasKey("Background Splash Screen"))
PlayerPrefs.SetInt("Background Splash Screen", 0);
int background = PlayerPrefs.GetInt("Background Splash Screen");
switch (background) {
case 0:
backgroundImage.sprite = launchBackground;
break;
case 1:
backgroundImage.sprite = restartBackground;
break;
default:
throw new Exception("Invalid Background Splash Screen PlayerPref value!");
break;
}
}
}
Of course SplashScene should be at index 0 in the standalone scenes list.
The only other thing you need to do is to add
PlayerPrefs.SetInt("Background Splash Screen", 0);
before any Application.Quit(); method you call in your project and
PlayerPrefs.SetInt("Background Splash Screen", 1);
before any AndroidPlugin.Restart(); method in your code.
Don't forget to assign the sprites and the image reference to the variables of the script via Inspector ofc (you can get them via GetComponent and asset loading if you prefer, it's up to you).
That's pretty much it. :)

How to avoid demo scene the second time you run an app in Unity [duplicate]

This question already has answers here:
In Unity, how to tell if it's the first time a game is being opened?
(4 answers)
Closed 5 years ago.
we are making an app-game in Unity, with a demonstration scene that runs the first time you open the app.
I would like to know how to go directly to the game main scene the second time you run the app and therefore avoid the demo scene.
Thanks
You can use PlayerPref to remember that the user has already visited. Something like this in the Awake() of the demo scene:
private void Awake(){
if(PlayerPref.hasKey("returningUser")){
SceneManager.LoadScene("MainScene");
return;
}
PlayerPref.setInt("returningUser", 1);
PlayerPref.Save();
}

Unity 3D | Aiming Down Sight's

I have my script that I created, but when I right click everything goes well, it plays the aim animation and stop when it ends, but when you release it, it re play's the animation. Can anyone help?
#pragma strict
function Update () {
if (Input.GetMouseButtonDown(0)) {
animation.Play("Shotgun_Shoot");
}
if (Input.GetMouseButton(1)){
animation.Play("Shotgun_Aim");
}
else if(Input.GetMouseButtonUp(1)){
animation.Rewind("Shotgun_Aim");
}
}
I believe it is because of
else if(Input.GetMouseButtonUp(1)){
animation.Rewind("Shotgun_Aim");
}
This happens when the user releases the button
It says in the Unity API for this function
Returns true during the frame the user releases the given mouse button.
You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has pressed the mouse button and released it again. button values are 0 for left button, 1 for right button, 2 for the middle button.
So you don't want that last else if
EDIT:
How about telling the animation also to stop if the mouse buttons are released?
Animation.Stop -- Stops all playing animations that were started with this Animation.
Stopping an animation also Rewinds it to the Start.

How do you make an AuGraph restart a audio file from the start of the file?

I am working with Apple's MixerHost application. It is a great example of how to set up an AuGraph, however the stop button is really a pause button. When you hit play it continues playing the sound files from the last position. I want to have a true stop button that causes the play head to move back to the start. I looked up AuGraph in apple's docs but I do not see anything about starting the song over without having to go through the process if creating a brand new graph, and I would prefer not to have to do this since i have a large loading delay.
Figured it out. This was my method
for (int audioFile = 0; audioFile < NUM_FILES; ++audioFile) {
soundStructArray[audioFile].sampleNumber = 0;
}