//MapEditor.MapObjectRoot.json //当前正在编辑的截图 尝试用缓存中拿取 public static void AsyncTryGetSpriteMapCaptureCurrentEditMap(VoidFuncObject cb_sprite, int width = 1136, int height = 640) { if (width <= 0 || height <= 0) { if (cb_sprite != null) { try { cb_sprite(null); } catch (Exception e) { } } return; } if (string.IsNullOrEmpty(MapEditor.MapObjectRoot.record_json)) { return; } _tasks.Enqueue(new Task { cb_sprite = cb_sprite, width = width, height = height, json = MapEditor.MapObjectRoot.record_json }); if (_tasks.Count <= 1) { CheckTask(); } }
IEnumerator LoadFromCache_AsyncCapture(string file_name, string uuid, VoidFuncObject cb, int WIDTH, int HEIGHT, bool showWeapon = false) { // throw new NullReferenceException(); Texture2D tex = null; //try load from disk string weaponExtension = showWeapon ? "_weapon" : ""; var www_local = LocalStorageMapCaptureImage.ins.LoadFromDisk(uuid.ToString() + "_" + WIDTH + "_" + HEIGHT + weaponExtension + ".jpg"); if (www_local != null) { using (www_local) { yield return(www_local); if (www_local.isDone && string.IsNullOrEmpty(www_local.error)) { tex = www_local.texture; } } } if (tex != null) { if (cb != null) { cb(tex); } else { GameObject.DestroyImmediate(tex); } } tex = null; }
//当前正在编辑的截图 public static void AsyncGetSpriteMapCaptureCurrentEditMap(VoidFuncObject cb_sprite, int width = 1136, int height = 640) { if (width <= 0 || height <= 0) { if (cb_sprite != null) { try { cb_sprite(null); } catch (Exception e) { } } return; } if (MapEditor.MapObjectRoot.ins == null || MapEditor.MapObjectRoot.ins == null) { return; } _tasks.Enqueue(new Task { cb_sprite = cb_sprite, width = width, height = height, json = MapObjectRoot.ins.SerializeToJson() }); if (_tasks.Count <= 1) { CheckTask(); } }
//加载本地地图 public static void AsyncGetSpriteMapCaptureLocalMapTmp(string uuid, VoidFuncObject cb_sprite, int width = 1136, int height = 640) { if (string.IsNullOrEmpty(uuid) || width <= 0 || height <= 0) { if (cb_sprite != null) { try { cb_sprite(null); } catch (Exception e) { } } return; } ///try load from cache if (MapEditor.MapCaptureLoadFromCache.LoadFromCacheAsyncCaptureLocalMapTmp(uuid, cb_sprite, width, height)) { //get ok return; } _tasks.Enqueue(new Task { uuid = uuid, cb_sprite = cb_sprite, width = width, height = height, LoadLocal = true }); if (_tasks.Count <= 1) { CheckTask(); } }
//step 1 public static void AsyncGetTextImage(string uuid, string text, VoidFuncObject cb_sprite, int width = 1136, int height = 640) { if (string.IsNullOrEmpty(uuid) || width <= 0 || height <= 0) { if (cb_sprite != null) { try { cb_sprite(null); } catch (Exception e) { } } return; } _tasks.Enqueue(new TextImageTask { uuid = uuid, cb_sprite = cb_sprite, width = width, height = height, text = text, }); if (_tasks.Count >= 1) { CheckTask(); } else { } }
//指定的json 截图 IEnumerator AsyncCaptureCurrentEditMap(string json, VoidFuncObject cb, int WIDTH, int HEIGHT, bool showWeapon = false) { if (showWeapon) { ShowWeaponSpawnPoints(); } var serializer = this.GetComponent <MapEditor.RuntimeSerialize>(); //先把地图加载出来 serializer.LoadFromJson(json); transform.position = new Vector3(0f, 1000f, 1000f); //销毁所有MapObject脚本 防止以外情况 // yield return new WaitForEndOfFrame(); yield return(new WaitForEndOfFrame()); if (showWeapon) { CheckWeaponSpawnPoints(); } foreach (var p in this.GetComponentsInChildren <MonoBehaviour>()) { if (p != null) { if (p.gameObject == this.gameObject) { continue; } GameObject.Destroy(p); } } //开始捕捉图像 RenderTexture tex = new RenderTexture(WIDTH, HEIGHT, 20); camera.targetTexture = tex; camera.Render(); RenderTexture.active = tex; Texture2D tex2d = new Texture2D(WIDTH, HEIGHT); tex2d.ReadPixels(new Rect(0, 0, WIDTH, HEIGHT), 0, 0); tex2d.Apply(); RenderTexture.active = null; camera.targetTexture = null; if (cb != null) { cb(tex2d); } else { GameObject.DestroyImmediate(tex2d); } GameObject.DestroyImmediate(tex); tex2d = null; tex = null; Application.UnloadLevel("MapRuntimeCapture"); }
public static bool LoadFromCacheAsyncCaptureLocalMapTmp(string uuid, VoidFuncObject cb, int WIDTH, int HEIGHT) { if (ins == null || string.IsNullOrEmpty(uuid) || cb == null || WIDTH <= 0 || HEIGHT <= 0) { return(false); } string file_name = LocalStorageMapCaptureImage.ins.GetRootDirectory() + "/" + "tmp" + uuid.ToString() + "_" + WIDTH + "_" + HEIGHT + ".jpg"; if (File.Exists(file_name)) { //缓存存在 直接读取 ins.StartCoroutine(ins.LoadFromCache_AsyncCaptureLocalMapTmp(file_name, uuid, cb, WIDTH, HEIGHT)); return(true); } return(false); }
public void BeingText2Image(VoidFuncObject cb, int WIDTH, int HEIGHT) { //yield return new WaitForEndOfFrame(); //string file_name = Application.dataPath + "/../Cache" + "/" + "tmp12345678" + "_" + WIDTH + "_" + HEIGHT + ".jpg"; string file_name = LocalStorageMapCaptureImage.ins.GetRootDirectory() + "/" + "tmp" + StaticData.uuid + "_" + WIDTH + "_" + HEIGHT + ".jpg"; //开始捕捉图像 RenderTexture tex = new RenderTexture(WIDTH, HEIGHT, 20); camera.targetTexture = tex; camera.Render(); RenderTexture.active = tex; Texture2D tex2d = new Texture2D(WIDTH, HEIGHT); tex2d.ReadPixels(new Rect(0, 0, WIDTH, HEIGHT), 0, 0); tex2d.Apply(); byte[] save_data = tex2d.EncodeToJPG(); File.WriteAllBytes(file_name, save_data); RenderTexture.active = null; camera.targetTexture = null; if (cb != null) { cb(tex2d); } else { GameObject.DestroyImmediate(tex2d); } GameObject.DestroyImmediate(tex); tex2d = null; tex = null; Application.UnloadLevel("text2image"); }
IEnumerator LoadFromCache_AsyncCaptureLocalMapTmp(string file_name, string uuid, VoidFuncObject cb, int WIDTH, int HEIGHT) { //本地的都带了tmp字样 // throw new NullReferenceException(); Texture2D tex = null; //try load from disk var www_local = LocalStorageMapCaptureImage.ins.LoadFromDisk("tmp" + uuid.ToString() + "_" + WIDTH + "_" + HEIGHT + ".jpg"); if (www_local != null) { using (www_local) { yield return(www_local); if (www_local.isDone && string.IsNullOrEmpty(www_local.error)) { tex = www_local.texture; } } } if (tex != null) { if (cb != null) { cb(tex); } else { GameObject.DestroyImmediate(tex); } } tex = null; }
//从本地临时地图开始截屏 IEnumerator AsyncCaptureLocalMapTmp(string uuid, VoidFuncObject cb, int WIDTH, int HEIGHT, bool showWeapon = false) { //本地的都带了tmp字样 // uuid = "tmp" + uuid; //先检查缓存图片存在否 如果存在 优先加载缓存 string weaponExtension = showWeapon ? "_weapon" : ""; string file_name = LocalStorageMapCaptureImage.ins.GetRootDirectory() + "/" + "tmp" + uuid.ToString() + "_" + WIDTH + "_" + HEIGHT + weaponExtension + ".jpg"; bool load_local_ok = false; if (File.Exists(file_name)) { // throw new NullReferenceException(); Texture2D tex = null; //try load from disk var www_local = LocalStorageMapCaptureImage.ins.LoadFromDisk("tmp" + uuid.ToString() + "_" + WIDTH + "_" + HEIGHT + ".jpg"); if (www_local != null) { using (www_local) { yield return(www_local); if (www_local.isDone && string.IsNullOrEmpty(www_local.error)) { tex = www_local.texture; } } } if (tex != null) { if (cb != null) { cb(tex); load_local_ok = true; } else { GameObject.DestroyImmediate(tex); } } tex = null; } if (load_local_ok == false) { download_ok = true; string x = LocalStorageMapTmp.ins.LoadTextFromDisk(StaticData.uuid + "_" + uuid.ToString() + ".json"); if (string.IsNullOrEmpty(x) || !Serializable.Map.IsMapJson(x)) { download_ok = false; } if (download_ok) { if (showWeapon) { ShowWeaponSpawnPoints(); } var serializer = this.GetComponent <MapEditor.RuntimeSerialize>(); //先把地图加载出来 serializer.LoadFromJson(x); transform.position = new Vector3(0f, 1000f, 1000f); //销毁所有MapObject脚本 防止以外情况 // yield return new WaitForEndOfFrame(); yield return(new WaitForEndOfFrame()); if (showWeapon) { CheckWeaponSpawnPoints(); } foreach (var p in this.GetComponentsInChildren <MonoBehaviour>()) { if (p != null) { if (p.gameObject == this.gameObject) { continue; } GameObject.Destroy(p); } } //开始捕捉图像 RenderTexture tex = new RenderTexture(WIDTH, HEIGHT, 20); camera.targetTexture = tex; camera.Render(); RenderTexture.active = tex; Texture2D tex2d = new Texture2D(WIDTH, HEIGHT); tex2d.ReadPixels(new Rect(0, 0, WIDTH, HEIGHT), 0, 0); tex2d.Apply(); byte[] save_data = tex2d.EncodeToJPG(); File.WriteAllBytes(file_name, save_data); RenderTexture.active = null; camera.targetTexture = null; if (cb != null) { cb(tex2d); } else { GameObject.DestroyImmediate(tex2d); } GameObject.DestroyImmediate(tex); tex2d = null; tex = null; } else { cb(null); } } Application.UnloadLevel("MapRuntimeCapture"); }