Unity_现场对位置

发布于 2019-11-21  17 次阅读


前言

如果做投影项目,一定会遇到跟现场位置对不上且投影机也动不了的问题。

正文

思路:把需要更改的物体,做好xml数据,然后Esc调试。

ChangePosTools.cs

/**
 *Copyright(C) 2018 by DefaultCompany
 *All rights reserved.
 *FileName:     ChangePosTools.cs
 *Author:       Pan
 *Version:      1.0
 *UnityVersion:2017.2.0f3
 *Date:         2019-05-16
 *Description:   
 *History:
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChangePosTools : SingletonMonoBehaviour<ChangePosTools>
{ 
    [Header("每帧间隔时间(秒)")]
    [Range(0.01f, 1)]
    public float offset = 0.1f;
    public GameObject go_DebugBg; //调试界面的背景

    //需要调试的物体,初始化的时候记得赋值。
    [HideInInspector]
    public List<GameObject> listGos_Temp = new List<GameObject>(); 

    [HideInInspector]
    public bool isActive = false; //是否开启Esc
    private bool isModif = false; //调试 
    private GameObject go_Temp; //当前选中的物体  
    private int currentNum = -1;//当前的Ids
    void Start ()
    {  
        TransformHelper.SetActive(go_DebugBg, isActive);
    }
    bool Exists(GameObject go)
    {
        return go.GetComponent<RectTransform>();
    } 
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        { 
            isActive = !isActive;
            if (isActive)
            {//打开Esc
             //true
                Debug.Log("开始调试"); 
                //进入调试模式 
                GetComponent<MainControl>().isDebug = true;
                GetComponent<MainControl>().OnEnterDebug();

                TransformHelper.SetActive(listGos_Temp, !isActive);
            }
            else
            {//关闭Esc
                GetComponent<MainControl>().isDebug = false; 
                currentNum = -1;
                Debug.Log("开始保存"); 
                //更新Xml数据
                //GetComponent<MainControl>().OnUpdateXml();
                //恢复内容
                //GetComponent<MainControl>().OnExitDebug(); 

                TransformHelper.SetActive(go_Temp, false);
            } 
            TransformHelper.SetActive(go_DebugBg, isActive); 
            isModif = !isModif; //
        }
        if (isActive)
        {
            if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                //点击向下
                 currentNum++;
                if (currentNum >= listGos_Temp.Count)
                {
                    currentNum = 0;
                }
                TransformHelper.SetActive(listGos_Temp, currentNum, true);
                go_Temp = listGos_Temp[currentNum];

            }
            else if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                //点击向下
                currentNum--;
                if (currentNum < 0)
                {
                    currentNum = listGos_Temp.Count -1;
                }
                TransformHelper.SetActive(listGos_Temp, currentNum, true);
                go_Temp = listGos_Temp[currentNum];

            }  
            //  go_Temp 
            //开始调位置
            if (isModif) //不需判断是不是Transform 还是Rectransform 每次都是0.1
            {
                if (go_Temp == null)
                {
                    return;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().anchoredPosition = new Vector2
                            (go_Temp.GetComponent<RectTransform>().anchoredPosition.x - offset *3.0f,
                            go_Temp.GetComponent<RectTransform>().anchoredPosition.y );
                    }
                    else
                    {
                        go_Temp.transform.position = new Vector3(go_Temp.transform.position.x - offset * 3.0f,
                            go_Temp.transform.position.y, go_Temp.transform.position.z);
                    }
                }
                if (Input.GetKey(KeyCode.D))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().anchoredPosition = new Vector2
                            (go_Temp.GetComponent<RectTransform>().anchoredPosition.x + offset * 3.0f,
                            go_Temp.GetComponent<RectTransform>().anchoredPosition.y );
                    }
                    else
                    {
                        go_Temp.transform.position = new Vector3(go_Temp.transform.position.x + offset * 3.0f,
                            go_Temp.transform.position.y, go_Temp.transform.position.z);
                    }
                }
                if (Input.GetKey(KeyCode.S))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().anchoredPosition = new Vector2
                            (go_Temp.GetComponent<RectTransform>().anchoredPosition.x,
                            go_Temp.GetComponent<RectTransform>().anchoredPosition.y - offset * 3.0f);
                    }
                    else
                    {
                        go_Temp.transform.position = new Vector3(go_Temp.transform.position.x,
                            go_Temp.transform.position.y - offset * 3.0f, go_Temp.transform.position.z);
                    }
                }
                if (Input.GetKey(KeyCode.W))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().anchoredPosition = new Vector2
                            (go_Temp.GetComponent<RectTransform>().anchoredPosition.x,
                            go_Temp.GetComponent<RectTransform>().anchoredPosition.y + offset * 3.0f);
                    }
                    else
                    {
                        go_Temp.transform.position = new Vector3(go_Temp.transform.position.x,
                            go_Temp.transform.position.y + offset * 3.0f, go_Temp.transform.position.z);
                    }
                } 

                if (Input.GetKey(KeyCode.F))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().localScale = new Vector2
                            (go_Temp.GetComponent<RectTransform>().localScale.x - offset*0.02f,
                            go_Temp.GetComponent<RectTransform>().localScale.y);
                    }
                }
                if (Input.GetKey(KeyCode.H))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().localScale = new Vector2
                            (go_Temp.GetComponent<RectTransform>().localScale.x + offset * 0.02f,
                            go_Temp.GetComponent<RectTransform>().localScale.y);
                    } 
                }
                if (Input.GetKey(KeyCode.T))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().localScale = new Vector2
                            (go_Temp.GetComponent<RectTransform>().localScale.x,
                            go_Temp.GetComponent<RectTransform>().localScale.y + offset * 0.02f);
                    }
                }
                if (Input.GetKey(KeyCode.G))
                {
                    if (Exists(go_Temp))
                    {
                        go_Temp.GetComponent<RectTransform>().localScale = new Vector2
                            (go_Temp.GetComponent<RectTransform>().localScale.x,
                            go_Temp.GetComponent<RectTransform>().localScale.y - offset * 0.02f);
                    }
                }
            }
        }
    } 
}

不积跬步,无以至千里;不积小流,无以成江海。