i want to clear answer for to pause,resume and save coroutines (or) gamestate…already i have googled,am i got some information from this link…forum.unity3d.com/threads/26199-Saving-and-loading-a-coroutine-state
here i got only the explanation and i want to know how i want to start game again using resume…
if i click “pause” button, i have stop all the coroutines from that state using “StopAllCoroutines”…
but i dunno how to start that means resumes the game again… can any one give correct way or idea??
i have already tried with “Time.timeScale” its pauses correctly,but when i resumes the game it’s resuming very fast…that means game is loading very quickly…how to stop it also??
Well, i have a GameManager script you can take a look at, and it works fine for pausing and unpausing, and even has a pause menu GUI
public var pause = false;
var player : Transform;
var Player : Transform;
var fullscreen : boolean;
var Pauseskin : GUISkin;
static var mouseLookArray : List.<MouseLook> = new List.<MouseLook>();
function Awake () {
fullscreen = Screen.fullScreen;
if (player == null GameObject.FindWithTag("Player"))
player = GameObject.FindWithTag("Player").transform;
if (Player == null GameObject.FindWithTag("MainCamera"))
Player = GameObject.FindWithTag("MainCamera").transform;
}
function Update () {
if (Input.GetButtonDown("Pause")) {
if (pause == false) {
pause = true;
}
}
if (pause == false) {
SendMessage("Unpause");
}
else if (pause == true) {
SendMessage("Pause");
}
if (fullscreen == false) {
Screen.fullScreen = false;
}
else if (fullscreen == true) {
Screen.fullScreen = true;
}
}
function OnGUI () {
if (pause == true) {
GUI.skin = Pauseskin;
GUI.Box (Rect (-5,-5,Screen.width + 10,Screen.height + 10), "Pause");
if (GUI.Button (Rect(Screen.width / 2, Screen.height / 2 + 30,75,50),"Exit")) {
Application.Quit();
}
if (GUI.Button (Rect(Screen.width / 2, Screen.height / 2 - 30,75,50),"Resume")) {
pause = false;
}
GUI.BeginGroup (Rect (Screen.width - 205, 5, 200, 300));
GUI.Box (Rect (0,0,200,300), "Display Settings");
fullscreen = GUI.Toggle (Rect (15,25,150,20), fullscreen, " Full Screen");
GUI.EndGroup ();
}
}
function Pause () {
for (var go : MouseLook in mouseLookArray) {
go.enabled = false;
}
Time.timeScale = 0;
Screen.lockCursor = false;
}
function Unpause () {
for (var go : MouseLook in mouseLookArray) {
go.enabled = true;
}
Time.timeScale = 1;
Screen.lockCursor = true;
}
static function AddToList(script : MouseLook){
mouseLookArray.Add(script);
}
some of it only works on my game, though, like the last function, and the stuff regarding the mouse look array, but it gives you an idea. hope it helps!!!
@The OP: I’m not aware of any built-in ‘pause/resume coroutines’ functionality in Unity. There’s always the possibility it’s there and I’ve overlooked it, but a quick search of the docs and the forums doesn’t seem to turn anything up.
If you search the forums for (e.g.) ‘pause and coroutine’, you can find some examples showing (I think) how ‘pausing’ of coroutines can be achieved. In general though, I think these methods involve adding code on a per-coroutine basis (or other similar workarounds).
Personally, I haven’t used coroutines in any of my projects for this very reason (that is, they don’t play nice with my ‘pause’ system). Many folks around here are strong advocates of coroutines though, and I’m sure there are solutions to the ‘pausing of coroutines’ problem. (Perhaps someone who uses coroutines regularly and also has a functioning ‘pause’ system can comment on this topic…)
@JM Studios , i’m using coroutines in my script…here i couldn’t find how to pause all coroutine process…i have already used for pause using “StopAllCoroutines”…but how can i resume it again??
@Jesse Anders , if there is means can you give me any examples or links ??
maybe you could somehow record all of the currently running coroutines, and then put them in something, and then tell your script to resume the coroutines that you have saved that you were running?
I don’t have the links handy, but everything I found, I found by searching the forums for ‘pause AND coroutines’, so if you perform the same search you should find the same things I did.
Really? I would have never guessed that. I’ve always considered you and Eric5h5 two of the most knowledgeable members of the community; funny that you have completely opposite opinions on coroutines
I don’t think I’d be able to live without my WaitForSeconds yields.
No, thats what makes a coroutine wait at all. no yield no wait, then it will eat up 100% of the engines thread time and nothing else will be processed at all anymore.
(yield is why you use coroutines in first place, to be able to make them wait for a condition to be met without freezing the whole application :))
We don’t actually have opposite opinions on them. The reasons I haven’t used them much are more circumstantial than ideological. Specifically, I put together a ‘pause’ system that I liked, but that couldn’t accommodate coroutines in an obvious way. I know there are ways to effectively pause coroutines, but all the methods I’ve come across or can think of require adding support code of some sort to each coroutine. (Cue Eric to come along and tell us how coroutines can be paused without doing that
Anyway, I fully support them in theory I just haven’t yet found a way to integrate them cleanly and transparently into my existing ‘pause’ system.
Ahh, I think I see what you mean. Do you mean like, needing to code a few lines in the beginning of a coroutine to make sure that whatever the script needed to satisfy to start this coroutine is still satisfied? That’s always been my biggest peeve with them, but i think its unavoidable.
For instance, I have a basic finite state machine with an ienumerable with a few different states, and I do a bunch of checks to see which state the AI should be in, set the state, then do a switch on the state and perform whatever action is associated with that state (a coroutine, like (yield Wander(), yield Chase(), yield Attack(), etc). But inside each of those coroutines, I have to basically do the same check I did that got me in this state in the first place, to determine when I should return and get out of it.
I wish I could just somehow have the check I originally did running simultaneously the whole time, and could switch the states even when the AI is in one of the coroutines associated with a different state.
Yeah, something like that. More generally, it seems that to make a coroutine pausable, there’d need to be some code in the coroutine to facilitate that. (Or at least it seems that way to me - I could be missing something though.)
Testing it right now, it appears that WaitForSeconds() DOES pause when Time.timeScale is 0. So there doesn’t seem to be any need for trickery in handling pause menus (although it means you literally can’t use coroutines while the game is paused since none of the yield functions will ever end)…
Is this a new thing for Unity 3.0, or am I missing something?
You can still use coroutines, just don’t use anything that is time based. If you simply yield the loop will still run with every frame update. You will need to keep track of time yourself using Time.realtimeSinceStartup as that keeps counting even when Time.timeScale is set to zero. Here a coroutine I use for animating a pause menu when the timeScale is zero:
private function showAnimation():IEnumerator {
var duration:float = 1;
var lastTime:float = Time.realtimeSinceStartup;
for (i = 0.0; i < 1.0; i += (Time.realtimeSinceStartup - lastTime) * (1/duration)) {
var t:float = Mathfx.easeOutElastic(.6, 1, i);
// Update stuff here
lastTime = Time.realtimeSinceStartup;
yield;
}
}
I have some gui effects when I pause the game and other stuff that needs the timeScale to be 1 so I just do this ‘do while’ inside the coroutines before any important thing that can’t be running while the game is paused (like an attack for example, in my case is some tools uses for my farming game) and that’s it.