u3d 动态加载图片

除了WWW加载的另外的两种方法。

需要system.drawing dll

Texture2D CUTPicture2( string path)

    {
   
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  
        Image image = Image.FromStream(fs);
   
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    
        Texture2D texTmp = new Texture2D(image.Width, image.Height);
       
        texTmp.LoadImage(ms.ToArray());
        ms.Close();
        clipPanel.SetActive(true);
        clipPanel.GetComponent<MeshRenderer>().material.mainTexture = texTmp;
        //obj.GetComponent<MeshRenderer>().material.mainTexture = texTmp;
        return texTmp;


    }
    void CUTPicture(string path)
    {


        MemoryStream ms = new MemoryStream();
        label = "ms start";
        System.Drawing.Image imgTmp = System.Drawing.Image.FromFile(path);//通过路径获取图片
        label ="img :"+ path;
        //不管是什么格式都转换成Jpeg格式
        imgTmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        label = "imgTmp :";
        //如果图片的格式是Jpeg就直接获取图片格式即可
        Texture2D texTmp = new Texture2D(imgTmp.Width, imgTmp.Height);
        label = "texTmp :";
        texTmp.LoadImage(ms.ToArray());//根据字节数组载入图片.
        ms.Close();
        label = " ms.Close(); :";
        clipPanel.SetActive(true);
        clipPanel.GetComponent<MeshRenderer>().material.mainTexture = texTmp;
        //viewClipCamera.enabled = true;
        //isClipImg = true;


    }

你可能感兴趣的:(u3d 动态加载图片)