想要讀取手機資料夾內的圖片,貼到新增的物件cube上,最後在手機上顯示
build完後雖然手機上有出現物件,可是材質是粉紅色的
有在unity裡測試圖片是正常顯示的,如圖下
手機內執行結果,如圖下
確定手機資料夾是有圖片的
程式碼
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class cube : MonoBehaviour
{
private GameObject cube1;
private Texture2D myTexture;
public GameObject Items;
public TextMeshProUGUI t;
// Use this for initialization
void Start()
{
Directory.CreateDirectory(Application.persistentDataPath + "/jpg");
cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube1.transform.position = new Vector3(0, 1, 5);
cube1.transform.localScale = new Vector3(10, 10, 1);
StartCoroutine(LoadLocal());
}
// Update is called once per frame
void Update()
{
cube1.transform.Rotate(Vector3.up * Time.deltaTime * 50f, Space.World);
}
IEnumerator LoadLocal()
{
WWW png = new WWW("file:///" + Application.persistentDataPath + "/jpg/car1.png");
yield return png;
myTexture = png.texture;
if (png != null && string.IsNullOrEmpty(png.error))
{
t.text = "yes";
}
cube1.gameObject.GetComponent<Renderer>().material.mainTexture = myTexture;
}
}
有上網爬文過,可能是Shader的問題?
拜託大家幫幫忙,謝謝~~