// For Load void OnFinishedImagePicker(string message) { if (LoadTextureFromImagePicker.IsLoaded()) { var material = obj.GetComponent <Renderer>().material; int width = material.mainTexture.width; int height = material.mainTexture.height; const bool createMipmap = true; Texture2D texture = LoadTextureFromImagePicker.GetLoadedTexture(message, width, height, createMipmap); if (texture) { // Load Texture #if false var lastTexture = material.mainTexture; material.mainTexture = texture; Destroy(lastTexture); // Destroy the last texture #else material.mainTexture = texture; #endif } LoadTextureFromImagePicker.ReleaseLoadedImage(); } else { // Closed LoadTextureFromImagePicker.Release(); } }
// For Save IEnumerator CaptureScreen() { yield return(new WaitForEndOfFrame()); //Get another copy to show on share screen Texture2D screenshot = ScreenCapture.Capture(); Color32[] pix = screenshot.GetPixels32(); Destroy(screenShotCopy); screenShotCopy = new Texture2D(screenshot.width, screenshot.height); screenShotCopy.SetPixels32(pix); screenShotCopy.Apply(); Debug.Log("Capture one frame"); #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { LoadTextureFromImagePicker.SaveAsJpgToPhotoLibrary(screenshot, gameObject.name, "OnFinishedSaveImage"); } #endif if (Application.platform == RuntimePlatform.Android) { SaveImageToLibraryAndriod(screenshot); } //FOR TEST //OnFinishedSaveImage(""); }
// For Load void OnFinishedImagePicker(string message) { if (LoadTextureFromImagePicker.IsLoaded()) { int width, height; var orgTexWidth = LoadTextureFromImagePicker.GetLoadedTextureWidth(); var orgTexHeight = LoadTextureFromImagePicker.GetLoadedTextureHeight(); if (orgTexWidth > orgTexHeight) { width = Mathf.Min(orgTexWidth, maxTextureSize); height = width * orgTexHeight / orgTexWidth; } else { height = Mathf.Min(orgTexHeight, maxTextureSize); width = height * orgTexWidth / orgTexHeight; } bool mipmap = false; Texture2D texture = LoadTextureFromImagePicker.GetLoadedTexture(message, width, height, mipmap); if (texture != null) { // Load Texture ReplaceSprite(texture, width, height); } LoadTextureFromImagePicker.ReleaseLoadedImage(); } else { // Closed LoadTextureFromImagePicker.Release(); } }
void Start() { if (targetMaterial == null) { targetMaterial = GameObject.Find("Cube").GetComponent <Renderer>().material; } #if UNITY_IPHONE // Resume if (targetMaterial != null) { var resume = false; if (isSaveToLocalFile) { var savedTex = LoadTextureFromImagePicker.LoadFromLocalFile(saveTexFileName); if (savedTex != null) { targetMaterial.mainTexture = savedTex; resume = true; } } if (resume == false) { targetMaterial.mainTexture = defaultTexture; } } #endif }
// For Save IEnumerator CaptureScreen() { yield return(new WaitForEndOfFrame()); // Save to PhotoLibrary Texture2D screenShot = ScreenCapture.Capture(); if (saveAsPng) { bool withTransparency = false; if (withTransparency) { // PNG with transparency LoadTextureFromImagePicker.SaveAsPngWithTransparencyToPhotoLibrary(screenShot, gameObject.name, "OnFinishedSaveImage"); } else { // PNG LoadTextureFromImagePicker.SaveAsPngToPhotoLibrary(screenShot, gameObject.name, "OnFinishedSaveImage"); } } else { // JPG LoadTextureFromImagePicker.SaveAsJpgToPhotoLibrary(screenShot, gameObject.name, "OnFinishedSaveImage"); } }
// For Save IEnumerator CaptureScreen() { yield return(new WaitForEndOfFrame()); uploadImage = ScreenCapture.Capture(); LoadTextureFromImagePicker.SaveAsJpgToPhotoLibrary(uploadImage, gameObject.name, "OnFinishedSaveImage"); }
// For Save IEnumerator CaptureScreen() { yield return(new WaitForEndOfFrame()); // Save to PhotoLibrary Texture2D texScreenShot = ScreenCapture.Capture(); LoadTextureFromImagePicker.SaveAsPngToPhotoLibrary(texScreenShot, this.name, "OnFinishedSaveImage"); }
public void LoadTexture() { #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { LoadTextureFromImagePicker.SetPopoverAutoClose(true); LoadTextureFromImagePicker.SetPopoverToCenter(); LoadTextureFromImagePicker.ShowPhotoLibrary(gameObject.name, "OnFinishedImagePicker"); } #endif }
// For Load void OnFinishedImagePicker(string message) { lastMessage = message; if (targetMaterial && LoadTextureFromImagePicker.IsLoaded()) { int width, height; if (bUseOriginalImageSize) { width = LoadTextureFromImagePicker.GetLoadedTextureWidth(); height = LoadTextureFromImagePicker.GetLoadedTextureHeight(); } else { width = textureWidth; height = textureHeight; } bool mipmap = true; Texture2D texture = LoadTextureFromImagePicker.GetLoadedTexture(message, width, height, mipmap); if (texture) { // Load Texture Texture lastTexture = targetMaterial.mainTexture; targetMaterial.mainTexture = texture; Destroy(lastTexture); if (isSaveToLocalFile) { // Save to local file LoadTextureFromImagePicker.SaveToLocalFile(saveTexFileName, texture); } } LoadTextureFromImagePicker.ReleaseLoadedImage(); } else { // Closed LoadTextureFromImagePicker.Release(); } }
void Update() { // Touch Event bool touched = false; Vector2 touchPos = Vector2.zero; // if (Application.platform == RuntimePlatform.IPhonePlayer) { if (Input.touchCount > 0) { // Touches for (int i = 0; i < Input.touchCount; ++i) { Touch touch = Input.GetTouch(i); if (touch.phase == TouchPhase.Began) { touchPos.x = touch.position.x; touchPos.y = touch.position.y; touched = true; break; // Single Touch } } } } else { // Mouse if (Input.GetMouseButtonDown(0)) { touchPos.x = Input.mousePosition.x; touchPos.y = Input.mousePosition.y; touched = true; } } // Check Touch to Object if (touched) { // Hit Check RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(touchPos); if (Physics.Raycast(ray, out hit, 100.0f)) { // HIt!! GameObject hitPlane = hit.collider.gameObject; if (hitPlane.Equals(objTexPhoto)) { // Photo #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { obj = objTexPhoto; LoadTextureFromImagePicker.ShowPhotoLibrary(this.name, "OnFinishedImagePicker"); } else #endif { Debug.Log("Touched on Photo"); } } else if (hitPlane.Equals(objTexCamera)) { // Camera #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { obj = objTexCamera; LoadTextureFromImagePicker.ShowCamera(this.name, "OnFinishedImagePicker"); } else #endif { Debug.Log("Touched on Camera"); } } else if (hitPlane.Equals(objSave)) { // Save if (Application.platform == RuntimePlatform.IPhonePlayer) { textSave.text = "Saving.."; StartCoroutine("CaptureScreen"); } else { Debug.Log("Touched on Save Button"); } } } } }
void OnGUI() { // Swithes GUI.Label(new Rect(0, Screen.height * 0.5f, 100, 30), "Options:"); bUseOriginalImageSize = GUI.Toggle(new Rect(0, Screen.height * 0.5f + 30, 400, 30), bUseOriginalImageSize, "Use Original Image Size"); iPadPopover_DisableAutoClose = GUI.Toggle(new Rect(0, Screen.height * 0.5f + 60, 400, 30), iPadPopover_DisableAutoClose, "iPad Popover: Disable Auto Close"); isDefaultFrontCamera = GUI.Toggle(new Rect(0, Screen.height * 0.5f + 90, 400, 30), isDefaultFrontCamera, "Default Front Camera"); isSaveToLocalFile = GUI.Toggle(new Rect(0, Screen.height * 0.5f + 120, 400, 30), isSaveToLocalFile, "Save/Resume last selected image to local file"); // Buttons float buttonWidth = Screen.width / 3; float buttonHeight = Screen.height / 5; float buttonMargine = buttonWidth / 3; Rect buttonRect = new Rect(0, Screen.height - buttonHeight, buttonWidth, buttonHeight); buttonRect.x = buttonMargine; if (targetMaterial == null) { GUI.Box(buttonRect, "(Set Target Material)"); } else if (GUI.Button(buttonRect, GetButtonLabel("Camera"))) { #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { LoadTextureFromImagePicker.SetDefaultFrontCamera(isDefaultFrontCamera); LoadTextureFromImagePicker.SetPopoverToCenter(); LoadTextureFromImagePicker.ShowCamera(gameObject.name, "OnFinishedImagePicker"); } #endif } buttonRect.x = buttonMargine + buttonWidth + buttonMargine; if (targetMaterial == null) { GUI.Box(buttonRect, "(Set Target Material)"); } else if (GUI.Button(buttonRect, GetButtonLabel("Load Image\nfrom PhotoLibrary"))) { #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { LoadTextureFromImagePicker.SetPopoverAutoClose(iPadPopover_DisableAutoClose == false); LoadTextureFromImagePicker.SetPopoverTargetRect(buttonRect.x, buttonRect.y, buttonWidth, buttonHeight); LoadTextureFromImagePicker.ShowPhotoLibrary(gameObject.name, "OnFinishedImagePicker"); } #endif } // // for Save Image buttonRect.width = Screen.width / 4; buttonRect.height = Screen.height / 6; buttonMargine = 0; buttonRect.y = 0; buttonRect.x = buttonMargine + (buttonRect.width + buttonMargine) * 1; if (GUI.Button(buttonRect, GetButtonLabel("Save JPG\nto PhotoLibrary"))) { #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { saveAsPng = false; StartCoroutine("CaptureScreen"); } #endif } buttonRect.x = buttonMargine + (buttonRect.width + buttonMargine) * 2; if (GUI.Button(buttonRect, GetButtonLabel("Save PNG\nto PhotoLibrary"))) { #if UNITY_IPHONE if (Application.platform == RuntimePlatform.IPhonePlayer) { saveAsPng = true; StartCoroutine("CaptureScreen"); } #endif } // Disp Texture Size if (targetMaterial) { Texture targetTexture = targetMaterial.mainTexture; GUI.Label(new Rect(0, 0, 400, 100), "Current Texture Size:\n" + "width=" + targetTexture.width + ", height=" + targetTexture.height); } // Disp Last Message GUI.Label(new Rect(0, 80, 200, 60), "Last Result:\n" + lastMessage); }