Пример #1
0
    //TODO: Better Controller-Platform detection. Support for Xbox360Pad on Linux (it could already work, if not just add the appropriate name in UseOnPlatform)
    public static void RegisterControllers()
    {
        controllers = new List <ActionController> ();
        //initialize correct controller

        var os = SystemInfo.operatingSystem;

        Debug.Log(os);

        //If a keyboard is avaliable (aka : we are on PC) add it
        var keyboard = new KeyboardController();

        if (keyboard.useOnPlatform("keyboard"))
        {
            controllers.Add(keyboard);
            isOnPC = true;
        }

        //GetJoystickNames is not exported in WP8 builds because unity is dumb (it should just return 0 instead of #if)

#if !UNITY_WP8 || (UNITY_WP8 && UNITY_EDITOR)
        //Go trough every joystick and instantiate the correct controller (if it exist)
        var joysticks = Input.GetJoystickNames();
        Debug.Log("Found " + joysticks.Length + " joysticks");

        //instantiate valid controller for every platfom
        foreach (var joystick in joysticks)
        {
            var Xbox360Pad = new Xbox360Controller();

            if (Xbox360Pad.useOnPlatform(joystick))
            {
                Xbox360Pad.Start();
                controllers.Add(Xbox360Pad);
            }
        }
#endif

        //If we are on a touch enabled platform instantiate a touch controller
        var touchSensor = new TouchController();
        if (touchSensor.useOnPlatform("touch"))
        {
            controllers.Add(touchSensor);
        }

        if (controllers.Count == 1 && isOnPC)
        {
            currentController = 0; //Use keyboard if no controller and on PC
        }
        else if (isOnPC)
        {
            currentController = 1;
        }
    }