1. 程式人生 > >3dContactPointAnnotationTool開發日誌(二)

3dContactPointAnnotationTool開發日誌(二)

  今天看的時候發現其實www的方式是可以根據指定路徑讀取本地圖片到Image中的。也就是昨天提到的第二種方式。
  隨便選了個圖片做示範:
2.jpg
1.png
修改後的程式碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonOkOnClick : MonoBehaviour {
    public InputField imagePath;
    public Image referenceImage;
    
    public void Click() {
        Debug.Log("onClick");
        StartCoroutine(GetImage(imagePath.text));
    }
    private IEnumerator GetImage(string path) {
        Debug.Log(path);
        Debug.Log(path.Replace('\\', '/'));
        //WWW www = new WWW("file://"+path.Replace('\\','/'));

        WWW www = new WWW("file://" + "C:/Users/A/Desktop/2.jpg");
        Debug.Log(www.url);
        yield return www;
        if (www != null && string.IsNullOrEmpty(www.error))
        {
            Texture2D texture = new Texture2D(www.texture.width, www.texture.height);
            texture.SetPixels(www.texture.GetPixels());
            texture.Apply(true);
            texture.filterMode = FilterMode.Trilinear;
            referenceImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
        }
        else {
            Debug.Log("no such image!");
        }
    }
}