範例檔提供AR開發步驟與實作會使用到的模型
2016年10月29日 星期六
[Unity 3D] 透過php檔案存取MySql資料庫
網頁部分
安裝完成後,找到在此路徑下C:\xampp\htdocs放要執行SQL語法的網頁,如下:此範例圖示.php的檔案內容,內容是新增一筆資料到MySQL資料庫,完成後將檔案儲存在C:\xampp\htdocs底下。Unity 部分
讀取資料
using UnityEngine; using System.Collections; using System.IO; using System.Net; using UnityEngine.UI; public class rankingboard : MonoBehaviour { public string url; //url是指要連結的php檔案位置,此範例為127.0.0.1/select.php HttpWebRequest request; void Update() { StartCoroutine (reflashboard ()); } IEnumerator reflashboard() { yield return new WaitForSeconds (2f); request = (HttpWebRequest)WebRequest.Create(url); //request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); rankinginfo = sr.ReadToEnd(); string[] word = rankinginfo.Trim().Split(','); //word[0]就是讀到的第一筆資料 } }新增,刪除,修改資料(要執行那一個功能,是根據連結到的php檔案是執行刪除的sql語法還是刪除或修改的sql語法)
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Net;
public class ButtonClick : MonoBehaviour {
{
public string url; //url是指要連結的php檔案位置,此範例為127.0.0.1/insert.php
void Update()
{
WWWForm form = new WWWForm ();
form.AddField ("score", score.ToString ()); //score是php檔案內的$_POST['score'],單引號內的變數
form.AddField ("groups", team_name); //groups是$_POST['groups'],單引號內的變數
for (int i = 0; i < DBcolumn.Length; i++)
{
form.AddField (DBcolumn[i], inputfield[i].text);
}
WWW www = new WWW (url, form);
StartCoroutine (updatesco (www));
}
IEnumerator (updatesco (WWW www){
yield return www;
}
}
沒有範例
[Unity 3D] 白板功能(切換筆色,清除和恢復筆畫)和擷取螢幕畫面
結果呈現如下:
SCREEN SHOT : Unity 3D目前螢幕擷取方式有兩種
第一種 :
使用Unity 3D現成的Application.CaptureScreenshot("pic.png")函式來完成螢幕畫面的擷取,圖片儲存路徑預設為Application.persistentDataPath
第二種 :
使用ReadPixel的方式來完成螢幕畫面的擷取,程式碼如下:
public Camera mainCamera; IEnumerator ScreenCapture() { //在擷取畫面之前請等到所有的Camera都Render完 yield return new WaitForEndOfFrame(); Texture2D texture = new Texture2D((int)mainCamera.pixelWidth, (int)mainCamera.pixelHeight); //擷取全部畫面的資訊 texture.ReadPixels(new Rect(0, 0, (int)mainCamera.pixelWidth, (int)mainCamera.pixelHeight),0,0, false); texture.Apply(); }螢幕擷取後,儲存圖片:
void SaveTextureToFile(Texture2D texture, string fileName) { byte[] bytes = texture.EncodeToPNG(); string filePath = Application.dataPath + "/" + fileName + ".png"; using (FileStream fs = File.Open(filePath, FileMode.Create)) { BinaryWriter binary = new BinaryWriter(fs); binary.Write(bytes); } }程式碼參考自 -> http://www.iverv.com/2014/04/unityscreenshot.html
螢幕畫面擷取後儲存的路徑分類:
Application.dataPath
建議視窗開發中用的路徑:
windows: /Assets
IPone: Application/???/Name.app/Data
Android: /data/app/Name.apk
Application.persistentDataPath
Contains the path to a persistent data directory (Read Only).
平台中的公開目錄,文件持久性的保存不會因為應用程式更新或升級而刪除
windows: C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
IPone: Application/???/Documents
Android: /data/data/Name/files
Application.streamingAssetsPath
專案目錄下面的 Assets/StreamingAssets
windows: /Assets/StreamingAssets
IPone: Application/???/Name.app/Data/Raw
Android: jar:file:///data/app/Name.apk/!/assets
Application.temporaryCachePath
Contains the path to a temporary data / cache directory (Read Only).
平台的快取儲存路徑
windows: C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName
IPone: Application/???/Library/Caches
Android: /data/data/Name/cache
訂閱:
文章 (Atom)