unity以战斗截图并加上微信二维码分享

开发游戏时,会遇到战斗结算,成就等奖励界面,鼓励玩家分享到朋友圈,这时通常会在图片上加上一些内容,比如游戏Logo,二维码下载等内容。

现在分享一下我制作游戏分享图片的过程。

第一步首先截图,截屏的方法有一个unity提供的方法,CaptureScreenshot,但并不推荐使用这个方法。一来这就是一个截屏函数,必须放在startcortinoute中,二来耗时比较长。

第二种是使用RenderTexture的方法,直接利用相机的渲染输出到Texture2D中。现假设将两个相机的内容输到一个图片上。

主相机视野为:

第二个相机视野为

两个相机综合输出的视野为

。贴代码

csharp 复制代码
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RenderTextureTest : MonoBehaviour {

	RawImage showImage = null;
	Camera mCam = null;
	Camera Came2 = null;
	RenderTexture RenderTex = null;
	Texture2D RenderSavedTexure2d = null;
	// Use this for initialization
	void Start () {
		showImage = GameObject.Find("ShowRenderTexure").GetComponent<RawImage>();   //显示输出的渲染图片
		mCam = Camera.main;
		Came2 = GameObject.Find("Camera2").GetComponent<Camera>();
		RenderTex = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.ARGB32);//初始化存储
		RenderSavedTexure2d = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
		//screenUITex.mainTexture = RenderSavedTexure2d; // 如果用NGUI UITexure显示图片
		showImage.texture = RenderSavedTexure2d;
	}

	void RenderCam()
	{
		if (null != mCam && null != Came2)
		{
			mCam.targetTexture = RenderTex;  //降相机的targetTexture指向RenderTex,即输出到RenderTex
			mCam.Render();
			Came2.targetTexture = RenderTex;
			Came2.Render();

			RenderTexture.active = RenderTex;//激活RenderTexture

			RenderSavedTexure2d.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);  //将RenderTexture内容读到RenderSavedTexure2d
			RenderSavedTexure2d.Apply();//应用RenderSavedTexure2d

			mCam.targetTexture = null;
			Came2.targetTexture = null;

			RenderTexture.active = null;
			
		}
	}

	public void ClickRenderCame()
	{
		RenderCam();
	}
	// Update is called once per frame
	void Update () {
	
	}
}
相关推荐
weixin_424294675 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames5 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy3258643645 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs5 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0125 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋5 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕5 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
小菱形_5 天前
【Unity】TimeLine
unity·游戏引擎
小贺儿开发6 天前
Unity3D 自动化物流分拣模拟
运维·科技·unity·自动化·人机交互·传送带·物流分拣
EQ-雪梨蛋花汤6 天前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化