Loading Images on iOS vs Standalone Build
Hi All,
I have a simple program for iOS that shows you what is on the webcam, and, when you click on one of 25 cubes, it maps that to the cube. When you return to the scene later in the game, your picture is still there. this works fine on a standalone build. However, when I connect my iPAD and play it there, i get a strange behavior. The pics map to the cubes when i click on them, but when I reload the scene later, the cubes are all back in their original, unmapped state with no pictures on them. Is there something different about how I should save and load the files in smartupdatepics() function. I feel it is a simple fix, but I can't for the life of me figure it out! Any suggestions? Thanks, Roger
#pragma strict
import System.IO;
import UnityEngine.UI;
//put this on an object like a cube
//Then create a button that, when pressed, activates the takephoto function of this script
//takes pic to Assets/MiscImages, cant seem to work anywhere else...
var CubePlayers : GameObject[] = new GameObject[25];
var PrefabCubePlayers : GameObject[] = new GameObject[25];
var PrefabTextures: Texture[] = new Texture[25];
var PhotoTextures: Texture[] = new Texture[25];
var placeholder:int;
var indicator: GameObject;
var MajorCube: GameObject;
var onpremade = false;
var blankphototex: Texture;
public static var webCamTexture: WebCamTexture;
function Start() {
//Get textures on object and write to persistent data path for easy access later on
for(var cctr = 0; cctr < 25; cctr++){
var AsTex = PrefabTextures[cctr] as Texture2D;
var bytes = AsTex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/prefabnum"+cctr+".jpg", bytes);
Debug.Log("Wrote File: "+Application.persistentDataPath + "/prefabnum"+cctr+".jpg");
}
webCamTexture = new WebCamTexture();
this.GetComponent.<Renderer>().material.mainTexture = webCamTexture;
webCamTexture.Play();
smartupdatepics();
}
function Update(){
detectobjecttouched();
}
function smartupdatepics() {
//load photos and such
var sboard2textures : Texture[] = new Texture[25];
for(var tctr = 0; tctr < 25; tctr++)
{
Debug.Log("loaded: "+Application.persistentDataPath + "/num"+tctr+".jpg");
if (!System.IO.File.Exists(Application.persistentDataPath + "/num"+tctr.ToString+".jpg")){
CubePlayers[tctr].GetComponent.<Renderer>().material.mainTexture = blankphototex;
}
if (System.IO.File.Exists(Application.persistentDataPath + "/num"+tctr+".jpg")){
var bytestorage = System.IO.File.ReadAllBytes(Application.persistentDataPath + "/num"+tctr+".jpg");
Debug.Log("loaded: "+Application.persistentDataPath + "/num"+tctr+".jpg");
var textoload = new Texture2D(1,1);
textoload.LoadImage(bytestorage);
CubePlayers[tctr].GetComponent.<Renderer>().material.mainTexture = textoload;
}
}
//load up textures on cubes for prefab players
var prefabtextures : Texture[] = new Texture[25];
for(var pctr = 0; pctr < 25; pctr++){
PrefabCubePlayers[pctr].GetComponent.<Renderer>().material.mainTexture = PrefabTextures[pctr];
}
}
function detectobjecttouched(){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButton(0)){
if ( Physics.Raycast( ray, hit, 1000 ) ){
var phoo = hit.collider.gameObject.transform.name;
var photo2 = new Texture2D(webCamTexture.width, webCamTexture.height);
photo2.SetPixels(webCamTexture.GetPixels());
TextureScale.Bilinear(photo2, 320, 190);
photo2.Apply();
var i: int;
if (int.TryParse(phoo,i)){
hit.collider.gameObject.GetComponent.<Renderer>().material.mainTexture = photo2;
var bytes = photo2.EncodeToPNG();
// File.WriteAllBytes(Application.persistentDataPath + "/" + someFilename, someTexture.EncodePNG());
File.WriteAllBytes(Application.persistentDataPath + "/num"+int.Parse(phoo)+".jpg", bytes);
Debug.Log("saved: "+Application.persistentDataPath + "/num"+int.Parse(phoo)+".jpg");
}
}
}
}
function RotatePics() {
//transform.Rotate(Vector3 (0,90,0), 10 * Time.deltaTime);
if (onpremade == false){
MajorCube.transform.rotation.y=1;
onpremade = true;
}
else if (onpremade == true){
MajorCube.transform.rotation.y=0;
onpremade = false;
}
}
function TurnOffWebPlayerCamera(){
webCamTexture.Stop();
}
Your answer
Follow this Question
Related Questions
Save model from Blender in Unity and open it subsequently 0 Answers
How to make a save script in javascript. 1 Answer
Save/Load Player System 0 Answers
Save and load component values 0 Answers
Save/load not work on mobile phone 1 Answer