2017年1月14日 星期六

[WPF] 抓取網頁資料

Step 1: 工具 > NuGet封裝管理員 > 管理方案的NuGet套件

Step 2: 瀏覽 > 搜尋HtmlAgilityPack > 安裝HtmlAgilityPack

*xpath是使用HAPXPathFinder v0.9軟體來尋找出來的,範例檔有提供

Step 3: Coding
using HtmlAgilityPack;
using System.Net;
using System.IO;

string url = "http://www.just-the-word.com/main.pl?word=hello";
string xpath = "/html[1]/body[1]/div[1]/div[3]/div[1]";  //xpath是使用HAPXPathFinder v0.9軟體來尋找出來的

HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
try
{
    HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

     Stream stream = httpWebResponse.GetResponseStream();
     StreamReader reader = new StreamReader(stream, Encoding.UTF8);
     string s = reader.ReadToEnd();
     reader.Close();
     stream.Close();
     httpWebResponse.Close();

     HtmlDocument htmlDoc = new HtmlDocument();

     htmlDoc.LoadHtml(s);
     //16GB 32GB 64GB 的運送時間XPATH
     HtmlNode anchors = htmlDoc.DocumentNode.SelectSingleNode(xpath);
     //HtmlNode anchors32 = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[3]/div/div[2]/div[2]/div[3]/ul/li[2]/label/span/span[3]/span");
     //HtmlNode anchors64 = htmlDoc.DocumentNode.SelectSingleNode("/html/body/div[2]/div[3]/div/div[2]/div[2]/div[3]/ul/li[3]/label/span/span[3]/span");

     //output
     //set RichTextBox
     richTextBox.Document.Blocks.Add(new Paragraph(new Run(anchors.InnerText)));
     //get RichTextBox
     //string richText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
}
catch (WebException web)
{
     //error message
}

完成

參考資料:
http://www.just-the-word.com/main.pl?word=walk&mode=combinations#N N*

範例檔下載

2017年1月9日 星期一

[Unity 3D] Text放大不失焦和調整AR拍攝距離

Text放大不失焦

調整AR拍攝距離
AR Camera > Camera > 調整屬性Field of View的數值

[Unity 3D] Image元件和Toggle按鈕(複選題)

從屬性欄位設定Image Source,顯示圖片
Step 1:

Step 2:

Toggle按鈕(複選題)
Step 1:
Step 2:
Step 3:

[Unity 3D] Keeping the object on the screen even when the target is lost


讓AR Camera 偵測到目標物後,產生的虛擬物件不會消失的方法有兩種:

The first one : 設定屬性
Step 1 : 要把ImageTarget 放到 ARCamera階層下.
Step 2 : Enable Extended Tracking 的選項打勾.

The Second one : 修改腳本
The script that handles what happens when tracking is lost is called DefaultTrackableEventHandler.cs and is found in Assets > Vuforia > Scripts. 
In that file you will find a function OnTrackingLost() This function disables all the renderComponents and colliderComponents for each of the children of the ImageTarget. If you want your object to stay visible comment out the following foreach loops like so:
private void OnTrackingLost()  //當目標物遺失就執行這方法
{
    Renderer[] rendererComponents = GetComponentsInChildren(true);
    Collider[] colliderComponents = GetComponentsInChildren(true);
    /*
    // Disable rendering:
    foreach (Renderer component in rendererComponents)
    {
        component.enabled = false;  //把false改成true,當目標物遺失虛擬物件不會跟著消失
    }

    // Disable colliders:
    foreach (Collider component in colliderComponents)
    {
        component.enabled = false;   //把false改成true,當目標物遺失虛擬物件不會跟著消失

    }
    */
    Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}

2016年10月29日 星期六

[Unity 3D] Vuforia結合Unity實作AR

完成結果 : 沉思的骷顱頭

範例檔提供AR開發步驟與實作會使用到的模型

[Unity 3D] 透過php檔案存取MySql資料庫

網頁部分
安裝架站伺服器XAMPP (https://www.apachefriends.org/zh_tw/index.html)
安裝完成後,找到在此路徑下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