Search Unity

Mouse lock in Flash export?

Discussion in 'Flash' started by RetronamicGames, Oct 17, 2013.

  1. RetronamicGames

    RetronamicGames

    Joined:
    Feb 9, 2013
    Posts:
    84
    Flash Player 11.2 introduced the long-awaited mouse lock feature. How can we make use of that feature in Unity Flash games?
     
  2. DDowell

    DDowell

    Joined:
    Feb 8, 2012
    Posts:
    52
    Custom ActionScript code.
     
  3. DDowell

    DDowell

    Joined:
    Feb 8, 2012
    Posts:
    52
    Here is an example of a script that will (in fullscreen mode) lock the cursor and enable mouse look.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Flash;
    5.  
    6. public class MouseLock : MonoBehaviour
    7. {
    8.     public MonoBehaviour MouseLook;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         //Add a fullscreen button to your GUI using Flash display object (I just use a sprite here).
    14.         ActionScript.Import("flash.display.Sprite");
    15.        
    16.         ActionScript.Import("com.unity.UnityNative");
    17.         ActionScript.Import("flash.display.Stage");
    18.         ActionScript.Import("flash.events.MouseEvent");
    19.        
    20.         ActionScript.Statement("var spr:Sprite = new Sprite()");
    21.         ActionScript.Statement("UnityNative.stage.addChild(spr)");
    22.  
    23.         ActionScript.Statement("spr.graphics.beginFill(0xFF0000)");
    24.         ActionScript.Statement("spr.graphics.drawRect(0,0,200,40)");
    25.         ActionScript.Statement("spr.graphics.endFill()");
    26.  
    27.         ActionScript.Statement("spr.addEventListener(MouseEvent.CLICK, HandleFullScreenButtonClicked)");
    28.     }
    29.  
    30.     [NotRenamed]
    31.     public void HandleFullScreenButtonClicked(object @flashEvent)
    32.     {
    33.         ActionScript.Import("com.unity.UnityNative");
    34.         ActionScript.Import("flash.display.Stage");
    35.         ActionScript.Import("flash.display.StageDisplayState");
    36.  
    37.         var fullScreen = ActionScript.Expression<bool>("UnityNative.stage.displayState == StageDisplayState.FULL_SCREEN");
    38.         if (fullScreen)
    39.         {
    40.             ActionScript.Statement("UnityNative.stage.mouseLock = false");
    41.  
    42.             ActionScript.Statement("UnityNative.stage.displayState = StageDisplayState.NORMAL");
    43.  
    44.             MouseLook.enabled = false;
    45.         }
    46.         else
    47.         {
    48.             ActionScript.Statement("UnityNative.stage.displayState = StageDisplayState.FULL_SCREEN");
    49.  
    50.             ActionScript.Statement("UnityNative.stage.mouseLock = true");
    51.  
    52.             MouseLook.enabled = true;
    53.         }
    54.     }
    55.  
    56.     // Update is called once per frame
    57.     void Update () {
    58.    
    59.     }
    60. }
    61.  
    Just attach the script to a game object and link up an instance of MouseLook.