我一直试图解决这个问题好几天了,我不确定如何 . 首先,我正在创建具有预制件的AssetBundles和带有纹理的GameObjects . 在构建AssetBundle之后,它被上传到服务器,在另一个Android应用程序中,我检索它并卸载其资产 . 在Unity编辑器“游戏视图”中一切正常,但在Android手机上,动态游戏对象的纹理显示为黑色 . 静态纹理,就像我用于背景的纹理一样,通常出现在编辑器和Android设备上 . 看似黑色的纹理是我从AssetBundles中检索到的纹理 .
当我搜索它时,我发现了这些建议:
-
这可能与压缩有关 . 问题是,这些纹理是动态地从服务器上的AssetBundles中检索的 . 从捆绑包中卸载后,我无法使用建议的设置"build"项目 . 我尝试在将图像加载到AssetBundle之前自动将图像压缩到"Compressed ETC 4 Bits",但它没有解决问题 . https://forum.unity.com/threads/black-textures-on-some-devices-android-versions.195328/
-
我还尝试通过从AssetBundle加载纹理(和材质)并将其分配给加载的对象来手动完成,但它仍然只出现在编辑器中,而不是手机中 . https://answers.unity.com/questions/238109/problem-with-materials-after-creating-asset-bundle.html
-
我尝试将图像保存为精灵,然后再将其加载到AssetBundle . 在应用程序中,我尝试使用该精灵创建一个新的纹理,但这种方法也不起作用 . https://answers.unity.com/questions/651984/convert-sprite-image-to-texture.html
首先,我拖放纹理的图像,然后使用此函数自动压缩:
// Automatically Compress all imported textures to the project to ETC_RGBA
void OnPreprocessTexture(Texture2D t)
{
EditorUtility.CompressTexture(t, TextureFormat.ETC_RGBA8_3DS, TextureCompressionQuality.Normal);
}
然后我在编辑器中手动将纹理添加到3D GameObject并创建一个预制件并将GameObject添加到其中 . 我将预制本身,材料和图像添加到AssetBundle并执行此操作以导出AssetBundle :(现在AssetBundle总共包含3个资产 . 我将其上传到服务器)
//Creates a new menu (Build Asset Bundles) and item (Normal) in the Editor
[MenuItem("Assets/Build Asset Bundles/Normal")]
static void BuildABsNone()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundle", BuildAssetBundleOptions.None, BuildTarget.Android);
}
在应用程序中,我从服务器动态加载AssetBundle,我想使用Vuforia在ImageTarget上显示它 . 这是CloudHandler类中IEnumerator DownloadAndCache()函数的一部分,它加载材质资源并实例化GameObject "mBundleInstance" . 然后我在图像目标上显示"mBundleInstance":
bundle = www.assetBundle;
var materials = bundle.LoadAllAssets(typeof(Material));
containsMaterial = (materials.Length > 0);
if (AssetName == "")
{
mBundleInstance = Instantiate(getMainAsset(bundle)) as GameObject;
}
else
{
mBundleInstance = Instantiate(bundle.LoadAsset(AssetName, typeof(GameObject))) as GameObject;
}
Debug.Log(mBundleInstance.GetComponent<Renderer>().material.mainTexture.name);
//returns the correct name of the compressed png image
if (containsMaterial)
{
foreach (Material m in materials)
{
var shaderName = "Standard";
var newShader = Shader.Find(shaderName);
if (newShader != null)
{
m.shader = newShader;
mBundleInstance.GetComponent<Renderer>().material = m;
Debug.Log("actual texture name: " + mBundleInstance.GetComponent<Renderer>().material.mainTexture.name);
//returns the correct name of the png image
}
else
{
Debug.LogWarning("unable to refresh shader: " + shaderName + " in material " + m.name);
}
}
}
以下是编辑器GameView中的样子:Texture of GameObject appears: (The King of Diamonds image)
这就是它在Android设备上的样子:Texture of Gameobject does not appear.
由于没有明显的错误,我不确定为什么会发生这种错误 . 任何帮助或建议为什么会发生这种情况或我可以做些什么来解决它?