简体   繁体   English

改变材质的质感

[英]Changing the texture of material

I'd first like to apologise beforehand if my question is very basic and dumb as I am very new to coding and unity/vuforia in general.如果我的问题非常基本和愚蠢,我首先想事先道歉,因为我对编码和统一/vuforia 总体上还很陌生。

I'd like to track a custom image and display a quad under it (i've done this part).我想跟踪一个自定义图像并在它下面显示一个四边形(我已经完成了这部分)。 I've also managed to set the material/texture/look of the quad to a image located in my computer.我还设法将四边形的材质/纹理/外观设置为位于我计算机中的图像。 However, when trying to do build into a android app, I am unable to set the file path correctly and it doesnt work.但是,当尝试构建到 android 应用程序中时,我无法正确设置文件路径并且它不起作用。

I would like the material/texture/look of the quad to be constantly changing/updating, is this possible?我希望四边形的材质/纹理/外观不断变化/更新,这可能吗? I can constantly overwrite the image file on my computer/phone but will the photo update in the app itself?我可以不断覆盖计算机/手机上的图像文件,但应用程序本身中的照片会更新吗?

Thank you so much for any help in advance!非常感谢您提前提供任何帮助!

A desperate student绝望的学生

   void Start()
    {

        string path = "file://storage/emulated/0/Android/data/com.kenny.argame/files/im2.jpg";
        StartCoroutine(DownloadImage(path));
    }


    IEnumerator DownloadImage(string MediaUrl)
    {
        GetComponent<Renderer>().material = FinalMaterialRef;

        UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
            Debug.Log(request.error);
        else
            _material.mainTexture = ((DownloadHandlerTexture)request.downloadHandler).texture;
        FinalMaterialRef = _material;


    }

}

While it's not clear from your code where _material and FinalMaterialRef are originally declared -- they're not in the scope you showed -- you might have your references mixed up and overwrite them, but not the actual material.虽然从您的代码中_materialFinalMaterialRef最初是在哪里声明的——它们不在您展示的范围内——但您可能将引用混淆并覆盖它们,但不是实际的材料。 This works:这有效:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class ChangeTexture : MonoBehaviour
{
    Material material = null;

    void Start()
    {
        material = GetComponent<Renderer>().material;

        const string path = "file://E:/_temp/1.jpg";
        StartCoroutine(DownloadAndAssignImage(path));
    }

    IEnumerator DownloadAndAssignImage(string mediaUrl)
    {
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(mediaUrl);
        yield return request.SendWebRequest();

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
        }
        else
        {
            material.mainTexture =
                ((DownloadHandlerTexture)request.downloadHandler).texture;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM