x


how to achieve a LateFixedUpdate effect

Hi, everyone, I was just wondering if there was a way to achieve a LateFixedUpdate() function effect, because I am trying to stay with fixed update for my game, and I need a late update function. All help is appreciated!!! :)

more ▼

asked Apr 21 '11 at 10:53 PM

zmar0519 gravatar image

zmar0519
946 59 66 78

I edited my post

Apr 24 '11 at 12:39 AM Peter G
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Here's an idea:

I do this with all my code. This is a little confusing if you don't program, but it is actually faster than having lots of Update() calls.

  1. I create an event manager, then have all then have all my class have a psuedo-update function that they add to the event. Then I fire the event and they all get fired.

  2. Unity is single treaded. So if you call a function Unity will finish it before it continues onto its next task. So if you have two events, the second one should be called after the first one is completely finished working similar to a LateFixedUpdate().

Here is a sample:

using System;
using UnityEngine;
using System.Collections;

public class TickManager : MonoBehaviour {

    public event Action tick;
    public event Action lateTick;

    // Use this for initialization
    void Start () {
         StartCoroutine(FixedTick());
    }

    IEnumerator FixedTick() {
         for(;;) {
             if(tick != null)
                  tick();
             if(lateTick != null)
                  lateTick();
             yield return new WaitForFixedUpdate();
         }
    }
}

This will create 2 events. Then you can have classes register to them.

using System;
using UnityEngine;
using System.Collections;

public class SomeClass : MonoBehaviour {

    // Use this for initialization
    void Start () {

        TickManager eventManager = (TickManager)FindObjectOfType(typeof(TickManager));
        tickManager.tick += new Action (Tick);
        tickManager.lateTick += new Action(LateTick);

    }

    void Tick () {
            //This will get called as frequently as FixedUpdate
    }

    void LateTick () {
            //This should be called after the first one.
    }
}

By the way, if someone more knowledgeable than myself can tell me that my understanding of Unity's threading is completely wrong then please do ;).

EDIT: I managed to get it working with javascript. The first code needs to be in C# because I don't believe you can do events in javascript. But the second code can be in js with the following syntax:

var tickManager : TickManager;

function Start () {
    //using function types.  Similar to an implicit delegate.
    tickManager.tick += Tick;
    tickManager.lateTick += LateTick;

    //Also supports anonymous functions
    tickManager.tick += function() {
             Debug.Log("Tick");
        };
}

function Tick () {
    Debug.Log("Fired on a FixedUpdate interval");
}

function LateTick () {
    Debug.Log("Called after the prior method");
}
more ▼

answered Apr 21 '11 at 11:51 PM

Peter G gravatar image

Peter G
15.1k 16 44 137

Your understanding of Unity's threading is completely wrong! ^.^ nah it looks good :)

Apr 22 '11 at 12:27 AM Joshua

Peter G, thank you so much, but would it be possible to get that code in js? if not, thats ok, seeing as some things cannot be done in js, but it would be nice to get it.

Apr 22 '11 at 01:11 AM zmar0519

Oh, another trick you can do. Create an interface that has these methods. Then have algorithm in your 1st script that finds instances of all classes that implement that interface. foreach(MonoBehaviour mB in the scene) { if( mB is IEventManager) { //add to list } }

Apr 22 '11 at 02:30 AM Peter G
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5273
x513
x79
x24

asked: Apr 21 '11 at 10:53 PM

Seen: 1441 times

Last Updated: Apr 21 '11 at 10:53 PM