iT邦幫忙

2

[已解決]Unity 匯出到android手機後 材質變粉紅色

  • 分享至 

  • xImage

想要讀取手機資料夾內的圖片,貼到新增的物件cube上,最後在手機上顯示
build完後雖然手機上有出現物件,可是材質是粉紅色的

有在unity裡測試圖片是正常顯示的,如圖下
https://ithelp.ithome.com.tw/upload/images/20181126/20110063cefiuINZBd.jpg

手機內執行結果,如圖下
https://ithelp.ithome.com.tw/upload/images/20181126/20110063vo6PYIwU1N.jpg

確定手機資料夾是有圖片的
https://ithelp.ithome.com.tw/upload/images/20181126/20110063pjmelqiuh8.jpg

程式碼


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的問題?
拜託大家幫幫忙,謝謝~~

wjing iT邦新手 1 級 ‧ 2018-11-26 23:56:22 檢舉
透過加入shader後解決此問題~

到以下網址下載shader
https://github.com/KrzysztofBogdan/Unity-Builtin-Shaders-2017.2.0f3

把Standard.shader加到Resources
更改程式碼 cube1.gameObject.GetComponent<Renderer>().material.mainTexture = myTexture;

改成
Shader myShader = Resources.Load("Standard") as Shader;
Material material = new Material(myShader);
material.mainTexture = myTexture;
cube1.gameObject.GetComponent<Renderer>().material = material;
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答