Frage

is it possible to launch unity from an android activity passing an image and then using that image as a sprite? e.g using something like:

Intent i = new Intent(this, UnityActivity.class);
i.putExtra(imageFile);
startActivity(i);

And if this is possible how do you handle the received image in unity?

Thanks

War es hilfreich?

Lösung

I solved my own problem by saving the image to a specific location in the android activity:

private static boolean storeImage(Bitmap imageData, String filename) {
        //get path to external storage (SD card)
        String iconsStoragePath = Environment.getExternalStorageDirectory() + "/gameFiles/myImages/";
        File sdIconStorageDir = new File(iconsStoragePath);

        //create storage directories, if they don't exist
        sdIconStorageDir.mkdirs();

        try {
            String filePath = sdIconStorageDir.toString() + "/" + filename;
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);

            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);


            imageData.compress(CompressFormat.PNG, 100, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        }

        return true;
    }

And then I used this image to create a sprite in unity by referencing the location I had saved the file in the android activity:

IEnumerator Start()
   {
       string path = "file:///mnt//sdcard//gameFiles//myImages//img.png";
       WWW www = new WWW(path);
       yield return www;

       SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
       Sprite sprite = new Sprite();
       sprite = Sprite.Create(www.texture, new Rect(-0.7f, -0.7f, 330, 440), new Vector2(0.5f, 0.5f), 150.0f);

       renderer.sprite = sprite;
       Time.timeScale = 0;
   }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top