设置texture

Posted lipper_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置texture相关的知识,希望对你有一定的参考价值。

//获取内部资源贴图
    public void setInsideTexture()
    {
        Texture2D texture = Resources.Load(texture_url) as Texture2D;
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = texture;
    }

    //获取外部资源贴图(本地路径)
    public void setExternalTextureByIO()
    {
        //创建文件读取流
        FileStream fileStream = new FileStream(texture_url, FileMode.Open,
            FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);
        //创建文件长度缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes, 0, (int) fileStream.Length);
        //释放文件读取流
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;

        //创建Texture
        int width = 800;
        int height = 640;
        Texture2D texture = new Texture2D(width, height);
        texture.LoadImage(bytes);
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = texture;

    }

    /// <summary>
    /// 以WWW方式进行加载
    /// </summary>
    private void setExternalTextureByWWW()
    {
        StartCoroutine(LoadWWW());
    }
IEnumerator LoadWWW() {
//请求WWW WWW www = new WWW(texture_url); yield return www; Renderer renderer = GetComponent<Renderer>(); renderer.material.mainTexture = www.texture; Debug.Log(www.ToString()); Stream outStream = File.Create(ExportClass.prePath + "test.png"); byte[] buffer = www.bytes; outStream.Write(buffer, 0, buffer.Length); outStream.Close(); }

 

以上是关于设置texture的主要内容,如果未能解决你的问题,请参考以下文章

初识OpenGL (-)纹理(Texture)

初识OpenGL (-)纹理(Texture)

OpenGL 纹理学习总结

如何在片段着色器中进行自定义模板测试

[设置纹理时内存泄漏=新的Texture2D(…)

片段着色器中未使用纹理数据 - OpenGL