Home > other >  Unity3D light texture loss, as a result of dynamic loading game resources
Unity3D light texture loss, as a result of dynamic loading game resources

Time:09-23

Unity3d recently doing micro side load, a deadly problems, such as title, I don't know which master more to the same question, or to do resource dynamic loading, how to avoid such problems appear when the scene is always to be baked or really ugly

CodePudding user response:

What unit of game resources? Scene? Prefab? Material? Texture?
Pro, ask something the more clearly describe the problem about good

CodePudding user response:

Hello, I also encountered this problem, the scene dynamic loading baked out of the shadow of the lost, do you solve

CodePudding user response:

 using UnityEngine; 
using System.Collections;
using System.Collections.Generic;

Public class PrefabLightmapData: MonoBehaviour
{
[System. The Serializable]
Struct RendererInfo
{
Public the Renderer the Renderer.
Public int lightmapIndex;
Public Vector4 lightmapOffsetScale;
}

[SerializeField]
RendererInfo [] m_RendererInfo;
[SerializeField]
Texture2D [] m_Lightmaps;

Void Awake ()
{
If (m_RendererInfo==null | | m_RendererInfo. Length==0)
return;

Var lightmaps=LightmapSettings. Lightmaps;
Var combinedLightmaps=new LightmapData [lightmaps. Length + m_Lightmaps. Length].

Lightmaps. CopyTo (combinedLightmaps, 0);
for (int i=0; i {
CombinedLightmaps [I + lightmaps. Length]=new LightmapData ();
CombinedLightmaps [I + lightmaps. Length]. LightmapFar=m_Lightmaps [I];
}

ApplyRendererInfo (m_RendererInfo, lightmaps. Length);
LightmapSettings. Lightmaps=combinedLightmaps;
}


The static void ApplyRendererInfo (RendererInfo [] infos, int lightmapOffsetIndex)
{
for (int i=0; i {
Var info=infos [I];
The info. The renderer. LightmapIndex=info. LightmapIndex + lightmapOffsetIndex;
The info. The renderer. LightmapScaleOffset=info. LightmapOffsetScale;
}
}

# if UNITY_EDITOR
[UnityEditor MenuItem (" Assets/Bake Prefab Lightmaps ")]
The static void GenerateLightmapInfo ()
{
If (UnityEditor. Lightmapping. GiWorkflowMode!=UnityEditor. Lightmapping. GIWorkflowMode. OnDemand)
{
The Debug. LogError (" ExtractLightmapData requires that have baked you lightmaps and Auto mode is disabled. ");
return;
}
//UnityEditor. Lightmapping. Bake ();

Prefabs PrefabLightmapData []=FindObjectsOfType (a);

The foreach (var instance in prefabs)
{
Var gameObject=instance. GameObject;
Var rendererInfos=new List (a);
Var lightmaps=new List
GenerateLightmapInfo (gameObject, rendererInfos, lightmaps);

The instance. M_RendererInfo=rendererInfos. ToArray ();
The instance. M_Lightmaps=lightmaps. ToArray ();

Var targetPrefab=UnityEditor. PrefabUtility. GetPrefabParent (gameObject) as gameObject;
If (targetPrefab!=null)
{
//UnityEditor. Prefab
UnityEditor. PrefabUtility. ReplacePrefab (gameObject, targetPrefab);
}
}
}

The static void GenerateLightmapInfo (GameObject root, List RendererInfos List{
Var renderers=root. GetComponentsInChildren (a);
Foreach (MeshRenderer renderer in renderers)
{
If (the renderer lightmapIndex!=1)
{
RendererInfo info=new RendererInfo ();
The info. The renderer=the renderer;
Info. LightmapOffsetScale=the renderer. LightmapScaleOffset;

Texture2D lightmap=LightmapSettings. Lightmaps [the renderer. LightmapIndex]. LightmapFar;

Info. LightmapIndex=lightmaps. IndexOf (lightmap);
If (info. LightmapIndex==1)
{
Info. LightmapIndex=lightmaps. Count;
Lightmaps. Add (lightmap);
}

RendererInfos. Add (info);
}
}
}
# endif

}

5. X is will trouble later, this is also the official offer, you need to load the following script on the prefab
  • Related