I've got an exit button that loads from the main menu to the outro scene. The scene gets loaded but doesn't start playing (playonawake is on). I'm using a timeline in the outro scene that displays a few sentences before quitting completely.
This is how i load my outro:
public void Outro()
{
SceneManager.LoadScene("OutroScene");
}
Then in the outro slide this should load:
Timeline
But it doesn't start playing, it just shows the first sentence.
I was expecting that the timeline start automatically when the scene is loaded.
Related
When I start my game (regardless of a platform) I notice that all the scenes load in background directly after showing the splash screen. I know they're being loaded because once I try to load them from the menu they show up immediately. And it takes a lot of time to load right after startup.
However I only want to load the menu scene (title) first. And only after an option is selected I want to load the other ones.
In menu code I load scenes like this once a button is clicked.
StartCoroutine (LoadALevel ());
The coroutine method:
private IEnumerator LoadALevel()
{
async = SceneManager.LoadSceneAsync("space");
yield return async;
}
Any help is appreciated!
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. :)
Is there a way of returning back to my previous game application when I close unity/close game application on button press. Basically I have a scene with a button in it, if the player press the button, it will bring them to a new level (scene 1). This is where I want to save this scene (scene 2) using something like playerprefs to keep tabs on it so that if I close the game application, or close unity and then I re-open and play the application (regardless of what scene I am in) the game should automatically bring me back to scene 2. Is it possible to return to previous application (even if I close the application or game) on button click?
So if the button is clicked on, and I exit out of the application and or game, then re open it and play the game then it should automatically bring me back to the scene I exited out of (scene 2)
When ever application quit event is called, Save current scene as player preference.
using UnityEngine.SceneManagement;
void quitGame()
{
Scene currentScene = SceneManager.GetActiveScene();
PlayerPrefs.SetString("lastSceneName",currentScene.name);
Application.Quit();
}
above code will save the name of current scene at time of application quit.
Now when you restart your game and your scene(0) is loaded. call following method in Start() or Awake() function.
void loadLastScene()
{
if (PlayerPrefs.GetInt("isFirstTime") == 0) //this is to make sure this chunk of code doen't run on the very first usage.
{
PlayerPrefs.SetInt("isFirstTime", 1);
}
else if (PlayerPrefs.GetInt("isFirstTime") == 1)
{
SceneManager.LoadScene(PlayerPrefs.GetString("lastSceneName"));
}
}
Code not tested.
I'm having difficulty pausing the game when I leave and switch back to it.
I'm trying to pause the SKSpriteNode called main, which contains all my sprites, when the view returns from the background. In-game, I can touch the pause button and the game pauses, and the resume button and it resumes.
This is my code:
func didBecomeActive() {
println("didBecomeActive")
main.paused = true
}
The first time this runs is when the app opens for the first time, and everything is paused as it should be. The second time, is when it returns from the background, and suddenly all the animations (SKActions, particles, etc.) start working.
I've confirmed that the method is running, and I've also tried setting main.paused to false and then true, and even self.paused to true. Nothing works.
I'm completely stumped. Anyone know what the issue is here?
Setting self.scene.paused = YES should fix this. I have tried it with a game I am developing and it works fine.
Just set self.scene.paused = YES when the game enters the background, then when it return to the foreground, it should stay paused till you resume it, i.e. set self.scene.paused = NO.
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.