I am making a game now. I have also make a save game and load game.
What i want is when i load the game (this mean code to load) i will show loading progress until code load execution is finish.
For example (LoadGame.cs):
using UnityEngine;
using System.Collections;
public class Load_Timer_Merchant : MonoBehaviour {
CloudSaver CloudSave;
// Use this for initialization
void Start () {
CloudSave = GameObject.FindGameObjectWithTag("CloudSave").GetComponent<CloudSaver>();
CloudSave.Load_Timer_Merchant ();
}
// Update is called once per frame
void Update () {
}
}
I want to make the progress loading of that file script LoadGame.cs
Is that possible to do it ?
Thanks
Dennnis
You have required Co-routine to do this task
For instance, You want to log successful text as image load from a url something like this
IEnumerator DoImageLoad()
{
WWW txTexture = new WWW(m_szTexturePath);
yield return txTexture;
renderer.material.mainTexture = txTexture.texture;
Debug.Log("Image Loaded. This message will show as image loaded");
}
Start(){
StartCoroutine("DoImageLoad");
}
Related
When I start to play , it can't jump to the next scene .It also pumps out many error ,one error repeats many times , Coroutine couldn't be started because the the game object 'FadeOut' is inactive!It made me confused since the rawimage is all black and must be inactive before the function onclick.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ToLoading : MonoBehaviour
{
public Button toLoad;
public RawImage fadeOut;
public Text loading;
// Start is called before the first frame update
void Start() //need to SetActive(false); ? if I set it false already in unity
{
}
// Update is called once per frame
void Update()
{
ButtonClicked();
}
public void ButtonClicked() //
{
fadeOut.gameObject.SetActive(true);
loading.gameObject.SetActive(true);
StartCoroutine(ToNextScene());
}
//going to do: fade out> 2 secoonds > to next scene
private IEnumerator ToNextScene()
{
yield return new WaitForSeconds(2);
SceneManager.LoadScene("SceneForMovingAround");
}
}
My question is : Should I put this script ToLoading to the fadeout rawimage ,or create an empty gameobject then put the script to it .Also , how to solve the problem -fade out error.And last , will it a problem about the code of scene part ?all of your help is appreciated, thanks!
Your ButtonClicked() method is executed every frame, which causes the unwanted behaviour your describe.
Remove the entire Update() method with its body from your file and run your game again.
Add gameObject.SetActive(false); to your ButtonClicked() method to deactivate the button upon clicking.
If you have attached the ButtonClicked() method to the OnClick() event in the inspector as you show in your screenshot then it should work as you would expect.
I cannot find anything in docs that breaks down the scene loading process and how the Scene.isLoaded property is determined.
I need to mock large scene loading and cannot find a way to delay a scene's loading. It would be useful to understand the flow especially when needing to load external assets in the scene and only mark the scene as loaded via code.
You should have a look at SceneManager.LoadSceneAsync and especially allowSceneActivation which allows you to do exactly that: Delay a scenes loading and e.g. display a loading screen meanwhile.
Example from the docs:
// This script lets you load a Scene asynchronously.
// It uses an asyncOperation to calculate the progress and outputs
// the current progress to Text (could also be used to make progress bars).
// Attach this script to a GameObject
// Create a Button (Create>UI>Button) and a Text GameObject (Create>UI>Text)
// and attach them both to the Inspector of your GameObject
//In Play Mode, press your Button to load the Scene, and the Text
// changes depending on progress. Press the space key to activate the Scene.
//Note: The progress may look like it goes straight to 100% if your Scene doesn’t have a lot to load.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AsyncOperationProgressExample : MonoBehaviour
{
public Text m_Text;
public Button m_Button;
void Start()
{
//Call the LoadButton() function when the user clicks this Button
m_Button.onClick.AddListener(LoadButton);
}
void LoadButton()
{
//Start loading the Scene asynchronously and output the progress bar
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
yield return null;
//Begin to load the Scene you specify
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Scene3");
//Don't let the Scene activate until you allow it to
asyncOperation.allowSceneActivation = false;
Debug.Log("Pro :" + asyncOperation.progress);
//When the load is still in progress, output the Text and progress bar
while (!asyncOperation.isDone)
{
//Output the current progress
m_Text.text = "Loading progress: " + (asyncOperation.progress * 100) + "%";
// Check if the load has finished
if (asyncOperation.progress >= 0.9f)
{
//Change the Text to show the Scene is ready
m_Text.text = "Press the space bar to continue";
//Wait to you press the space key to activate the Scene
if (Input.GetKeyDown(KeyCode.Space))
//Activate the Scene
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}
}
Also checkout sceneLoaded: as stated in the docs
Add a delegate to this to get notifications when a Scene has loaded.
This happens after the scene has been loaded and afaik after the Awake calls finished. You can easily test this by using a script like
public class Test : MonoBehaviour
{
private void Awake()
{
Debug.Log("Awake");
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("OnSceneLoaded");
}
}
You will see both the
Awake
OnSceneLoaded
debugs in the console meaning that Awake was called before OnSceneLoaded.
→ At this moment I would also expect the Scene.isLoaded to be set to true.
I have created a vr app in unity using google vr sdk. Currently i could click on the screen to start the video. When i use the vr headset, i need to use the bluetooth controller to play/stop the video. Can someone help me do this?
You need to identify how unity map your controller buttons, once you identify what button do you want to press and how unity mapped in his Input Manager (Edit->Project Settings->Input) you just need to call you function from the Update like this:
void Update()
{
if(Input.GetButtonUp("Fire1"))
{
playVideoFuncion();
}
}
Where playVideoFunction() is your own fuction.
In this example I used "Fire1" but maybe in your case is different.
For example for the Xbox Controller you have this configuration explained in Xbox 360 Controller Input on Unity
If you can't find anything related on your controller you can do something like:
void Update()
{
if(Input.GetButtonUp("Fire1"))
{
Debug.Log("Fire 1 Pressed");
}
if(Input.GetButtonUp("Fire2"))
{
Debug.Log("Fire 1 Pressed");
}
if(Input.GetButtonUp("0"))
{
Debug.Log("Button 0 pressed");
}
// Add more buttons and logs
}
There are maybe other ways to identify the input from random controllers but I don't know how. I needed the mapping for the Xbox controller and that page was useful.
You can create a script and attach to the video player so that when a user clicks on the player the commands are run. I assume you already have the unity package for Google VR(GVR). Add the following sample script and modify to suit your need.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VideoPlayer : MonoBehaviour {
private bool _isPlaying;
// Use this for initialization
void Start () {
_isPlaying = false
}
// Update is called once per frame
void Update () {
if (GvrController.ClickButtonUp && _isPlaying) {
PlayVideo();
}
else if (GvrController.ClickButtonUp && !_isPlaying){
StopVideo ();
}
}
public void PlayVideo{
//Logic to Play Video
}
public void StopVideo{
//Logic to Stop Video
}
}
I want to put loader in between dialog boxes come up for the purchase. What is the way for this?
Because when game player press Buy button, he should require to wait for 5 to 10 second depends on internet speed and server response and this process happed 2 to 3 times because multiple dialogs come up within screen.
So in this case, may be player can leave the screen. I want to put the loader so that game player realise that some processing is running in background, he required to wait for some time.
At present I was following completely this code for Unity IAP setup.
Integrating Unity IAP In Your Game
I assume this is for mobile platform but even if its not still the following can be considered:
Simple solution is to create a full screen Image (UI/Panel) object in your UI to block clicks. I would use Animator component (with triggers) to display this panel in front of other UI when there is a background process running.
public class Loader : MonoBehaviour
{
public static Loader Instance;
Animator m_Animator;
public bool Loading {get; private set;}
void Awake()
{
Instance = this; // However make sure there is only one object containing this script in the scene all time.
}
void Start()
{
//This gets the Animator, which should be attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
Loading = false;
}
public void Show()
{
Loading = true;
m_Animator.SetBool("Loading", Loading); // this will show the panel.
}
public void Hide()
{
Loading = false;
m_Animator.SetBool("Loading", Loading); // this will hide the panel.
}
}
Then in any script which manipulates UI:
public void BuyButtonClicked()
{
Loader.Instance.Show();
// process time taking stuff
Loader.Instance.Hide();
}
You can also create any kind of loading animation as child of panel object using simple images and animation tool inside Unity (for example rotating animation (use fidget spinner, its cool)).
And in case of Android where user have option to leave screen by pressing OS back button you can prevent going back by checking if any loading is in progress by following example:
// code for back button
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
BackButtonPressed();
}
}
void BackButtonPressed()
{
if(Loader.Instance.Loading)
return;
// use back button event. (For example to leave screen)
}
Hope this helps ;)
This is only related to Unity3D, the game engine.
So let's say I have one line of code, and then another. How would I make a delay between those two lines of code. To repeat, I'm also doing this for Unity3D.
I've looked everywhere, but nothing will work.
As a FRAME-BASED game engine, Unity of course, obviously, has every imaginable sort of timer and run loop control built-in.
It's absolutely trivial to make timers ...
Debug.Log("Race begins!");
// 3 seconds later, make the crowd cheer
Invoke("CrowdCheers", 3f);
// 7 seconds later, show fireworks
Invoke("Fireworks", 3f);
private void CrowdCheers()
{
crowd.Play();
}
private void Fireworks()
{
fireworks.SetActive(true);
}
If you are more advanced with coding, you can use "coroutines". Coroutines are a ridiculously simple way to access the run loop. Examples:
using UnityEngine;
using System.Collections;
public class demo : MonoBehaviour {
// Details at: http://docs.unity3d.com/Manual/Coroutines.html
// Use this for initialization
void Start () {
// Some start up code here...
Debug.Log("Co-1");
StartCoroutine("OtherThing");
Debug.Log("Co-2");
}
IEnumerator OtherThing()
{
Debug.Log("Co-3");
yield return new WaitForSeconds(0f);
Debug.Log("Co-4");
DoOneThing();
yield return new WaitForSeconds(1f);
Debug.Log("Co-5");
DoOtherThing();
}
void DoOneThing()
{
Debug.Log("Co-6");
}
void DoOtherThing()
{
Debug.Log("Co-7");
}
}