unity切换场景后动态截图并使用

代码演示

 	public Image im;
    public bool isSet = false;
    void Start()
    {
        if (isSet) {
            if (im.sprite == null &&guiManager.Instance.tex!=null&&guiManager.Instance.Scenepos!=0) {
            	//将Texture2D转化成Image图片并应用到场景中
                im.sprite = Sprite.Create(guiManager.Instance.tex[guiManager.Instance.Scenepos-1], new Rect(0, 0, Screen.width, Screen.height), Vector2.zero);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        StartCoroutine("SetTexture",new Rect(0, 0, Screen.width, Screen.height));
    }
    IEnumerator SetTexture(Rect rec) {
        yield return new WaitForEndOfFrame();
        Texture2D tex1 = new Texture2D((int)rec.width, (int)rec.height, TextureFormat.RGB24, false);
        tex1.ReadPixels(rec, 0, 0);
        tex1.Apply();//一定要Apply();
        guiManager.Instance.tex[guiManager.Instance.Scenepos] = tex1;
    }

注:要使用协程进行动态截图,因为这个截图要在渲染完成之后进行截图

你可能感兴趣的:(unity笔记,unity,游戏引擎)