2017-09-13 41 views
0

隨時隨地這裏是我的腳本:生成ClickMouseButton(0)上展出

void Update() { 
    public float clickTimer = 2f; 
    clickTimer -= Time.deltaTime; 
    if(clickTimer <= 0){ 
     //"Generate Click" I try: Input.GetMouseButtonDown(0); 
     clickTimer = 2f; 
    } 
} 

我不想單擊任一特定的對象,因爲我有RayCastHit,我想生成點擊顯示的任何地方。

+3

而不是「生成點擊」你可能只是想模仿任何代碼,正常的鼠標點擊會做。所以如果你真正的鼠標點擊創建一個射線,你點擊發生器也應該生成一個射線。 – ryeMoss

+0

你需要點擊隨機或raycastRandom?或點擊rayCastHit屏幕位置? – joreldraw

+0

@joreldraw http://prntscr.com/gko2s6 http://prntscr.com/gko5rg這實際上是我的代碼。我只是想讓Unity在顯示的任何位置生成LeftClickDown,因爲在Cardboard應用程序中單擊的位置並不重要,它總是在屏幕中間生成Ray。 –

回答

0

使用此腳本(用於2D遊戲),您可以從屏幕中用鼠標單擊的位置創建RayCast,並檢查您已擊中的GameObjects。我建議你先標記任何你想要的遊戲對象被點擊

void Update() { 

    clickTimer -= Time.deltaTime; 

    if(clickTimer <= 0){ 

     if(Input.GetMouseButtonDown(0)) { 

      //What point was pressed 
      Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
      worldPoint.z = Camera.main.transform.position.z; 
      //Generate a Ray from the position you clicked in the screen 
      Ray ray = new Ray(worldPoint, new Vector3(0, 0, 1)); 
      //Cast the ray to hit elements in your scene 
      RaycastHit2D hit = Physics2D.GetRayIntersection(ray); 

      if(hit.collider != null) { 
       Debug.Log("I hit: "+hit.gameObject.tag); 
       //And here you can check what GameObject was hit 

       if(hit.gameObject.tag == "AnyTag"){ 
        //Here you can do whatever you need for AnyTag objects 
       } 
      } 
     } 

     clickTimer = 2f; 
    } 
} 
0

雖然這通常是不好的做法(您應該設計系統的方式只需要鼠標點擊時調用一個函數,然後您可以在任何地方調用該函數,因此您不需要模擬它),它是可以在C# .net的某個位置模擬鼠標點擊。

第一步是將鼠標光標移動到所需的位置(我知道你不想這樣做,你可以設置一些稍後的步驟來產生鼠標點擊,這只是爲了未來參考),Unity本身並沒有任何方法來控制鼠標光標。這意味着我要在這裏展示的示例只能在Windows機器上運行,您必須實現不同的代碼才能在OSX上運行。首先,我們必須包括啓動:

using System.Runtime.InteropServices; 
在我們的腳本

,這條線是什麼能夠讓我們導入user32.dll中,使我們可以重新定位的鼠標,我們想要的。所以,那麼下一階段是導入將使我們移動鼠標的功能,這是像這樣

[DllImport("user32.dll")] 
public static extern bool SetCursorPos(int X, int Y); 

現在,我們可以使用在我們的代碼此功能設置鼠標光標的位置,現在做請記住user32.dll僅適用於Windows,因此不能移植到其他操作系統。這是你避免這樣做的主要原因。

public void Start() 
{ 
    SetCursorPos(50, 50); 
} 

在Windows操作系統上0,0是屏幕的左上點,所以如果你想要得到屏幕的每一個解決你將不得不中心的座標不擴展到屏幕尺寸使用Screen類獲取顯示大小。

將鼠標正確定位後,我們所需要做的就是執行鼠標單擊,不幸的是,這是非常棘手的.net並沒有真正有任何內置的方式來做到這一點。此時最好的選擇是這樣的類,它可以爲我們執行鼠標操作:

public class MouseOperations 
{ 
    [Flags] 
    public enum MouseEventFlags 
    { 
     LeftDown = 0x00000002, 
     LeftUp = 0x00000004, 
     MiddleDown = 0x00000020, 
     MiddleUp = 0x00000040, 
     Move = 0x00000001, 
     Absolute = 0x00008000, 
     RightDown = 0x00000008, 
     RightUp = 0x00000010 
    } 

    [DllImport("user32.dll", EntryPoint = "SetCursorPos")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool SetCursorPos(int X, int Y);  

    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool GetCursorPos(out MousePoint lpMousePoint); 

    [DllImport("user32.dll")] 
    private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); 

    public static void SetCursorPosition(int X, int Y) 
    { 
     SetCursorPos(X, Y); 
    } 

    public static void SetCursorPosition(MousePoint point) 
    { 
     SetCursorPos(point.X, point.Y); 
    } 

    public static MousePoint GetCursorPosition() 
    { 
     MousePoint currentMousePoint; 
     var gotPoint = GetCursorPos(out currentMousePoint); 
     if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); } 
     return currentMousePoint; 
    } 

    public static void MouseEvent(MouseEventFlags value) 
    { 
     MousePoint position = GetCursorPosition(); 

     mouse_event 
      ((int)value, 
      position.X, 
      position.Y, 
      0, 
      0) 
      ; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct MousePoint 
    { 
     public int X; 
     public int Y; 

     public MousePoint(int x, int y) 
     { 
      X = x; 
      Y = y; 
     } 

    } 

} 

補充說,到項目中,我們可以使用這個類來模擬鼠標操作後,這是可以做到像這樣:

MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown | MouseOperations.MouseEventFlags.LeftUp); 

該行將執行左鍵單擊,鼠標事件功能採用預製的標誌,可以執行不同的鼠標功能。如果您只傳遞了LeftDown標誌,那麼它將保持鼠標左鍵不放,然後執行左鍵單擊。這就是爲什麼你還必須通過左側的標誌來獲得點擊。

Here's the code I used that worked flawlessly on my test project.

只是再次強調了這一點,你不應該做這種方式。這是一種笨重且不可靠的黑客攻擊。做到這一點的正確和好方法是讓正常的左鍵點擊簡單地調用一個函數,並且每當你想要左鍵點擊發生時,你可以調用該函數,而不必經過複雜的步驟來模擬一個。