如何限制Button在3D地图Unity3D中像Cursor一样工作

时间:2017-02-24 14:28:08

标签: c# unity3d kinect

大家好,我正在研究一款基于Kinect Movmement和Csharp语言的足球比赛。

我有一个UI按钮,我把它像画布下的圆圈一样 在3D模型中找到的画布是体育场。

按钮的工作方式类似于光标,它通过脚本进行转换,脚本在向前移动的播放器的右手和作为原始按钮的光标之间进行同步。

好吧所有这些限制工作都很好而且完美,只有一件事,当我从2D的其他场景移动到3D场景时,我发现光标在这个位置移动-12125.34 , -3132.932 , -1, 而在先前的场景中,这个位置是2D -616 , 288 , -1

如果你不理解我只是认为你向右移动你的kinect并且你用同一只手移动一个物体 我为那个限制写的代码就是那个

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HandController : MonoBehaviour {

    public static HandController Instance;

    private Vector2 initialPosition;//body initial position 

    public GameObject cursorObject;
    private bool cursorEnter = false;

    private float delay ;
    private float timeing;
    private float firstContactTime;
    private string[] flagsTable = new string [] {
        "Tunisia", "Maroco","Austria", "Belgium", "cameroon",
        "cote_d_ivoire", "Egypt","france", "guini", "jordan",
        "liberia", "madagascar","mali", "mauritius", "moldova",
        "romanie", "Slovakie","Spain", "United_kingdom", "Vanuatu",
        "polgne"
    };


    private bool checkingFlagsTableDone = false;
    public static bool flagsFound = false;
    public static string countryChoosingName = "";
    public Button btnSelected;
    public string buttonSelected = "";
    public Button myCursor;
    public Image myCursorImage;

    void Awake()
    {
        Instance = this;

    }
    void Start () {

        DontDestroyOnLoad(transform.root.gameObject);
       // var vertExtent = Camera.main.camera.orthographicSize;
        //var horzExtent = vertExtent * Screen.width / Screen.height;

    }
    public int offstX= 937;
    public int offstY= 520;

    bool isInited=false;

    void Update() {
        float initialXposition = transform.localPosition.x;
        float initialYposition = transform.localPosition.y;
        float nextXposition=0;
        float nextYposition=0;

        KinectManager manager = KinectManager.Instance;

        if (DepthImageViewer.Instance.jointColliders != null)
        {
            if (manager.Player1Calibrated && isInited == false)
            {
                isInited = true;
                nextXposition = transform.localPosition.x;
                nextYposition = transform.localPosition.y;
                if (initialXposition != nextXposition && initialYposition != nextYposition)
                {
                   // Debug.Log("I moved the cursor the the initial pisition");
                }
                //else
                   // Debug.Log("Sorry I didn't translate the cursor");
            } else if (manager.Player1Calibrated && isInited == true)
            {
                float XvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.x * 192.0f + 184430.4f + offstX;
                float YvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.y *108.0f + 58832.1f + offstY;


            Vector2 newPos = new Vector2(XvalueAre - offstX, YvalueAre - offstY);
                transform.position = new Vector3(newPos.x,newPos.y,-1);
            }
         }
        else { Debug.Log("Erreur remplir joints colliders "); }
        /////////////////////////////////////////////Delay Time Over Button /////////////////////////////////////////////


        if (cursorEnter == true) {
            delay = Time.time - firstContactTime;
            //Debug.Log("waiting time is => " + delay);
            myCursorImage .fillAmount =  1 - (delay / 3) ;
            if (delay > 3) {

                if (currentBtn != null) { 
                    CheckOnOverButtonName(currentBtn.name);

                    Debug.Log("Button Name => " + currentBtn.name);
                    if (flagsFound == true)
                    {
                        Debug.Log("the country choose is => " + countryChoosingName);
                        currentBtn.onClick.Invoke();
                        myCursorImage.fillAmount = 1;
                        cursorEnter = false;
                        buttonSelected = countryChoosingName; 
                    }
                    Debug.Log("the country choose is => " + currentBtn.name);
                    currentBtn.onClick.Invoke();
                    myCursorImage.fillAmount = 1;
                    cursorEnter=false;
                }
            }
        }

    }

    Button currentBtn;
    void OnTriggerEnter2D(Collider2D coll)
    {
        firstContactTime = Time.time;
        cursorEnter = true;
        currentBtn = coll.GetComponent<Button>();
    }

    void OnTriggerExit2D(Collider2D other)
    {
        cursorEnter = false; 
        flagsFound = false;
        countryChoosingName = "";
        checkingFlagsTableDone = false;
    }


    void CheckOnOverButtonName(string buttonName)
    {
        for(int i = 0 ; i < flagsTable.Length ; i++ ){
            if (string.Compare(flagsTable[i],buttonName) == 0) {
                flagsFound = true;
                countryChoosingName = flagsTable[i];
                if (i == flagsTable.Length) {
                    checkingFlagsTableDone = true;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您好我在使用Mathf.Clamp

后解决了问题
   float XvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.x * 192.0f + 184430.4f + offstX;
                    float YvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.y * 108.0f + 58832.1f  + offstY;

                    float x = Mathf.Clamp(XvalueAre, 0.0f,1024.0f);
                    float y = Mathf.Clamp(YvalueAre, 0.0f,768.0f);
                    transform.position = new Vector3(x, y, -1);