---
tags: 遊戲製作
---
# Making cake (AR Game):
This is an app for young children made by Unity and Vuforia. Children can use this software to make different styles of cakes, show their creativity, and take pictures with them at the end.
## how to play:
Through the teaching steps, users can choose the cake flavor they want to scan the picture card, and then, they can piece together their own cake, and at the end, they can take a photo with their cake.
---
## production experience:
In fact, this file was not built at the end, because we didn't notice that it can only be built in the Android environment during production, but we designed it with the UI and program of the computer, so this APP At present, it can only be operated in Unity. At the end of the production, I was deeply impressed by the fact that if there is a plug-in to be used, it must be built and confirmed.
## Codes
This program adds Vuforia API in order to realize AR environment
Web: https://developer.vuforia.com/


And some of its functions need to be implemented on the website, such as detecting certain pictures, if detected, other actions will be performed. These need to be performed on the website, so they need to be connected.
The file reading system used in this app is briefly shown here:
```cpp=
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ReadAndWrite : MonoBehaviour
{
void Start()
{
//Specify the generated file location, usually in Assets
string path = Application.dataPath + "/test.txt";
//Generates one if it doesn't exist
if (!File.Exists(path))
{
string createText = "Hello and Welcome\n";
//Overwrite text in .txt
File.WriteAllText(path, createText);
}
string appendText = "This is extra text\n";
//Add text into .txt
File.AppendAllText(path, appendText);
//read text in .txt
string readText = File.ReadAllText(path);
Debug.Log(readText);
}
}
```
---
Here is the way to take photo:
```cpp=
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using UnityEngine.UI;
/// <summary>
/// 截圖儲存安卓手機相簿
/// </summary>
public class CaptureScreenshotMgr : MonoBehaviour
{
public Text text;
string _name = "";
/// <summary>
/// 儲存截圖圖片,並且重新整理相簿 Android
/// </summary>
/// <param name="name">若空就按照時間命名</param>
public void CaptureScreenshot()
{
_name = "";
_name = "Screenshot_" + GetCurTime() + ".png";
#if UNITY_STANDALONE_WIN //PC平臺
// 編輯器下
// string path = Application.persistentDataPath + "/" + _name;
string path = Application.dataPath + "/" + _name;
ScreenCapture.CaptureScreenshot(path, 0);
Debug.Log("圖片儲存地址" + path);
#elif UNITY_ANDROID //安卓平臺
//Android版本
StartCoroutine(CutImage(_name));
//在手機上顯示路徑
// text.text = "圖片儲存地址" +
// Application.persistentDataPath.Substring(0,
// Application.persistentDataPath.IndexOf("Android"))
// + "/DCIM/Camera/" + _name;
text.text = "圖片儲存地址" +
Application.persistentDataPath.Substring(0,
Application.persistentDataPath.IndexOf("Android"))
+ "/截圖/" + _name;
#endif
}
//截圖並儲存
IEnumerator CutImage(string name)
{
//圖片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height,
TextureFormat.RGB24, true);
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height),
0, 0, true);
tex.Apply();
yield return tex;
byte[] byt = tex.EncodeToPNG();
string path = Application.persistentDataPath.Substring(0,
Application.persistentDataPath.IndexOf("Android"));
// File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt);
// //儲存到 安卓手機的 DCIM/下的Camera 資料夾下
File.WriteAllBytes(path + "/截圖/" + name, byt);
//儲存到安卓手機的 檔案管理下面的 《截圖》資料夾下
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
//重新整理圖片,顯示到相簿中
void ScanFile(string[] path)
{
using (AndroidJavaClass PlayerActivity = new
AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject playerActivity =
PlayerActivity.GetStatic<AndroidJavaObject>
("currentActivity");
using (AndroidJavaObject Conn = new
AndroidJavaObject("android.media.MediaScannerConnection",
playerActivity, null))
{
Conn.CallStatic("scanFile", playerActivity, path,
null, null);
}
}
}
/// <summary>
/// 獲取當前年月日時分秒,如20181001444
/// </summary>
/// <returns></returns>
string GetCurTime()
{
return DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() +
DateTime.Now.Minute.ToString() +
DateTime.Now.Second.ToString();
}
}
```
## Responsible for:
1. Planning (total 2 people)
2. Coding (total 1 people) all code by my self
## More Information
Video: https://youtu.be/72OchFer6Pg
Github: https://github.com/andy091045/A-piece-of-cakes
HackMD: https://hackmd.io/@andy091045/H1wqAkzu9