Files
XMen/Assets/Scripts/DrawRender.cs
2025-07-10 14:49:53 +08:00

22 lines
794 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawRender : MonoBehaviour
{
public SpriteRenderer render;
public RenderTexture renderTexture;
public void Start()
{
// 创建一个临时的Texture2D用于存储RenderTexture的像素数据
Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
// 将RenderTexture的内容拷贝到Texture2D中
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
// 创建Sprite
Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.one * 0.5f);
render.sprite = sprite;
}
}