Unity5.x dynamic loading of lightmaps (baking textures), unity5.xlightmaps

Source: Internet
Author: User

Unity5.x dynamic loading of lightmaps (baking textures), unity5.xlightmaps

The scenario in the project takes 24 hours a day. The Time of day plug-in will not be used here.

Google to an article http://wiki.unity3d.com/index.php/lightmapswitcherto test and find it available. Prepare to put the Code directly into the project and find that there is a problem that is not a problem

Using UnityEngine; using UnityEditor; using System. linq; using System. text; using System. collections; using System. collections. generic; using System. text. regularExpressions; // Why [System. serializable] to serialize, instead of using System, directly [Serializable] // it should have an impact on the following <T> as object // serialization here is to edit [System. serializable] public class U_P_D {// path for storing lightmaps. For example, if my folder is 001 under Assets, you only need to enter 001. If it is Assets-Scence-Main, enter SC. Ence/Main public string _ URL; public string URL {get {return _ URL;} set {_ URL = value ;}// the extension name lightmaps is. exr public string _ Pattern; public string Pattern {get {return _ Pattern;} set {_ Pattern = value ;}} // dir or light indicates whether it is a vision or a near view. dir indicates lightmapNear, and light indicates lightmapFar public string _ Distance; public string Distance {get {return _ Distance ;} set {_ Distance = value ;}} [System. serializable] public class ALLNF {[System. nonSerialized] public Texture2D [] NF;} [System. serializable] public class AllLightMap {[System. nonSerialized] public LightmapData [] LM;} public class CC_LightMapSwitcher: MonoBehaviour {// you do not know that only light is available after you encounter lightmaps. exr, no dir. exr, so here I add a judgment // everything to be lazy public bool _ Switchs; public U_P_D [] _ upd.public List <ALLNF> _ AllNFs; public List <AllLightMap> _ AllLFs; void Start () {SelectSelf (); // Sort the lightmaps in for (int I = 0; I <_ AllNFs. count; I ++) {_ AllNFs [I]. NF = _ AllNFs [I]. NF. orderBy (t2d => t2d. name, new NaturalSortComparer <string> ()). toArray () ;}// if you check, it indicates that your lightmaps have two options: dir and light. if (_ Switchs) {for (int AllLFs_ I = 0; AllLFs_ I <_ AllLFs. count; AllLFs_ I ++) {int AllNFs_ I = AllLFs_ I * 2; _ AllLFs [AllLFs_ I]. LM = new LightmapData [_ AllNFs [AllNFs_ I]. NF. length]; for (int I = 0; I <_ AllNFs [AllNF S_ I]. NF. length; I ++) {_ AllLFs [AllLFs_ I]. LM [I] = new LightmapData (); _ AllLFs [AllLFs_ I]. LM [I]. lightmapNear = _ AllNFs [AllNFs_ I]. NF [I]; _ AllLFs [AllLFs_ I]. LM [I]. lightmapFar = _ AllNFs [AllNFs_ I + 1]. NF [I] ;}} else {for (int AllLFs_ I = 0; AllLFs_ I <_ AllLFs. count; AllLFs_ I ++) {// int AllNFs_ I = AllLFs_ I * 2; _ AllLFs [AllLFs_ I]. LM = new LightmapData [_ AllNFs [AllLFs_ I]. NF. length]; for (int I = 0; I <_ AllNFs [AllLFs _ I]. NF. length; I ++) {_ AllLFs [AllLFs_ I]. LM [I] = new LightmapData (); // _ AllLFs [AllLFs_ I]. LM [I]. lightmapNear = _ AllNFs [AllLFs_ I]. NF [I]; _ AllLFs [AllLFs_ I]. LM [I]. lightmapFar = _ AllNFs [AllLFs_ I]. NF [I] ;}}}# region SelectSelf public void SelectSelf () {if (_ UPDs. length = _ AllNFs. count) {for (int I = 0; I <_ upps. length; I ++) {_ AllNFs [I]. NF = LoadAsset <Texture2D> (_ UPDs [I]. URL, _ upps [I]. pattern, _ upps [I]. distance) ;}}t [] LoadAsset <T> (string path, string pattern, string Distance) where T: Object {string objPath = Application. dataPath + "/" + path; string [] directoryEntries; List <T> objList = new List <T> (); try {directoryEntries = System. IO. directory. getFileSystemEntries (objPath); for (int I = 0; I <directoryEntries. length; I ++) {string p = directoryEntries [I]; string [] tempPaths = SplitW IthString (p, "/Assets/" + path + "\"); if (tempPaths [1]. EndsWith (Distance + "." + pattern) {if (! TempPaths [1]. startsWith ("ReflectionProbe") {T tempTex = AssetDatabase. loadAssetAtPath ("Assets/" + path + "/" + tempPaths [1], typeof (T) as T; if (tempTex! = Null) {objList. add (tempTex) ;}}}} catch (System. IO. directoryNotFoundException) {Debug. log ("The path encapsulated in the" + objPath + "Directory object does not exist. ");} if (objList. count> 0) return objList. toArray (); return null;} public static string [] SplitWithString (string sourceString, string splitString) {string tempSourceString = sourceString; list <string> arrayList = new List <string> (); string s = string. empty; while (sourceString. indexOf (splitString)>-1) // cut {s = sourceString. substring (0, sourceString. indexOf (splitString); sourceString = sourceString. substring (sourceString. indexOf (splitString) + splitString. length); arrayList. add (s);} arrayList. add (sourceString); return arrayList. toArray () ;}# endregion # region Publics public void SetToDay () {// different from the source code LightmapSettings. lightmaps = _ AllLFs [0]. LM;} public void SetToNight () {LightmapSettings. lightmaps = _ AllLFs [1]. LM ;}# endregion # region Debug [ContextMenu ("Set to Night")] void Debug00 () {SetToNight ();} [ContextMenu ("Set to Day")] void Debug01 () {SetToDay () ;}# endregion}

