Particle System loaded from Asset Bundle not rendering in Editor
Hi everyone!
I’m having a problem with a prefab loaded at runtime from an AssetBundle.
This prefab contains a ParticleSystem that doesn’t show up in the editor.
If during playmode I select the game object, I can see that the PS is emitting, but it doesn’t show in the game view, and appear as empty quads in the Scene view.
If select the PS and open its material properties in the inspector, the particles magically start to show up.
The PS uses Default-ParticleSystem material and Particles/Standard Unlit shader. In the material properties it has a warning saying that I use the incorrect vertex streams , but it works fine in another scene.
I am aware that there are issue with shaders and material loader from a bundle, so as a workaround I re-assign the shader when I instantiate the prefab, and I put it in the “Always included” shader list. The PS works fine on target mobile device (iOS/Android platforms).
Here is the code I use to fix the material/shaders :
...
//It's obviously not named like this, but let's just say it's a reliable way to get the
`//`prefab from the bundle as an Object
UnityEngine.Object asset = GetThePrefabFromTheBundle();
if (null != asset)
{
GameObject reqPrefab = (GameObject)asset;
//m_target is a member variable of Component type that I know it's a transform in this case
Transform t = (Transform)m_Target;
GameObject instance = Instantiate(reqPrefab, t);
//m_offset is a class member Vector3 variable
instance.transform.localPosition = m_offSet;
FixParticleShaders(instance);
}
....
private void FixParticleShaders(GameObject i_gameobject)
{
#if UNITY_EDITOR
ParticleSystemRenderer[] particles = i_gameobject.GetComponentsInChildren<ParticleSystemRenderer>();
for (int i = 0; particles.Length > i; i++)
{
Material mat = particles[i].sharedMaterial;
FixupShader(mat);
}
#endif
}
private void FixupShader(Material i_material)
{
#if UNITY_EDITOR
if ((RuntimePlatform.OSXEditor == Application.platform)
|| (RuntimePlatform.WindowsEditor == Application.platform))
{
if (null != i_material
&& null != i_material.shader
&& !string.IsNullOrEmpty(i_material.shader.name))
{
string shaderName = i_material.shader.name;
Shader sh = Shader.Find(shaderName);
if (null != sh)
{
i_material.shader = sh;
}
}
}
#endif
}
What am I doing wrong?
PS: Sorry for the horribly formatted code, but the textbox here doesn't help much