拖到想运行时移动的模型上,运行后 按一下 “开关控制的按钮”比如默认是F1,你可以给不同的模型设置不同的启动按键,按一下就开启控制,再按一下就关闭控制。步长和按键设置需要在打包之前就设置好,修改完位置后 记得按一下用于保存的按钮。
WechatIMG4382.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class RunTimeMoveSave : MonoBehaviour
{
[Header("保存名,需唯一,空则为本身")]
public string m_SaveName = "";
[Header("开关控制按钮")]
public KeyCode m_OpenCloseControlKey = KeyCode.F1;
[Header("要控制的物体,空则为本身")]
public Transform m_ControlObj;
[Header("控制移动的步长")]
public float m_MoveStep = 0.01f;
[Header("控制移动的按键")]
public KeyCode m_Move_x_Up = KeyCode.UpArrow;
public KeyCode m_Move_x_Down = KeyCode.DownArrow;
public KeyCode m_Move_y_Up = KeyCode.LeftArrow;
public KeyCode m_Move_y_Down = KeyCode.RightArrow;
public KeyCode m_Move_z_Up = KeyCode.N;
public KeyCode m_Move_z_Down = KeyCode.M;
[Header("控制旋转的步长")]
public float m_RotateStep = 1f;
[Header("控制旋转的按键")]
public KeyCode m_Rotate_x_Up = KeyCode.W;
public KeyCode m_Rotate_x_Down = KeyCode.S;
public KeyCode m_Rotate_y_Up = KeyCode.A;
public KeyCode m_Rotate_y_Down = KeyCode.D;
public KeyCode m_Rotate_z_Up = KeyCode.Q;
public KeyCode m_Rotate_z_Down = KeyCode.E;
[Header("控制缩放的步长")]
public float m_ScaleStep = 0.01f;
[Header("控制缩放的按键")]
public KeyCode m_Scale_Up = KeyCode.O;
public KeyCode m_Scale_Down = KeyCode.P;
[Header("用于保存的按键")]
public KeyCode m_SaveKey = KeyCode.L;
[Header("用于清除的按键,清除后请退出程序重开,会加载默认角度位置")]
public KeyCode m_ClearKey = KeyCode.C;
// Use this for initialization
void Start()
{
if (m_ControlObj == null)
m_ControlObj = this.transform;
if (m_SaveName == "")
m_SaveName = this.name;
Read();
}
void Read()
{
if (PlayerPrefs.GetInt("IsSet_" + m_SaveName) == 0)
{
//首次加载保存即可
Save();
return;
}
m_ControlObj.localPosition = new Vector3(PlayerPrefs.GetFloat("PosX_" + m_SaveName), PlayerPrefs.GetFloat("PosY_" + m_SaveName), PlayerPrefs.GetFloat("PosZ_" + m_SaveName));
m_ControlObj.localEulerAngles = new Vector3(PlayerPrefs.GetFloat("RotX_" + m_SaveName), PlayerPrefs.GetFloat("RotY_" + m_SaveName), PlayerPrefs.GetFloat("RotZ_" + m_SaveName));
m_ControlObj.localScale = new Vector3(PlayerPrefs.GetFloat("Scal_" + m_SaveName), PlayerPrefs.GetFloat("Scal_" + m_SaveName), PlayerPrefs.GetFloat("Scal_" + m_SaveName));
}
void Save()
{
PlayerPrefs.SetInt("IsSet_" + m_SaveName, 1);
PlayerPrefs.SetFloat("PosX_" + m_SaveName, m_ControlObj.localPosition.x);
PlayerPrefs.SetFloat("PosY_" + m_SaveName, m_ControlObj.localPosition.y);
PlayerPrefs.SetFloat("PosZ_" + m_SaveName, m_ControlObj.localPosition.z);
PlayerPrefs.SetFloat("RotX_" + m_SaveName, m_ControlObj.localEulerAngles.x);
PlayerPrefs.SetFloat("RotY_" + m_SaveName, m_ControlObj.localEulerAngles.y);
PlayerPrefs.SetFloat("RotZ_" + m_SaveName, m_ControlObj.localEulerAngles.z);
PlayerPrefs.SetFloat("Scal_" + m_SaveName, m_ControlObj.localScale.y);
}
void Clear()
{
PlayerPrefs.SetInt("IsSet_" + m_SaveName, 0);
}
bool isSetting = false;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(m_OpenCloseControlKey))
{
isSetting = !isSetting;
}
if (Input.GetKeyDown(m_SaveKey))
Save();
if (Input.GetKeyDown(m_ClearKey))
Clear();
if (!isSetting)
return;
if (Input.GetKeyDown(m_Move_x_Up))
{
m_ControlObj.localPosition += new Vector3(1, 0, 0) * m_MoveStep;
}
if (Input.GetKeyDown(m_Move_x_Down))
{
m_ControlObj.localPosition -= new Vector3(1, 0, 0) * m_MoveStep;
}
if (Input.GetKeyDown(m_Move_y_Up))
{
m_ControlObj.localPosition += new Vector3(0, 1, 0) * m_MoveStep;
}
if (Input.GetKeyDown(m_Move_y_Down))
{
m_ControlObj.localPosition -= new Vector3(0, 1, 0) * m_MoveStep;
}
if (Input.GetKeyDown(m_Move_z_Up))
{
m_ControlObj.localPosition += new Vector3(0, 0, 1) * m_MoveStep;
}
if (Input.GetKeyDown(m_Move_z_Down))
{
m_ControlObj.localPosition -= new Vector3(0, 0, 1) * m_MoveStep;
}
if (Input.GetKeyDown(m_Rotate_x_Up))
{
m_ControlObj.localEulerAngles += new Vector3(1, 0, 0) * m_RotateStep;
}
if (Input.GetKeyDown(m_Rotate_x_Down))
{
m_ControlObj.localEulerAngles -= new Vector3(1, 0, 0) * m_RotateStep;
}
if (Input.GetKeyDown(m_Rotate_y_Up))
{
m_ControlObj.localEulerAngles += new Vector3(0, 1, 0) * m_RotateStep;
}
if (Input.GetKeyDown(m_Rotate_y_Down))
{
m_ControlObj.localEulerAngles -= new Vector3(0, 1, 0) * m_RotateStep;
}
if (Input.GetKeyDown(m_Rotate_z_Up))
{
m_ControlObj.localEulerAngles += new Vector3(0, 0, 1) * m_RotateStep;
}
if (Input.GetKeyDown(m_Rotate_z_Down))
{
m_ControlObj.localEulerAngles -= new Vector3(0, 0, 1) * m_RotateStep;
}
if (Input.GetKeyDown(m_Scale_Up))
{
m_ControlObj.localScale += Vector3.one * m_ScaleStep;
}
if (Input.GetKeyDown(m_Scale_Down))
{
m_ControlObj.localScale -= Vector3.one * m_ScaleStep;
}
}
}
网友评论