I have a project.The project has a scene I want to switch more than two maps. I build a new scene and use the first scene save scene as...this.So I have two same scene. Only two different lighting scenes. In unity I just set up 'Lighting-Scene-Environment Lighting-Ambient GI-Realtime' and 'Lighting-Scene-General GI-Directional Mode-Directional',Build lighting have two lightingmaps,There are 55 Lightmaps like this 'Lightmap-55_comp_dir.exr and Lightmap-55_comp_light.exr'.Than I use scripts switch lightmaps than has this error'input_begin <= input_end' error,but my scripts is ok with test project with 2 lightmaps. and I want try-catch,but no fand. After,I remove my script but this error not disappear.Means my project have this error when run now.Who can help me.
![alt text][1]
you can build a project and my scripts is
"LightMapSwitcher.cs"
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
internal class ALLNF
{
internal Texture2D[] NF;
}
internal class AllLightMap
{
internal LightmapData[] LM;
}
public class LightMapSwitcher : MonoBehaviour
{
public int times;
public string[] URLS;
private bool _Switchs;
private List _AllNFs = new List ();
private List _AllLFs = new List ();
void Start ()
{
try {
SelectSelf ();
for (int i = 0; i < _AllNFs.Count; i++) {
_AllNFs [i].NF = _AllNFs [i].NF.OrderBy (t2d => t2d.name, new NaturalSortComparer ()).ToArray ();
}
if (_Switchs) {
allfsAdd ();
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[AllNFs_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 {
allfsAdd ();
for (int AllLFs_i = 0; AllLFs_i < _AllLFs.Count; AllLFs_i++) {
_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];
}
}
}
} catch (System.Exception ex) {
Debug.Log(ex);
}
}
private void allfsAdd ()
{
for (int i = 0; i < URLS.Length; i++) {
AllLightMap alm = new AllLightMap ();
_AllLFs.Add (alm);
}
}
#region 根据路径自动加载lightmaps
public void SelectSelf ()
{
_Switchs = SwitchJudgMent (URLS [0]);
int _UPDsLentgth;
if (_Switchs) {
_UPDsLentgth = URLS.Length * 2;
int x = 0;
for (int i = 0; i < _UPDsLentgth; i++) {
ALLNF newAllNf = new ALLNF ();
if (i != 0 && i % 2 == 0) {
x++;
}
if (i % 2 == 0) {
newAllNf.NF = LoadAsset (URLS [i - x], "exr", "dir");
} else {
newAllNf.NF = LoadAsset (URLS [i - 1 - x], "exr", "light");
}
_AllNFs.Add (newAllNf);
}
} else {
_UPDsLentgth = URLS.Length;
for (int i = 0; i < _UPDsLentgth; i++) {
ALLNF newAllNf = new ALLNF ();
//newAllNf.NF=LoadAsset (_UPDs[i], "exr", "dir");
newAllNf.NF = LoadAsset (URLS [i], "exr", "light");
_AllNFs.Add (newAllNf);
}
}
}
T[] LoadAsset (string path, string pattern, string Distance) where T : Object
{
string objPath = Application.dataPath + "/" + path;
string[] directoryEntries;
List objList = new List ();
try {
directoryEntries = System.IO.Directory.GetFileSystemEntries (objPath);
for (int i = 0; i < directoryEntries.Length; i++) {
string p = directoryEntries [i];
string[] tempPaths = SplitWithString (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 arrayList = new List ();
string s = string.Empty;
while (sourceString.IndexOf(splitString) > -1) { //切割
s = sourceString.Substring (0, sourceString.IndexOf (splitString));
sourceString = sourceString.Substring (sourceString.IndexOf (splitString) + splitString.Length);
arrayList.Add (s);
}
arrayList.Add (sourceString);
return arrayList.ToArray ();
}
private bool SwitchJudgMent (string path)
{
string objPath = Application.dataPath + "/" + path;
string[] directoryEntries;
int JudgMT_light = 0;
int JudgMT_dir = 0;
try {
directoryEntries = System.IO.Directory.GetFileSystemEntries (objPath);
JudgMT_light = Judg_LD (directoryEntries, path, "light.exr");
JudgMT_dir = Judg_LD (directoryEntries, path, "dir.exr");
} catch (System.IO.DirectoryNotFoundException) {
Debug.Log ("The path encapsulated in the " + objPath + "Directory object does not exist.");
}
if (JudgMT_dir + JudgMT_light == 2)
return true;
return false;
}
private int Judg_LD (string[] directoryEntries, string path, string JudgLD)
{
for (int i = 0; i < directoryEntries.Length; i++) {
string p = directoryEntries [i];
string[] tempPaths = SplitWithString (p, "/Assets/" + path + "\\");
if (tempPaths [1].EndsWith (JudgLD)) {
return 1;
}
}
return 0;
}
#endregion
#region use
public void SetToTimes (int i)
{
try {
LightmapSettings.lightmaps = _AllLFs [i].LM;
} catch (System.Exception ex) {
Debug.Log ("your times is out lightmaps index:" + ex);
}
}
#endregion
#region inspector
[ContextMenu ("Set to Lightmaps ")]
void Debug00 ()
{
SetToTimes (times);
}
#endregion
}
"NaturalSortComparer.cs"
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class NaturalSortComparer : IComparer, IDisposable
{
private readonly bool isAscending;
public NaturalSortComparer(bool inAscendingOrder = true)
{
this.isAscending = inAscendingOrder;
}
#region IComparer Members
public int Compare(string x, string y)
{
throw new NotImplementedException();
}
#endregion
#region IComparer Members
int IComparer.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 table = new Dictionary();
public void Dispose()
{
table.Clear();
table = null;
}
}
[1]: /storage/temp/60814-jlqpk5.png
↧