1

Anyone knows how to use Resources.LoadAll or another method like this, in EditorWindow?

I have that error: LoadAll can only be called from the main thread.

Generaly I create node editor, where i need all items in my resources.

enter image description here


This is how I use my static method:

foreach (var item in DataManager.Items.All<Food>())
{
      _foodItems.Add(item.Name);
}

This is my static method:

internal static IEnumerable<T> All<T>() where T : BaseItem
{
       return Resources.LoadAll<T>(itemsPaths[typeof (T)]);
}

BaseItem is public abstract class with some public variables.

public abstract class BaseItem : Datablock
{
     #region Data

     /// <summary>
     /// Game item name.
     /// </summary>
     public string Name;

Ohhh and QuestTaskNode class where I want use Resources.LoadAll inherits from ScriptableObject

1
  • Everything here is wrong. You can't use statics at all in Unity. Unity is an ECS system and has no connection at all to OO. You are simply writing components which are attached to "GameObject" items. Commented Jul 10, 2016 at 12:20

1 Answer 1

1

Bad:

Class BadCode{
  Resources.LoadAll(....);
}

Bad:

Class BadCode{
    BadCode(){
     Resources.LoadAll(....);
   }
}

Good:

Class GoodCode{

  void putUnityAPIInsideAFunction()
  {
      Resources.LoadAll(....);
  }
}

Just put the Unity API code inside a function.Also don't put it in a class constructor that inherits from MonoBehaviour. You will get the-same error because these are not called on the main Thread.

Sign up to request clarification or add additional context in comments.

4 Comments

Already i have static method: internal static IEnumerable<T> All<T>() where T : BaseItem { return Resources.LoadAll<T>(itemsPaths[typeof (T)]); } And i used it like this: foreach (var item in DataManager.Items.All<Food>()) { _foodItems.Add(item.Name); } Sorry, i'm new and i don't know how corrent use formatting here.
I inherits from ScriptableObject.
Edit your question and add the code there. Also add the BaseItem class. You need to add something I can use to find out what's going on. Also make sure to add the programming language to your question in the future
Ok sorry for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.