现在很多手游都是通过摇杆操作:
- 比如左手摇杆,右手点击按钮攻击的MMO系列;
- 或者是左右手都有类似摇杆拖动设计的按钮(王者荣耀:左手摇杆右手点击或者拖动技能方向。)
- 比如左手摇杆控制移动,右手开枪,开枪的同时开支持那个手指滑动瞄准(类似吃鸡的操作)。
- 比如左手大拇指控制摇杆移动,左手食指点击吃药,右手大拇指控制攻击,右手食指控制滑动,右手小手指负责扣脚(没有这个游戏)
等等。。。。
具体设计思路:
代码:
代码分两个脚本:
一个负责监听整个场景的所有触摸事件:TouchManager
一个负责单独添加到想要实现各种操作的摇杆按钮(图片)上: joySticButton(是否屏蔽下层事件需要在Inspector上设置)
TouchManager:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TouchManager : MonoBehaviour
{
private static TouchManager instance = null;
private Dictionary<int, List<JoyStickButton>> dicJoyStick;
public static TouchManager getInstant()
{
return instance;
}
// Start is called before the first frame update
void Start()
{
dicJoyStick = new Dictionary<int, List<JoyStickButton>>();
}
private void Awake()
{
if(instance == null)
{
instance = this;
}
else
{
Destroy(this);
}
}
// Update is called once per frame
void Update()
{
for(int i = 0; i < Input.touchCount; ++i)
{
Touch touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began)
{
this.checkAndRegist(touch.fingerId, touch.position);
this.callTouchBegan(touch.fingerId, touch.position);
}
else if (touch.phase == TouchPhase.Moved)
{
this.callTouchMove(touch.fingerId, touch.position);
}
else if (touch.phase == TouchPhase.Ended)
{
this.callTouchEnd(touch.fingerId, touch.position);
this.removeRegist(touch.fingerId);
}
}
}
private void callTouchBegan(int fingerId, Vector2 position)
{
if(this.dicJoyStick.ContainsKey(fingerId))
{
foreach(JoyStickButton jsb in this.dicJoyStick[fingerId])
{
jsb.onTouchBegan(position);
}
}
}
private void callTouchMove(int fingerId, Vector2 position)
{
if (this.dicJoyStick.ContainsKey(fingerId))
{
foreach (JoyStickButton jsb in this.dicJoyStick[fingerId])
{
jsb.onTouchMove(position);
}
}
}
private void callTouchEnd(int fingerId, Vector2 position)
{
if (this.dicJoyStick.ContainsKey(fingerId))
{
foreach (JoyStickButton jsb in this.dicJoyStick[fingerId])
{
jsb.onTouchEnd(position);
}
}
}
private void checkAndRegist(int fingerId, Vector2 position)
{
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = position;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
for (int i = 0; i < results.Count; ++i)
{
JoyStickButton jsb = results[i].gameObject.GetComponent<JoyStickButton>();
if (jsb && !jsb.isActive())
{
jsb.setActive(true);
if (!dicJoyStick.ContainsKey(fingerId))
{
dicJoyStick.Add(fingerId, new List<JoyStickButton>());
}
dicJoyStick[fingerId].Add(jsb);
if (jsb.swallowTouch)
{
break;
}
}
}
}
private void removeRegist(int fingerId)
{
if (dicJoyStick.ContainsKey(fingerId))
{
foreach(JoyStickButton jsb in dicJoyStick[fingerId])
{
jsb.setActive(false);
}
dicJoyStick[fingerId].Clear();
dicJoyStick.Remove(fingerId);
}
}
}
joySticButton:
using UnityEngine;
public class JoyStickButton : MonoBehaviour
{
private bool active = false;
public bool swallowTouch = true;
// Start is called before the first frame update
void Start()
{
}
void OnDestroy()
{
}
// Update is called once per frame
void Update()
{
}
public void onTouchBegan(Vector2 position)
{
Debug.Log("=========onTouchBegan=========: " + gameObject.name);
}
public void onTouchMove(Vector2 position)
{
Debug.Log("=========onTouchMove=========: " + gameObject.name);
}
public void onTouchEnd(Vector2 position)
{
Debug.Log("=========onTouchEnd=========: " + gameObject.name);
}
public void setActive(bool state)
{
this.active = state;
}
public bool isActive()
{
return active;
}
}
才写的。。还热乎呢。。。还没来得及细细整理下,想发个文章就吃饭去了。。。
所以。。。。。。。
去吃饭了,就到这吧。。。
把joySticButton脚本的三个onTouchXXX的void前面加上virtual
用于不同的按钮或者摇杆,新建一个脚本, 然后继承joySticButton,override三个onTouch就好