심심해서 제스처 플러그인 쓰기

8685 단어 Unity3D
GestureCool
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class GestureCool : SingleFramework {

    //       16  8 , X     ,   0,1,2,3,4,5,6....15
    Dictionary> DicGestures = new Dictionary>();    //   

    string INI_File_Path;   //         

    //        ,            
    string nowCollectGestureKeyName;

    public override void Init()
    {
        INI_File_Path = Application.dataPath + "/MyFrame/MyLibs/GestureRecognition/GESTURECOOLINI.ini";
    }

    /// 
    ///              ,        ~
    /// 
    bool LoadGesturesFromINI()
    {
        try
        {
            string strLoadFromFile = File.ReadAllText(INI_File_Path);
            DicGestures = LitJson.JsonMapper.ToObject>>(strLoadFromFile);
            return true;
        }
        catch
        {
            return false;
        }     
    }

    /// 
    ///               
    /// 
    public bool SaveGesturesToINI()
    {
        try
        {
            string json = LitJson.JsonMapper.ToJson(DicGestures);
            File.WriteAllText(INI_File_Path, json, Encoding.Default);
            return true;
        }
        catch
        {
            return false;
        }
        
    }

    /// 
    ///       
    /// 
    ///        
    public void StartCollectionWithKeyName(string GestureName)
    {
        nowCollectGestureKeyName = GestureName;
        //           
        if (!DicGestures.ContainsKey(GestureName))
        {
            DicGestures.Add(GestureName, new List());
        }
    }

    /// 
    ///        
    /// 
    /// 
    /// 
    public bool SaveRecoCode(string RecCode)
    {
        if (nowCollectGestureKeyName == "" || CheckIsRepeat(RecCode))
        {           
            return false;
        }
        //          
        DicGestures[nowCollectGestureKeyName].Add(RecCode);     
        return true;
    }

    /// 
    ///          
    /// 
    /// 
    /// 
    bool CheckIsRepeat(string RecCode)
    {
        for (int i = 0; i < DicGestures[nowCollectGestureKeyName].Count; i++)
        {
            if (RecCode == DicGestures[nowCollectGestureKeyName][i])
            {
                return true;
            }
        }
        return false;
    }

    /// 
    ///            
    /// 
    ///     
    /// 
    public List 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, IPointerDownHandler, IPointerUpHandler
{
    //       16  8 ,        ,     0,1,2,3,4,5,6....15
    Dictionary> DicGestures = new Dictionary>();

    //    
    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();
    }

    int drawCounter;   //    

    int GetPointFrequency = 2;  //    
    int GetPointUpdateCounter;  //Update   

    public int RecognitionLevel = 2;   //     ,       RecognitionLevel      

    Vector3 DrawPos;         //     
    Vector3 Direction;       //     

    List lstDrawPos3 = new List();  //     
    List lstGestures = new List();          //     

    void Update()
    {
        if (isDrawing)
        {
            //   ,  
            if (GetPointUpdateCounter++ % GetPointFrequency == 0)   //          
            {
                drawCounter++;      //      
                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)));
                    }
                }
            }
        }
    }

    /// 
    ///          
    /// 
    /// 
    /// 
    List RemoveInvalid(List OriLst)
    {
        List newLst = new List();
        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;
    }

    //                (16    )
    int CalculateGesture16Part(float Angel)
    {
        return (int)(((Angel + 11.25f) % 360) / 22.5f);
    }

    //(  )                (8    )
    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;
    }

    /// 
    ///              
    /// 
    /// 
    /// 
    public string GetRecoCodeByDirectionLst(List lstDirections)
    {
        string RecoCode = "";  
        for (int i = 0; i < lstDirections.Count; i++)
        {
            RecoCode += lstDirections[i].ToString() + "_";
        }
        RecoCode = RecoCode.TrimEnd('_');
        return RecoCode;
    }
}

좋은 웹페이지 즐겨찾기