- Home /
When generating prefab, i cant load images from either resource or assetbundle
I want to generate prefabs for later use, because I have a lot of images to be used as textures. I tried almost every method (useful or not) online but still cannot figure it out. I have tried Resources.Load<Material>
or Resources.Load() as Material
, but all testing result returns null from Resources.Load
. I am sure that the path to the Resource folder is correct, and so is the filename. The code is as below:
GameObject quads_Axial = new GameObject();
string localPath2 = "Assets/Prefabs/Axial/brain-Axial.prefab"; //2
for (int i = 0; i < fCount2; ++i)
{
Material mat = Resources.Load("Textures/Axial/brain-Axial-" + (i + 1).ToString()) as Material;
if (mat == null) Debug.Log( (i + 1).ToString());
//mat.SetTexture("_MainTex", Resources.Load("Textures/Axial/brain-Axial-" + (i + 1).ToString()) as Texture2D);
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.position = new Vector3(0, half_scale - (float)i * 0.002f, half_scale);
quad.transform.Rotate(Vector3.right, 90.0f);
quad.transform.localScale = new Vector3(scale_size, scale_size, scale_size);
//quad.GetComponent<Renderer>().material.color = Color.red; //
quad.GetComponent<Renderer>().sharedMaterial = mat;
quad.transform.parent = quads_Axial.transform;
}
if (AssetDatabase.LoadAssetAtPath(localPath2, typeof(GameObject)))
{
if (EditorUtility.DisplayDialog("Are you sure?",
"The prefab already exists. Do you want to overwrite it?",
"Yes",
"No"))
{
CreateNew(quads_Axial, localPath2);
}
}
else
{
Debug.Log(quads_Axial.name + " is not a prefab, will convert");
CreateNew(quads_Axial, localPath2);
}
Hello, can anybody help me with this problem? Or some code that can create prefab programmatically? Anything is appreciated.
Answer by kayeejoe · Aug 02, 2018 at 01:38 AM
I figured it out in another way. My purpose was to load images at runtime, AND can run in as more UWP environment (such as Hololens Emulator or Hololens) as possible. But the system default folders are not same when the hardware is not same. Here let me introduce another way.
To be clearer, the Resouce.load
function will never return a file by using filename as its argument in the Editor mode. In fact, Resouce.load
method is somehow zigzag comparing with a batch 'dragging' method, which is an operation that allows you to drag multiple images (textures) to a script, in where you can use an array of textures to feed your gameObjects. If you do not understand what I am talking, please see this link: link text. Remember you need to lock the inspector where you will drag the images into!
Answer by Zhiyong_Chen · Jul 27, 2018 at 02:58 AM
Should localPath2 add .prefab?
Actually the prefab file was generated by this code, just without correct materials (sometimes pink or black). So I guess the problem would be I did not load the images correctly.