Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

I’ve recently come across what initially seems like a bug with the Unity 4 editor. it involves the use of the Resources.Load method from within a class marked InitializeOnLoad.

Take the fallowing example code …

namespace CBX.CoreProjectCode
{
    using UnityEditor;

    using UnityEngine;

    [InitializeOnLoad]
    public class EditorInitialization
    {
        static EditorInitialization()
        {
            LoadData(); // this will fail to see the test.txt file in the resource folder
        }     

        private static void LoadData()
        {
            // try to load data
            var data = Resources.Load("test", typeof(TextAsset)) as TextAsset;
            if (data == null)
            {
                Debug.LogWarning("No data found");
                return;
            }

            Debug.Log(data.text);
        }
    }
}

After Unity compiles the scripts it will invoke any static constructors of classes marked with InitializeOnLoad. But at this point I suspect that Unity has not yet identified assets you have within any Resources folders. When the class constructor calls the Resources.Load method it will fail to read the test.txt resource file and return a warning message out to the console.

What you could do at this point is right click the “Assets” folder in the project window and select “Reimport”. Unity will reimport the assets and again call classes marked with InitializeOnLoad. But this time Resources.load will successfully load the test.txt file resource and display it’s contents out to the Unity console window.

One work around to this is to hook into the EditorApplication.update callback and make a call to the LoadData method from there.  An example is provided below …

namespace CBX.CoreProjectCode
{
    using UnityEditor;

    using UnityEngine;

    [InitializeOnLoad]
    public class EditorInitialization
    {
        private static bool ranOnce;

        static EditorInitialization()
        {
            EditorApplication.update += RunCallbacks;
        }

        private static void RunCallbacks()
        {
            if (!ranOnce)
            {
                // this will see the test.txt file in the Resource folder and display
                // its contents
                LoadData();
                ranOnce = true;

                // return or don't it’s up to you
                return;
            }

            // do some work here
        }

        private static void LoadData()
        {
            var data = Resources.Load("test", typeof(TextAsset)) as TextAsset;
            if (data == null)
            {
                Debug.LogWarning("No data found");
                return;
            }

            Debug.Log(data.text);
        }
    }
}

Again I am unsure whether this is an actual bug or not but I have submitted a Unity bug report anyway just in case. Here is a link to the my bug report https://fogbugz.unity3d.com/default.asp?525005_eroka98ru2v4netm


Add comment



biuquote
  • Comment
  • Preview
Loading






Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume