Search Unity

How to make a rendering manager in Unity?

Discussion in 'Scripting' started by NicholasFrancis, Jul 16, 2013.

  1. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Ok, so I've now tried a bunch of different things, and I've come to an impassé: What is the best way of creating a manager in Unity?

    Let's say (for the sake of argument) that I'm making my own HaloManager. It builds a mesh of halos and renders them use Graphics.DrawMesh. I have some halos in the scene that adds themselves to the HaloManager in OnEnable. The HaloManager is a component living on some game object.

    Idea 1: use LateUpdate [ExecuteInEditMode]
    This more-or-less works, except:
    1a) When the scripts get reloaded, no call to LateUpdate is being made. So until I move some object (or otherwise change the scene), I get no halos on-screen.
    1b) When the game is paused, no calls to LateUpdate is being done either, so any tweaks done during a paused game don't get shown by my manager until I resume playing.

    Idea 2: Get the initial update for the halos by using HaloManager.OnEnable
    Basically I would introduce void OnEnable () { LateUpdate (); } into my HaloManager. Use Script Execution order to make sure that my Halo_OnEnable was called before HaloManager.OnEnable. Helps, but:
    2a) Script Execution order is not always respected (works when entering play mode, not when changing a script - so HaloManager.OnEnable does not know about all the lamps yet.
    2b) It doesn't solve problem 1b

    Ok. Time to get creative. Idea 1 works fine in Player builds - let's just hack around in the editor.

    Idea 3: Use EditorApplication.Update
    #ifdef UNITY_EDITOR
    using UnityEditor;
    #endif

    class HaloManager
    {
    #ifdef UNITY_EDITOR
    void OnEnable () { EditorApplication.update += Update; }
    #endif
    }

    This actually works and does what I need it to, but:
    3a) I'm updating Halo's every 100th / sec. In the real scenario, this actually has a performance degradation on my editor. I could get really clever and do dirty-tracking, but it seems overkill to have to do that just to get my editor to behave.
    3b) The code is super ugly. I could hide this in a superclass, but is this really necessary?

    Surely, there must be some better way of doing this. Any ideas?