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 () {
	
	}
}
相关推荐
tealcwu1 天前
【Unity基础】为什么游戏需要物理按键映射?从 Unity Input Manager 到跨引擎输入设计
java·游戏·unity
tealcwu2 天前
【Unity高级】Unity URP:构建同时支持 2D 与 3D 场景的项目完全指南
3d·unity·游戏引擎
淡海水2 天前
10-01-互操作-C#与C++边界的内存管理
unity·c#·游戏引擎·.netcore·gc
小小数媒成员2 天前
UGUI原理(一)
开发语言·游戏·unity·游戏引擎
暴走约伯2 天前
【VFX工具链源码拆解 04】拯救丢失的绑定:Timeline 一键克隆与层级相对路径重映射算法
unity·c#·游戏引擎·硬件工程
小小数媒成员2 天前
Unity UGUI布局组件(LayoutGroup、ContentSizeFitter、LayoutElement)详解
unity·游戏引擎
tealcwu2 天前
【Unity基础】Unity输入与Time.deltaTime的使用原则
unity·游戏引擎
郝学胜-神的一滴3 天前
AI 编程智能体 22:解析器赋能与实战落地全指南
人工智能·程序人生·游戏·unity·游戏引擎
BuHuaX3 天前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
peter67684 天前
unity2d游戏开发经验总结
unity