[第二十二天]从0开始的UnityAR手机游戏开发-APP内撷取画面

在玩AR APP时都有一个拍照功能,本次章节会介绍如何撰写Unity的截图功能

先在Project新增一个C# Sprite并命名为Photo
打开Photo撰写程序码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Photo : MonoBehaviour
{

    [Header("档案名称")]
    public string FileName;
    int FileNumber;
    [Header("APP上的UI")]
    public GameObject[] UIs;

    public void CapturePhoto() {
        //先将UI全部关闭
        for (int i = 0; i < UIs.Length; i++) {
            UIs[i].SetActive(false);
        }
        //每按一次按钮FileNumber会+1
        FileNumber += 1;
        //图片名称为IThomeAR + FileNumber.png
        FileName = "IThomeAR" + FileNumber + ".png";
        //截图的程序码
        ScreenCapture.CaptureScreenshot(Application.persistentDataPath + FileName);
        //2秒後执行打开UI的函式
        Invoke("openUI", 2);
    }
    void openUI() {
        //将UI全部开启
        for (int i = 0; i < UIs.Length; i++)
        {
            UIs[i].SetActive(true);
        }
    }
}

在Hierarchy新增一个Button并将Text改为拍照。
将Photo脚本拖曳给EventSystem,点击EventSystem打开Inspector在Photo里有个UIs把Size改为Canvas下有在画面上的UI数量,像下图中有父物件包着多个Button的话只要拉父物件到UIs就可以了
https://ithelp.ithome.com.tw/upload/images/20211006/20140569eHgPZxntpO.png

点击新增的Button打开Inspector点击OnClick()+号把EventSystem拖曳到GameObject(None)选择Photo→CapturePhoto
https://ithelp.ithome.com.tw/upload/images/20211006/20140569xY10Ed3az0.png

按下执行键後点击拍照就会有照片在电脑里罗
https://ithelp.ithome.com.tw/upload/images/20211006/20140569lzLhsbdC4d.png

电脑拍照後所撷取的路径在C:\Users\user\AppData\LocalLow\DefaultCompany
如果找不到截图路径的话可以参考这篇
https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html


下一章节会教大家如何在APP开启网站


<<:  22 | WordPress 查询回圈区块 Query Loop Block

>>:  ## D21 - 彭彭的课程# Python 乱数与统计模组(1)

Day25 安装 Heartbeat

今日本篇重点是要安装Heartbeat,以如何设定要监控的服务项目。 Heartbeat 安装 步骤...

Day 21 - Vue Router基本概念(1)

如同我们前面几天提到的,Vue的核心是用来处理状态、版面、以及元件的逻辑。 但是当整个网站逐渐发展成...

DAY3 安装Android Studio

人们常说学语言在特定环境学会比较快,所以像是学英文把自己丢到美国去学是最快的,学台语就回家跟阿嬷吃饭...

[Day 20] 资料标注 (1/2) — Forget about the price tag ♫

The only thing that never changes is that everyth...

Day 28 JavaScript < 简单介绍>

1.JS是什麽? Java Script 是一种运行在客户端的脚本语言 (script就是脚本的意思...