生活随笔
收集整理的這篇文章主要介紹了
无聊写个手势插件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
GestureCool
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;public class GestureCool : SingleFramework<GestureCool> {//將一個(gè)圓等分稱16或者8份,從X軸開(kāi)始標(biāo)記,一次為0,1,2,3,4,5,6....15Dictionary<string, List<string>> DicGestures = new Dictionary<string, List<string>>(); //手勢(shì)庫(kù)string INI_File_Path; //手勢(shì)庫(kù)配置文件信息//當(dāng)前操作手勢(shì)名稱,可能用于采集識(shí)別時(shí)候用到string nowCollectGestureKeyName;public override void Init(){INI_File_Path = Application.dataPath + "/MyFrame/MyLibs/GestureRecognition/GESTURECOOLINI.ini";}/// <summary>/// 讀取配置文件中的手勢(shì)庫(kù)信息,并綁定到字典庫(kù)中~/// </summary>bool LoadGesturesFromINI(){try{string strLoadFromFile = File.ReadAllText(INI_File_Path);DicGestures = LitJson.JsonMapper.ToObject<Dictionary<string, List<string>>>(strLoadFromFile);return true;}catch{return false;} }/// <summary>/// 存儲(chǔ)字典庫(kù)中的手勢(shì)信息到字典/// </summary>public bool SaveGesturesToINI(){try{string json = LitJson.JsonMapper.ToJson(DicGestures);File.WriteAllText(INI_File_Path, json, Encoding.Default);return true;}catch{return false;}}/// <summary>/// 開(kāi)始采集手勢(shì)/// </summary>/// <param name="GestureName">采集手勢(shì)的名稱</param>public void StartCollectionWithKeyName(string GestureName){nowCollectGestureKeyName = GestureName;//已經(jīng)包含這個(gè)手勢(shì)就追加if (!DicGestures.ContainsKey(GestureName)){DicGestures.Add(GestureName, new List<string>());}}/// <summary>/// 存儲(chǔ)新的識(shí)別碼/// </summary>/// <param name="RecString"></param>/// <returns></returns>public bool SaveRecoCode(string RecCode){if (nowCollectGestureKeyName == "" || CheckIsRepeat(RecCode)){ return false;}//不重復(fù)就插入當(dāng)前新項(xiàng)DicGestures[nowCollectGestureKeyName].Add(RecCode); return true;}/// <summary>/// 檢測(cè)是否存在重復(fù)項(xiàng)/// </summary>/// <param name="RecognitionString"></param>/// <returns></returns>bool CheckIsRepeat(string RecCode){for (int i = 0; i < DicGestures[nowCollectGestureKeyName].Count; i++){if (RecCode == DicGestures[nowCollectGestureKeyName][i]){return true;}}return false;}/// <summary>/// 根據(jù)手勢(shì)名稱返回識(shí)別碼/// </summary>/// <param name="RecoName">手勢(shì)名稱</param>/// <returns></returns>public List<string> GetRecosStringByName(string RecoName){return DicGestures[RecoName];}
}
DrawGesture
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;
using System.IO;public class DrawGesture : SingleFramework<DrawGesture>, IPointerDownHandler, IPointerUpHandler
{//將一個(gè)圓等分稱16或者8份,從正上方開(kāi)始標(biāo)記,一次為手勢(shì)0,1,2,3,4,5,6....15Dictionary<string, List<string>> DicGestures = new Dictionary<string, List<string>>();//繪制手勢(shì)LineRenderer lineRenderer;//是否在繪制中bool isDrawing;public void OnPointerDown(PointerEventData eventData){isDrawing = true;drawCounter = 0;GetPointUpdateCounter = 0;}public void OnPointerUp(PointerEventData eventData){isDrawing = false;lineRenderer.SetVertexCount(0);lstDrawPos3.Clear();//獲取到識(shí)別碼string RecoCode = GetRecoCodeByDirectionLst(lstGestures);//交給庫(kù)去處理是否是采集if (GestureCool.instance.SaveRecoCode(RecoCode)){Debug.Log("追加手勢(shì)成功~");}else {Debug.Log("追加手勢(shì)失敗~");}lstGestures.Clear();}public override void Init(){lineRenderer = this.GetComponent<LineRenderer>();}int drawCounter; //繪點(diǎn)個(gè)數(shù)int GetPointFrequency = 2; //取點(diǎn)頻率int GetPointUpdateCounter; //Update計(jì)數(shù)器public int RecognitionLevel = 2; //識(shí)別的級(jí)別,出現(xiàn)的方向出現(xiàn)RecognitionLevel次以上才確定Vector3 DrawPos; //繪制點(diǎn)坐標(biāo)Vector3 Direction; //拖拽的方向List<Vector3> lstDrawPos3 = new List<Vector3>(); //所有點(diǎn)集合List<int> lstGestures = new List<int>(); //所有方向碼void Update(){if (isDrawing){//繪畫中,取點(diǎn)if (GetPointUpdateCounter++ % GetPointFrequency == 0) //滿足取點(diǎn)頻率開(kāi)始取點(diǎn){drawCounter++; //繪制點(diǎn)的個(gè)數(shù)lineRenderer.SetVertexCount(drawCounter);DrawPos = new Vector3(Input.mousePosition.x - Screen.width / 2, Input.mousePosition.y - Screen.height / 2, Input.mousePosition.z);lineRenderer.SetPosition(drawCounter - 1, DrawPos);lstDrawPos3.Add(DrawPos);//打印出方向if (drawCounter > 1){Direction = (DrawPos - lstDrawPos3[drawCounter - 2]).normalized;if (Direction != Vector3.zero){lstGestures.Add(CalculateGesture8art(GetAngelByTwoVector3(Direction)));}}}}}/// <summary>/// 去除手勢(shì)中的無(wú)效項(xiàng)/// </summary>/// <param name="OriLst"></param>/// <returns></returns>List<int> RemoveInvalid(List<int> OriLst){List<int> newLst = new List<int>();int repeatCounter = 1;for (int i = 0; i < OriLst.Count; i++){if (i != 0){//等于前一項(xiàng)if (OriLst[i] == OriLst[i - 1]){repeatCounter++;if (repeatCounter == RecognitionLevel){newLst.Add(OriLst[i]);}}else //不等于前一項(xiàng){repeatCounter = 1;}}}return newLst;}//根據(jù)角度計(jì)算出對(duì)應(yīng)的部分單一手勢(shì)(16區(qū)域手勢(shì))int CalculateGesture16Part(float Angel){return (int)(((Angel + 11.25f) % 360) / 22.5f);}//(默認(rèn))根據(jù)角度計(jì)算出對(duì)應(yīng)的部分單一手勢(shì)(8區(qū)域手勢(shì))int CalculateGesture8art(float Angel){return (int)(((Angel + 22.5f) % 360) / 45f);}//返回目標(biāo)向量_與X軸正方向 的夾角float GetAngelByTwoVector3(Vector3 vec3_target){float angelRes = 0;//先判斷方向是否是坐標(biāo)軸朝向if (vec3_target.x == 0 && vec3_target.y > 0) //Y軸正向{angelRes = 90;}else if (vec3_target.x == 0 && vec3_target.y < 0) //Y軸反向{angelRes = 270;}else if (vec3_target.y == 0 && vec3_target.x > 0) //X軸正向{angelRes = 0;}else if (vec3_target.y == 0 && vec3_target.x < 0) //X軸反向{angelRes = 180;}else if (vec3_target.x > 0 && vec3_target.y > 0) //第一象限{angelRes = (Mathf.Atan(Mathf.Abs(vec3_target.y) / Mathf.Abs(vec3_target.x)) * Mathf.Rad2Deg);}else if (vec3_target.x < 0 && vec3_target.y > 0) //第二象限{angelRes = 180 - (Mathf.Atan(Mathf.Abs(vec3_target.y) / Mathf.Abs(vec3_target.x)) * Mathf.Rad2Deg);}else if (vec3_target.x < 0 && vec3_target.y < 0) //第三象限{angelRes = 180 + (Mathf.Atan(Mathf.Abs(vec3_target.y) / Mathf.Abs(vec3_target.x)) * Mathf.Rad2Deg);}else if (vec3_target.x > 0 && vec3_target.y < 0) //第四象限{angelRes = 360 - (Mathf.Atan(Mathf.Abs(vec3_target.y) / Mathf.Abs(vec3_target.x)) * Mathf.Rad2Deg);}return angelRes;}/// <summary>/// 根據(jù)方向集合返回手勢(shì)識(shí)別碼/// </summary>/// <param name="lstDirections"></param>/// <returns></returns>public string GetRecoCodeByDirectionLst(List<int> lstDirections){string RecoCode = ""; for (int i = 0; i < lstDirections.Count; i++){RecoCode += lstDirections[i].ToString() + "_";}RecoCode = RecoCode.TrimEnd('_');return RecoCode;}
}
總結(jié)
以上是生活随笔為你收集整理的无聊写个手势插件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。