2017-09-13 36 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.

只是再次强调了这一点,你不应该做这种方式。这是一种笨重且不可靠的黑客攻击。做到这一点的正确和好方法是让正常的左键点击简单地调用一个函数,并且每当你想要左键点击发生时,你可以调用该函数,而不必经过复杂的步骤来模拟一个。