Unity: ExampleGameInput.cs

from https://gist.github.com/talecrafter/35fb1441fd3ba0ff7ae9


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using CraftingLegends.Framework;
using CraftingLegends.Core;
using System;
using CraftingLegends.ExtendedFramework;

/// <summary>
/// short example class showing how you can adapt to user switching control scheme
/// won't compile
/// </summary>
public class ExampleGameInput : MonoBehaviour
{
	public enum InputScheme : int
	{
		None,
		Mouse,
		Gamepad,
		Touch
	}

	// this is the most important part: other components like GUI should listen to this and then adapt when changed
	public event Action<InputScheme> inputSchemeChanged;

	[SerializeField]
	private InputScheme _inputScheme = InputScheme.None;
	public InputScheme inputScheme // public so other components can look at this when initializing
	{
		get
		{
			return _inputScheme;
		}
	}

	private bool anyMouseButtonDown
	{
		get
		{
			return Input.GetMouseButtonDown(0)
				|| Input.GetMouseButtonDown(1)
				|| Input.GetMouseButtonDown(2);
        }
	}

	private Vector3? _mousePosStart = null;
	private bool mouseHasMoved
	{
		get
		{
			if (_mousePosStart == null)
				return false;

			return _mousePosStart != Input.mousePosition;
		}
	}

	// ================================================================================
	//  unity methods
	// --------------------------------------------------------------------------------

	void Awake()
	{
		if (Input.GetJoystickNames().Length > 0)
		{
			SetInputScheme(InputScheme.Gamepad);
		}
		else if (MainBase.Info.hasTouch) // game specific code, looks for current platform
		{
			SetInputScheme(InputScheme.Touch);
		}
		else
		{
			SetInputScheme(InputScheme.Mouse);
		}
	}

	void Update()
	{
		CheckInputScheme();
	}

	// ================================================================================
	//  private methods
	// --------------------------------------------------------------------------------

	/// <summary>
	/// listen for input entries which indicate a specific control scheme
	/// </summary>
	private void CheckInputScheme()
	{
		if (Input.touchCount > 0)
		{
			SetInputScheme(InputScheme.Touch);
		}
		else if (mouseHasMoved || anyMouseButtonDown)
		{
			SetInputScheme(InputScheme.Mouse);
		}
		// this is very specific code, this game uses a wrapper around the InControl plugin
		else if (MainBase.Instance.gamepadInput != null
		         && (MainBase.Instance.gamepadInput.anyButton || MainBase.Instance.gamepadInput.hasMovement
				 || MainBase.Instance.gamepadInput.hasLookDirection))
		{
			SetInputScheme(InputScheme.Gamepad);
		}
	}

	private void SetInputScheme(InputScheme newScheme)
	{
		if (newScheme == _inputScheme)
			return;

		_inputScheme = newScheme;

		// save mouse position so we can check if the mouse is moved
		if (newScheme == InputScheme.Mouse)
		{
			_mousePosStart = null;
		}
		else
		{
			_mousePosStart = Input.mousePosition;
		}

		OnInputSchemeChanged();
	}

	private void OnInputSchemeChanged()
	{
		if (inputSchemeChanged != null)
		{
			inputSchemeChanged(_inputScheme);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值