ミッドランドブログ

長めのお知らせ、紹介記事を掲載します

Steam : 審査だけ通したい人向け。たぶん一番簡単なコントローラー対応

マウスで遊ぶゲームをSteamでコントローラー対応にする方法です。

実際に遊べなくていいから、コントローラー対応にして審査に通りたいという人のお役に立てるかもしれません。 ※Windowのみ対応です

f:id:MIDLAND_TESTG:20211206101147p:plain
コントローラーの対応
十字キーでカーソルを動かし、ABXYボタンでマウスダウンを操作をするという無理矢理な方法ですが、審査は通るようです。

CursorController.csというファイルを作成し、適当なブジェクトにアタッチしてください

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class CursorController : MonoBehaviour
{
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;
        public static implicit operator Vector2(POINT p)
        {
            return new Vector2(p.X, p.Y);
        }
    }


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


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out POINT lpPoint);
  
    [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlag, int dx, int dy, int cButtons, int dwExtraInfo);

    private const int m_mouse_leftDown = 0x2;
    private const int m_mouse_leftUp = 0x4;
    private const int m_mouse_rightDown = 0x8;
    private const int m_mouse_rightUp = 0x10;
    
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    unsafe  void Update () {
        if (Input.GetButton("Horizontal")) {
            POINT pos;
            Vector2 pos2 = Vector2.zero;

            if(GetCursorPos(out pos))
            {
                Debug.Log("x:"+pos.X+"    y:"+pos.Y);
                if (Input.GetAxis("Horizontal") < 0.0) { 
                    SetCursorPos(pos.X - 2, pos.Y);
                } else {
                    SetCursorPos(pos.X + 2, pos.Y);
                }
            }

        }
        
        if (Input.GetButton("Vertical")) {
            POINT pos;
            Vector2 pos2 = Vector2.zero;

            if(GetCursorPos(out pos))
            {

                if (Input.GetAxis("Vertical") < 0.0) { 
                    SetCursorPos(pos.X, pos.Y + 2);
                } else {
                    SetCursorPos(pos.X, pos.Y - 2);
                }
            }

        }

        if (Input.GetKeyDown ("joystick button 0")) {
            mouse_event(m_mouse_leftDown, 0, 0, 0, 0);
        } else if (Input.GetKeyDown ("joystick button 1")) {
            mouse_event(m_mouse_leftDown, 0, 0, 0, 0);
        } else if (Input.GetKeyDown ("joystick button 2")) {
            mouse_event(m_mouse_leftDown, 0, 0, 0, 0);
        } else if (Input.GetKeyDown ("joystick button 3")) {
            mouse_event(m_mouse_leftDown, 0, 0, 0, 0);
        } else if (Input.GetKeyDown ("space")) {
            mouse_event(m_mouse_leftDown, 0, 0, 0, 0);
        }
 
        if (Input.GetKeyUp ("joystick button 0")) {
            mouse_event(m_mouse_leftUp, 0, 0, 0, 0);
        } else if (Input.GetKeyUp ("joystick button 1")) {
            mouse_event(m_mouse_leftUp, 0, 0, 0, 0);
        } else if (Input.GetKeyUp ("joystick button 2")) {
            mouse_event(m_mouse_leftUp, 0, 0, 0, 0);
        } else if (Input.GetKeyUp ("joystick button 3")) {
            mouse_event(m_mouse_leftUp, 0, 0, 0, 0);
        } else if (Input.GetKeyUp ("space")) {
            mouse_event(m_mouse_leftUp, 0, 0, 0, 0);
        }
    }
}

動作させるとマウスdownの挙動が少々あやしいので、実用レベルにしたい人はもう少し手を加えたほうがいいと思います。