請記得 using System.Drawing,如果開發者沒有這個類別。
請自行下載System.Drawing dll 檔案。
System.Drawing dll 下載
------------------------------------------
/*********************************************************************************
* 模擬滑鼠 user32
* 參考文獻 : http://pinvoke.net/default.aspx/user32.mouse_event
https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms646260(v=vs.85).aspx
********************************************************************************/
using UnityEngine;
using System.Collections;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
class MouserClass {
[DllImport("user32")]
static extern bool GetCursorPos(ref Point getPoint);//取得滑鼠座標
private Point getPoint;
private bool getViewSw;
// 取得滑鼠開關
public bool getViewSwmassage(bool getViewSw)
{
return this.getViewSw = getViewSw;
}
//取得當前滑鼠偵測座標
public string getMosuPointSwMessage()
{
string mosuPointSwMessage = (getViewSw.Equals(true)) ? "正在偵測" : "取消偵測";
return mosuPointSwMessage;
}
//取得當前滑鼠座標
public void GetmouserPostatic()
{
while (getViewSw.Equals(true))
{
GetCursorPos(ref getPoint);
}
Thread.Sleep(1000);
}
//取得滑鼠座標
public string getMousePosistionMessage()
{
return getPoint.ToString();
}
// 終止所有執行緒
public void Stop()
{
getViewSw = false;
}
}
public class ThreadTest : MonoBehaviour {
MouserClass mouserClass;
GUIStyle guistyle;
Thread GetmousePointTime;
public bool getMousePosistionSw = true;
void Start() {
mouserClass = new MouserClass();
guistyle = new GUIStyle();
}
void Update() {
if (getMousePosistionSw.Equals(true))
{
// 取得當前滑鼠
GetmousePointTime = new Thread(mouserClass.GetmouserPostatic);
GetmousePointTime.Start();
}
mouserClass.getViewSwmassage(getMousePosistionSw);
}
void OnGUI() {
guistyle.fontSize = 30;
guistyle.normal.textColor = UnityEngine.Color.white;
// 讀取執行緒當前滑鼠座標
if (GUI.Button(new Rect(Screen.width / Screen.width * 5, Screen.height / Screen.height * 50, 100, 30), "當前滑鼠座標"))
getMousePosistionSw = !getMousePosistionSw;
GUI.Label(new Rect(Screen.width / Screen.width * 110, Screen.height / Screen.height * 50, 50, 20), mouserClass.getMosuPointSwMessage(), guistyle);
GUI.Label(new Rect(Screen.width / Screen.width * 230, Screen.height / Screen.height * 50, 50, 20), mouserClass.getMousePosistionMessage(), guistyle);
}
// 關掉軟體
void OnApplicationQuit()
{
mouserClass.Stop();
}
}
---------------------------------------------------
執行結果:
沒有留言:
張貼留言