Why Resources.Load does not works in Build?
Hello there. I just trying to load different types of resources using Resources.Load method. In Editor it works fine but when I make some build (windows standalone or iOS) - Resources.Load every time returns Null and I just cant load any resource. Why it so? How to fix it? Here is more info. I wrote a code which shows me if the resource was downloaded or not.
BGArt = (Sprite)Resources.Load(app.model.pathTrialImages+BackgroundArt, typeof(Sprite));
if (i < 5)
{
if (!BGArt)
Debug.LogError(app.model.pathTrialImages + BackgroundArt + " \t value: " + BGArt);
var bga = Resources.Load(app.model.pathTrialImages + BackgroundArt, typeof(Sprite));
Debug.LogError("---var---: " + app.model.pathTrialImages + BackgroundArt + "\t value: " + bga);
i++;
}
And here is Debug.Log in Editor all right... But in builded EXE..
As you can see, in build assets just don't uploads
About TextAsset - early I could not download it, but this code fixed it.
TextAsset ta = (TextAsset)Resources.Load("data", typeof(TextAsset));
string data = ta.text;
But with rest of assets it did not help.
About different types of Resources.Load - I just try all (or not all?) types of that method. See code below
BGArt = (Sprite)Resources.Load(app.model.pathTrialImages+BackgroundArt, typeof(Sprite));
if (i < 10)
{
Debug.LogError("-----I: " + i + "----- START");
Debug.LogError("Type name = (Type)Resources.Load(path, Type)\t value: " + BGArt);
var bga = Resources.Load(app.model.pathTrialImages + BackgroundArt, typeof(Sprite));
Debug.LogError("var name = Resources.Load("+ app.model.pathTrialImages + BackgroundArt + ", Type) \t value: " + bga);
var bga1 = Resources.Load<Sprite>(app.model.pathTrialImages + BackgroundArt);
Debug.LogError("var name = Resources.Load<Type>("+ app.model.pathTrialImages + BackgroundArt + ") \t value: " + bga1);
Sprite bga2 = Resources.Load<Sprite>(app.model.pathTrialImages + BackgroundArt);
Debug.LogError("Type name = Resources.Load<Type>(" + app.model.pathTrialImages + BackgroundArt + ") \t value: " + bga2);
Sprite bga3 = Resources.Load<Sprite>(app.model.pathTrialImages + BackgroundArt+".jpg");
Debug.LogError("Type name = Resources.Load<Type>(" + app.model.pathTrialImages + BackgroundArt + ".jpg" + ") \t value: " + bga3);
Debug.LogError("-----I: " + i + "----- END\n");
i++;
}
So I'm trying to download batch of sprites, and for first 10 of them I do Debug.Log(). Here is log for first three sprites
-----I: 0----- START
Type name = (Type)Resources.Load(path, Type) value:
var name = Resources.Load(images/iphone/iphone_6/game/trial/pg1pt01, Type) value:
var name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg1pt01) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg1pt01) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg1pt01.jpg) value:
-----I: 0----- END
-----I: 1----- START
Type name = (Type)Resources.Load(path, Type) value:
var name = Resources.Load(images/iphone/iphone_6/game/trial/pg2pt01, Type) value:
var name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg2pt01) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg2pt01) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/pg2pt01.jpg) value:
-----I: 1----- END
-----I: 2----- START
Type name = (Type)Resources.Load(path, Type) value:
var name = Resources.Load(images/iphone/iphone_6/game/trial/previous kept, Type) value:
var name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/previous kept) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/previous kept) value:
Type name = Resources.Load<Type>(images/iphone/iphone_6/game/trial/previous kept.jpg) value:
-----I: 2----- END
All Resources,Load() returns Null.
Going to need more information. How about some sample code? And show how the resource appears in the project hierarchy.
I have Resources folder in Assets folder. In Resources folder I have "data.txt" file, but I see only the name - "data". Code looks simple TextAsset textAsset = Resources.Load("data") as TextAsset; //also I have try TextAsset textAsset = Resources.Load("daya");
All that code works fine in Editor, but not in builded app.
Can you make a new project that just has a Resources/data.txt and a script to load it and see if that works? If it doesn't, upload your small project somewhere so we can see it.
You could also put the txt file in Strea$$anonymous$$gAssets and load it with System.IO.File.
What if you use the generics form of the function...
Sprite sprite = Resources.Load<Sprite>("your/sprite/path");
Also this...
var bga = Resources.Load(...
The best the compiler can tell about the data type for bga
is that it's a Object, which is the data type returned by the function. Either declare the variable using the data type, or again use the generics version. Not sure why it would behave differently in different environments... regardless, I always prefer explicitly declaring the variable type when it's ambiguous like this. So this...
var bga = Resources.Load<Sprite>("your/sprite/path");
will make sure the compiler knows bga
is a Sprite and not just an Object. I don't know if that will make any difference in what you're seeing though, but I don't really see anything else that's odd.
And you said something about a Text asset?? I don't see anything like that in your code.
have you tried including the file extension? Try "data.txt" ins$$anonymous$$d of just "data". You can also try different overloads that .Load provides, Dave mentions this in more detail.
In your updated code, you're always logging bga
, not bga1, bga2, etc.
.
Also, if the variable were actually null, wouldn't you be getting a null reference exception on your log messages ins$$anonymous$$d of an empty string?
By default, logging the way you are is going to use the ToString() method, which for Object returns the object's name
property. I suppose it's possible that property is an empty string? Try logging .name directly ins$$anonymous$$d. or bga.GetType() or something.
Answer by ChazAshley1 · Oct 23, 2015 at 02:56 PM
I have fixed it already. The problem was in folder with sprites.
I have had next folder hierarchy:
- Assets
- Resources
- images
ipad
iphone
- images
- Resources
Ipad and Iphone folders just contain the same sprites but with different size. And when I have deleted Ipad folder all works fine.
Why? I dont know.
Your answer
Follow this Question
Related Questions
Files put inside a Resources folder still don't end up in the build 1 Answer
why the Apk file size is too larger after build........? 2 Answers
Arguement Out Of Range (ONLY IN BUILD) 1 Answer
Why is hard drive space being used when creating an iOS project onto an external drive? 1 Answer
How to fix editor freezing after code change? (during build) 0 Answers