using System; using System.Collections.Generic; namespace Thousandto.Launcher.ExternalLibs { /// /// Launcher的全局数据信息 /// public static class GonbestGMemory { //通过字符串作为Key private static Dictionary _allData = new Dictionary(); //分配Float的内存块 public static float[] AllocFloatBlock(string key, int maxSize = -1) { GData ret = null; _allData.TryGetValue(key, out ret); if (maxSize > 0) { if (ret == null) { ret = new GData(maxSize, true); _allData[key] = ret; return ret.Fdata; } else { if (ret.FloatType) { ret.Reset(maxSize); return ret.Fdata; } else { UnityEngine.Debug.LogError("全局变量G["+key+"]不是Float型的数据!"); return null; } } } else { return ret != null ? ret.Fdata : null; } } //分配字符串的内存块 public static string[] AllocStringBlock(string key, int maxSize = -1) { GData ret = null; _allData.TryGetValue(key, out ret); if (maxSize > 0) { if (ret == null) { ret = new GData(maxSize, false); _allData[key] = ret; return ret.SData; } else { if (ret.FloatType) { ret.Reset(maxSize); return ret.SData; } else { UnityEngine.Debug.LogError("全局变量G[" + key + "]不是String型的数据!"); return null; } } } else { return ret != null ? ret.SData : null; } } //判断当前内存块是否有效 public static bool HasBlock(string key) { return _allData.ContainsKey(key); } /// /// 全局数据信息 /// private class GData { //当前全局数据是否为Float型 public bool FloatType { get; private set; } //Float的数据内存块 public float[] Fdata = null; //字符串的数据内存块 public string[] SData = null; //构造函数 public GData(int size, bool floatType) { FloatType = floatType; if (floatType) { Fdata = new float[size]; } else { SData = new string[size]; } } //重新设置大小 public void Reset(int maxSize = -1) { if (FloatType) { Fdata = NewBlock(Fdata, maxSize); } else { SData = NewBlock(SData, maxSize); } } public T[] NewBlock(T[] old,int maxSize) { if (maxSize <= 0) return old; if (old != null) { if (old.Length < maxSize) { T[] newData = new T[maxSize]; Array.Copy(old, newData, Fdata.Length); return newData; } } else { return new T[maxSize]; } return old; } } } }