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);
}