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 () {
	
	}
}
相关推荐
zdr尽职尽责6 天前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
ellis19708 天前
Claude Code[二] 上下文相关的机制
unity·ai编程
zdr尽职尽责9 天前
Unity 3C 动画系统:从「蜘蛛网」到可维护的动画架构
unity·架构·游戏引擎
国家一级摸鱼选手9 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
暴走约伯11 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
寂寞的船12 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
寂寞的船13 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无14 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
17岁的勇气15 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
寂寞的船16 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene