• 设置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(); }
    /// <summary>
    /// 根据json image中的base64数据还原贴图.
    /// </summary>
    /// <param name="imageStr"></param>
    private void setTextureByJsonData(string imageStr)
    {
    Debug.Log("开始 设置 材质球 来自json的贴图...");
    var base64Data = Regex.Match(imageStr, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
    byte[] bytes = Convert.FromBase64String(base64Data);
    Texture2D tex = new Texture2D(100,100);
    tex.LoadImage(bytes);
    Renderer renderer = game.GetComponent<Renderer>();
    renderer.material.SetTexture("_MainTex",tex);
    }
     
  • 相关阅读:
    第二周例行报告
    洛谷 P3384 【模板】轻重链剖分
    洛谷 P3380 【模板】二逼平衡树(树套树)
    洛谷 P4568 [JLOI2011]飞行路线
    2018 ICPC Asia Nanjing Regional Preliminary L. Magical Girl Haze
    牛客 2020 牛客国庆集训派对 day8 G. Shuffle Cards
    洛谷 P3224 [HNOI2012]永无乡
    洛谷 P1486 [NOI2004]郁闷的出纳员
    洛谷 P3391 【模板】文艺平衡树
    洛谷 P3369 【模板】普通平衡树
  • 原文地址:https://www.cnblogs.com/leeplogs/p/6713759.html
Copyright © 2020-2023  润新知