Yuxiang-Ma / 2.12-Hololens2

Unity project for Hololens2
1 stars 0 forks source link

Read image and display to the Hololens (Unity) #3

Closed young-j-park closed 1 year ago

young-j-park commented 1 year ago
  1. Add RawImage object
  2. Add script as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LoadIMGLocal : MonoBehaviour
{
    GameObject image;
    string filename = "C:\\Users\\yjpar\\2.12-Hololens2\\img\\ur5_force.png";

    void Start()
    {
        var rawData = System.IO.File.ReadAllBytes(filename);
        Texture2D tex = new Texture2D(2, 2); // Create an empty Texture; size doesn't matter (she said)
        tex.LoadImage(rawData);

        image = GameObject.Find("RawImage2");
        image.GetComponent<RawImage>().texture = tex;
    }

    void Update()
    {
        Texture2D tex = new Texture2D(2, 2); // Create an empty Texture; size doesn't matter (she said)

        try
        {
            var rawData = System.IO.File.ReadAllBytes(filename);
            tex.LoadImage(rawData);
            image.GetComponent<RawImage>().texture = tex;
        }
        catch
        {
            print("Failed to load image.");
        }
    }
}