- Home /
Assets aren't found at runtime
I have this method :
void setTexture(string pngName){
transform.renderer.material = (Material)Resources.Load ("GeneralBuildingMaterial");
Texture myTexture =(Texture) Resources.LoadAssetAtPath("Assets/Misc/Images/"+pngName+".png", typeof(Texture));
transform.renderer.material.mainTexture = myTexture;
}
It works perfectly inside of Unity, but if i want to build and run( .exe or in the webplayer), things that would normally use those assets become white. Do you guys happen to have encountered this error? Thanks a lot for taking your time to help me!
EDIT : Materials that don't have textures that change over time don't encounter this issue, only the objects that use this material for which I change the texture every now and then.
EDIT : I usually export in the main folder of the project, is there a particular place where you have to export?
Answer by DaveA · Aug 11, 2013 at 01:03 AM
I always make a Build folder at the same level as Assets, and build into there.
Can't you put your PNG's into the Resources folder? Then you don't need to prepend a path. Because that's how Resources works: it loads from the Resources folder. If you want to load as you do here, you may instead want to use WWW and a file:// path, but that will be relative to a) Assets in the Editor, or b) the program_Data folder when built (runtime). And the PNG's don't auto-copy to your build _Data folder either. For that I use a script like this (put into Editor folder):
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System;
using System.IO;
using System.Collections.Generic;
public static class PostBuild
{
[PostProcessBuild]
static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
string path = Path.GetDirectoryName (pathToBuiltProject);
string exe = Path.GetFileNameWithoutExtension (pathToBuiltProject);
FileUtil.CopyFileOrDirectory ("Assets/Textures/colors.png", path+"/"+exe+"_Data/colors.png");
}
}
Hello !Thanks for your answer. If you just use Resources.Load can you still make folders inside the Resources folder, loading some of them like this Resource.Load("Images\myImage.png")? Or all of them have to be Resource.Load("myImage.png"), having to have a mess containing all your files?
Your answer
Follow this Question
Related Questions
into which folder can i import my jpeg files so i can put some texture on a wall obj? 0 Answers
ridiculous load time using Resources.Load() on android build 0 Answers
Set texture / EnableKeyword not working in build 1 Answer
Resources folder fails to load 2048 texture in standalone 0 Answers
Resources.Load(path+name) as texture == null reference 7 Answers