The second NaturalSortComparer. cs has not changed.

using UnityEngine;using System.Collections;using System;using System.Collections.Generic;using System.Text.RegularExpressions;public class NaturalSortComparer<T> : IComparer<string>, IDisposable{    private readonly bool isAscending;        public NaturalSortComparer(bool inAscendingOrder = true)    {        this.isAscending = inAscendingOrder;    }        #region IComparer<string> Members    public int Compare(string x, string y)    {        throw new NotImplementedException();    }    #endregion        #region IComparer<string> Members    int IComparer<string>.Compare(string x, string y)    {        if (x == y)            return 0;                string[] x1, y1;                if (!table.TryGetValue(x, out x1))        {            x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");            table.Add(x, x1);        }                if (!table.TryGetValue(y, out y1))        {            y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");            table.Add(y, y1);        }                int returnVal;                for (int i = 0; i < x1.Length && i < y1.Length; i++)        {            if (x1[i] != y1[i])            {                returnVal = PartCompare(x1[i], y1[i]);                return isAscending ? returnVal : -returnVal;            }        }                if (y1.Length > x1.Length)        {            returnVal = 1;        }        else if (x1.Length > y1.Length)        {            returnVal = -1;        }        else        {            returnVal = 0;        }                return isAscending ? returnVal : -returnVal;    }        private static int PartCompare(string left, string right)    {        int x, y;        if (!int.TryParse(left, out x))            return left.CompareTo(right);                if (!int.TryParse(right, out y))            return left.CompareTo(right);                return x.CompareTo(y);    }    #endregion        private Dictionary<string, string[]> table = new Dictionary<string, string[]>();        public void Dispose()    {        table.Clear();        table = null;    }}

The usage order is reversed.

1. Switchs has dir and light, and only one of them is canceled.

2. AllLFs indicates that you have several textures. I only use two sets of daytime and night images, and you can have multiple sets of textures. The parameters must be changed below.

3. The length of AllNFs is the same as that of upd.if Switchs are hooked, the length of AllLFS is twice that of AllLFS. Switchs is not hooked and the length is the same as that of AllLFS.

4. the URL in upps indicates the lightmaps path.

Pattern is the suffix

Distance is a remote view

Code with comments

 

Test is available. Because I only use two sets of textures here, I click the event to write the day and night. If there are more than 10 sets of textures, you can control the code. I will not write it here.

This Code also applies to object texture replacement in static scenarios. Texture replacement in dynamic scenes is not considered now. However, I have seen a blog about Dynamic Replacement in Yusong MOMO. You can search for it.

 

I 'd like to hear from you if you have any questions or improvements in your code.

Related Article

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.