1
\$\begingroup\$

I have three scripts:

Level - Represents a Tile Based level.

LevelEditor - Custom Editor for Level.

Tile - A non monobehaviour class for storing tile information.

Level contains an array of Tiles to be allocated with 'new'. To avoid putting 'If array != null' everywhere in the code, I want to initialize the array simply to a size of 0 before anything else is ran.

However, I do not want this initialization to happen at run time, just only for editing purposes.

Is there some event I can use that will run before Level.OnDrawGizmos(), and also not be part of runtime code? Or is LevelEditor.OnEnable() the best I can do, with some checks in Level.OnDrawGizmos()?

\$\endgroup\$
1
\$\begingroup\$

If you want to initialize data, and for it to be available in the Editor, take a look at ExecuteInEditMode.

So you could add a new script component to your Level object, and have something like this:

#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class EditorRefresh : MonoBehaviour {

    void Update()
    {
        if (array == null)
        {
            InitArray();
        }
    }
}

#endif
\$\endgroup\$
  • \$\begingroup\$ Well I suppose it doesn't really reduce the overall amount of code running, but if that's not possible, then this is good enough. Thanks. :) \$\endgroup\$ – John Aug 14 '15 at 10:28
  • \$\begingroup\$ After trying this out, it didn't work, it still works in play mode, it'll only work correctly when the game is built. This is because play mode is still considered the Unity Editor. After some more research based on this, it seems there is no way to detect that you're in play mode, that I've found anyway. I'll just settle with the ways things are, it's not too bad. \$\endgroup\$ – John Aug 14 '15 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.