生活随笔
收集整理的這篇文章主要介紹了
无聊写个手势插件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
GestureCool
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;public class GestureCool : SingleFramework<GestureCool> {//將一個圓等分稱16或者8份,從X軸開始標記,一次為0,1,2,3,4,5,6....15Dictionary<string, List<string>> DicGestures = new Dictionary<string, List<string>>(); //手勢庫string INI_File_Path; //手勢庫配置文件信息//當前操作手勢名稱,可能用于采集識別時候用到string nowCollectGestureKeyName;public override void Init(){INI_File_Path = Application.dataPath + "/MyFrame/MyLibs/GestureRecognition/GESTURECOOLINI.ini";}/// <summary>/// 讀取配置文件中的手勢庫信息,并綁定到字典庫中~/// </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>/// 存儲字典庫中的手勢信息到字典/// </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>/// 開始采集手勢/// </summary>/// <param name="GestureName">采集手勢的名稱</param>public void StartCollectionWithKeyName(string GestureName){nowCollectGestureKeyName = GestureName;//已經(jīng)包含這個手勢就追加if (!DicGestures.ContainsKey(GestureName)){DicGestures.Add(GestureName, new List<string>());}}/// <summary>/// 存儲新的識別碼/// </summary>/// <param name="RecString"></param>/// <returns></returns>public bool SaveRecoCode(string RecCode){if (nowCollectGestureKeyName == "" || CheckIsRepeat(RecCode)){ return false;}//不重復就插入當前新項DicGestures[nowCollectGestureKeyName].Add(RecCode); return true;}/// <summary>/// 檢測是否存在重復項/// </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ù)手勢名稱返回識別碼/// </summary>/// <param name="RecoName">手勢名稱</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
{//將一個圓等分稱16或者8份,從正上方開始標記,一次為手勢0,1,2,3,4,5,6....15Dictionary<string, List<string>> DicGestures = new Dictionary<string, List<string>>();//繪制手勢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();//獲取到識別碼string RecoCode = GetRecoCodeByDirectionLst(lstGestures);//交給庫去處理是否是采集if (GestureCool.instance.SaveRecoCode(RecoCode)){Debug.Log("追加手勢成功~");}else {Debug.Log("追加手勢失敗~");}lstGestures.Clear();}public override void Init(){lineRenderer = this.GetComponent<LineRenderer>();}int drawCounter; //繪點個數(shù)int GetPointFrequency = 2; //取點頻率int GetPointUpdateCounter; //Update計數(shù)器public int RecognitionLevel = 2; //識別的級別,出現(xiàn)的方向出現(xiàn)RecognitionLevel次以上才確定Vector3 DrawPos; //繪制點坐標Vector3 Direction; //拖拽的方向List<Vector3> lstDrawPos3 = new List<Vector3>(); //所有點集合List<int> lstGestures = new List<int>(); //所有方向碼void Update(){if (isDrawing){//繪畫中,取點if (GetPointUpdateCounter++ % GetPointFrequency == 0) //滿足取點頻率開始取點{drawCounter++; //繪制點的個數(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>/// 去除手勢中的無效項/// </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){//等于前一項if (OriLst[i] == OriLst[i - 1]){repeatCounter++;if (repeatCounter == RecognitionLevel){newLst.Add(OriLst[i]);}}else //不等于前一項{repeatCounter = 1;}}}return newLst;}//根據(jù)角度計算出對應的部分單一手勢(16區(qū)域手勢)int CalculateGesture16Part(float Angel){return (int)(((Angel + 11.25f) % 360) / 22.5f);}//(默認)根據(jù)角度計算出對應的部分單一手勢(8區(qū)域手勢)int CalculateGesture8art(float Angel){return (int)(((Angel + 22.5f) % 360) / 45f);}//返回目標向量_與X軸正方向 的夾角float GetAngelByTwoVector3(Vector3 vec3_target){float angelRes = 0;//先判斷方向是否是坐標軸朝向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ù)方向集合返回手勢識別碼/// </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é)
以上是生活随笔為你收集整理的无聊写个手势插件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。