Unity 5.x Dynamically loading light information (all pits have been trampled)

Source: Internet
Author: User
Tags unity 5

Can be found here should be the new baking system pits a little time, 4.x to 5.x art must be re-baked, nothing about fine art, only---re-baking!

The new baking system, in order to compatible with the 5.x multi-scene editing function, the baking information from the mesh all moved to a middleware xxx.assets, this resource file after the 5.x baking and the light map is stored together, however, about this resource file, I am checking to find no interface can be accessed.

Baidu only Google, the Discovery program is basically serialized, because the above mentioned baking information is not stored in the mesh, but a chicken resource file, release xxx.assets can be deleted.

What data is serialized, which can be seen in the editor panel, can be seen in the code below.

Editor Script PrefabLightmapDataEditor.cs:

1 usingUnityengine; 2 usingUnityeditor;3 4  Public classPrefablightmapdataeditor:editor {5[MenuItem ("ojcgames tools/Save baking information for this scene preform",false,0)]6     Static voidSavelightmapinfobygameobject ()7     {  8Gameobject go =Selection.activegameobject;9 Ten         if(NULL= = Go)return; One  APrefablightmapdata data = go. Getcomponent<prefablightmapdata>(); -         if(Data = =NULL) -         { thedata = go. Addcomponent<prefablightmapdata>(); -         } -         //Save Lightmapdata Info by Mesh.render - data. Savelightmap (); +  - Editorutility.setdirty (go); +         //Applay Prefab A Prefabutility.replaceprefab (Go, prefabutility.getprefabparent (go), replaceprefaboptions.connecttoprefab); at     } -}

Serialization script PrefabLightmapData.cs that is bound to the parent of the prefab :

1 usingUnityengine; 2 usingSystem.Collections; 3 usingSystem.Collections.Generic;4 5  Public classPrefablightmapdata:monobehaviour6 {  7 [system.serializable]8     structRendererinfo9     {  Ten          PublicRenderer Renderer;  One          Public intLightmapindex;  A          PublicVector4 Lightmapoffsetscale;  -     } -  the #ifUnity_editor - [Unityengine.serializefield] -Texture2d[] Lightmaptexs;//the light map of the current scene - #endif +  - [Unityengine.serializefield] + rendererinfo[] rendererlist; A  at     #ifUnity_editor -      Public voidSavelightmap () -     { -Renderer[] Renders = getcomponentsinchildren<renderer> (true); - Rendererinfo Rendererinfo; -Rendererlist =Newrendererinfo[renders. Length]; in  -         intindex =0; to  +          for(intR =0, Rlength = renders. Length; r<rlength; ++R) -         { the             if(Renders[r].gameobject.isstatic = =false)Continue; *  $Rendererinfo.renderer =Renders[r];Panax NotoginsengRendererinfo.lightmapindex =Renders[r].lightmapindex; -Rendererinfo.lightmapoffsetscale =Renders[r].lightmapscaleoffset; the  +Rendererlist[index] =Rendererinfo; A  the++index; +         } -  $         //serializing a light map $lightmapdata[] Ldata =Lightmapsettings.lightmaps; -Lightmaptexs =NewTexture2d[ldata. Length]; -          for(intt =0, Tlength = Ldata. Length; t<tlength; ++t) the         { -LIGHTMAPTEXS[T] =Ldata[t].lightmapfar;Wuyi         } the     } -      Wu     voidAwake () -     { About          This. Loadlightmap (); $     } - #endif -  - #if! Unity_editor A      Public  + #endif  the     voidLoadlightmap () -     { $         if(NULL= = Rendererlist | | Rendererlist.length = =0) the         { theDebug.Log (Gameobject.name +"The light information is empty"); the             return; the         } -  inRenderer[] Renders = getcomponentsinchildren<renderer> (true); the  the          for(intR =0, Rlength = renders. Length; r<rlength; ++R) About         { theRenders[r].lightmapindex =Rendererlist[r].lightmapindex; theRenders[r].lightmapscaleoffset =Rendererlist[r].lightmapoffsetscale; the         } +  -         #ifUnity_editor the         if(NULL= = Lightmaptexs | | Lightmaptexs.length = =0)Bayi         { the             return; the         } -  -Lightmapsettings.lightmapsmode =lightmapsmode.nondirectional; thelightmapdata[] Ldata =NewLightmapdata[lightmaptexs.length]; theLightmapsettings.lightmaps =NULL; the  the          for(intt =0, tlength = Lightmaptexs.length; t<tlength; ++t) -         { theLDATA[T] =NewLightmapdata (); theLdata[t].lightmapfar =Lightmaptexs[t]; the         }94  theLightmapsettings.lightmaps =Ldata; the         #endif the     }  98}

which

Texture2d[] Lightmaptexs;
Awake ();
It's me. In order to facilitate the art test, in the release project, I manually manage when the lighting information and the lighting map are loaded, and the code is now available.

Here are some of the pits that hit:
1, PC Normal, published to Android or iOS completely invisible lighting information, and the serialization parameters are correct, the light map load normal, Lightmapsettings.lightmapsmode = Lightmapsmode.nondirectional set to normal:
At the time of publication, Edit-project Settings-graphics-shader stripping-lightmap modes-manual
Official note on this option: By default, Unity looks at your scenes and lightmapping settings to figure out which Fog and lightmapping modes is used; and skips corresponding shader variants. This saves game build data size, and improves loading times.
  In other words, if you want to control the script dynamically, you have to set this to manual mode.
2, the last pit is unable to fully believe the results of the search, others may be recorded only for the problems they appear.

Unity 5.x Dynamically loading light information (all pits have been trampled)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.