- Home /
Missing textures & materials in asset bundle
I am creating an asset bundle which contains a variety of materials, textures, gameobjects, meshfilters and meshrenderers. I am doing so using an editor script which is run from a command line. When I loop over and check the materials on the main asset before creating the bundle everything looks fine.
logFile.WriteLine("@@@@ BEGIN MAIN SCENE CHECK @@@@"); GameObject mainGO = sceneDummy.m_obj as GameObject;
MeshRenderer[] meshRenderArray = mainGO.GetComponentsInChildren<MeshRenderer>(true);
foreach (MeshRenderer mr in meshRenderArray)
{
logFile.WriteLine("Found MeshRenderer " + mr.name);
foreach (Material m in mr.materials)
{
if (m.shader == null)
{
logFile.WriteLine("Error diffuse shader missing from mat " + m.name);
}
else if (m.GetTexture("_MainTex") == null)
{
logFile.WriteLine("Error diffuse material missing from mat " + m.name);
}
else
{
logFile.WriteLine("Found material " + m.name);
}
}
}
logFile.WriteLine("@@@@ END MAIN SCENE CHECK @@@@");
UnityEngine.Object[] allAssets = (UnityEngine.Object[])assetList.ToArray(typeof(UnityEngine.Object));
BuildPipeline.BuildAssetBundle(sceneDummy.m_obj, allAssets, bundleTarget, BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies);
The log for this seems to indicate everything is fine. However when I load the main asset back in in my unity application using the following code.
void CreateModel(AssetBundle bundle) { bundle.LoadAll(); GameObject model = (GameObject)Instantiate(bundle.mainAsset); GameObject mainAssetGO = bundle.mainAsset as GameObject; m_models.Add(model);
Debug.Log("CreatingModel");
MeshRenderer[] meshRenderArray = mainAssetGO.GetComponentsInChildren<MeshRenderer>(true);
foreach (MeshRenderer mr in meshRenderArray)
{
Debug.Log("Found MeshRenderer " + mr.name);
foreach (Material m in mr.materials)
{
Debug.Log("Found material " + m.name);
Texture2D tex = m.GetTexture("_MainTex") as Texture2D;
if (tex != null)
{
Debug.Log("Found texture " + tex.name);
}
else
{
Debug.Log("Missing diffuse texture");
}
}
}
gameObject.GetComponent<MouseOrbit>().target = model.transform;
}
Whether I look at the instantiated object or not all the materials & textures appear to be missing. I have tried directly adding the textures and materials to the assetList but this had no effect.
What am I missing that is stopping the textures from being exported correctly?
Answer by Ricardo Arango · Jul 02, 2010 at 11:27 AM
I can build and load with no problems using this:
Create the bundle:
BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/testAsset.prefab"), null, "Assets/testAssetBundle.unity3d", options);
Instantiate in another project:
WWW www = new WWW (pathToBundle);
yield return www;
Instantiate(www.assetBundle.mainAsset);
Your code is almost correct, but you have to change
foreach (Material m in mr.materials)
to
foreach (Material m in mr.sharedMaterials)
As it will throw an error in Unity and disconnect the materials from the mesh, preventing a correct build.
$$anonymous$$aking these changes reduced caused a number of material linkages to survive. Using an updated asset from the artist resolved the remainder though I am not sure why.
One thing still puzzles me, why the thrown error is not caught either by my script (which is wrapped in a try/except and logs the error) or by the command line output.