纳金网
标题:
保存物体的位置信息及布尔状态的小扩展
[打印本页]
作者:
会飞的鱼
时间:
2012-4-1 11:21
标题:
保存物体的位置信息及布尔状态的小扩展
有用的小代码,可以保存物体的位置信息及布尔状态!
Hi all, i present you my first plugin for Unity3d. It serves for save position (Vector3), rotation (Quaternion) and booleans.
I hope it like you!
Script name : APlayerPrefs (C# Script)
using UnityEngine;
using System.Collections;
public class APlayerPrefs : MonoBehaviour
{
public static void SaveObjectPosition(string saveName,Vector3 value)
{
PlayerPrefs.SetFloat(saveName + "px",value.x);
PlayerPrefs.SetFloat(saveName + "py",value.y);
PlayerPrefs.SetFloat(saveName + "pz",value.z);
}
public static void SaveObjectRotation(string saveName,Quaternion value)
{
PlayerPrefs.SetFloat(saveName + "rx",value.x);
PlayerPrefs.SetFloat(saveName + "ry",value.y);
PlayerPrefs.SetFloat(saveName + "rz",value.z);
PlayerPrefs.SetFloat(saveName + "rw",value.w);
}
public static Vector3 LoadObjectPosition(string loadPosition)
{
float pX = PlayerPrefs.GetFloat(loadPosition + "px");
float pY = PlayerPrefs.GetFloat(loadPosition + "py");
float pZ = PlayerPrefs.GetFloat(loadPosition + "pz");
return new Vector3(pX,pY,pZ);
}
public static Quaternion LoadObjectRotation(string loadRotation)
{
float rX = PlayerPrefs.GetFloat(loadRotation + "rx");
float rY = PlayerPrefs.GetFloat(loadRotation + "ry");
float rZ = PlayerPrefs.GetFloat(loadRotation + "rz");
float rW = PlayerPrefs.GetFloat(loadRotation + "rw");
return new Quaternion(rX,rY,rZ,rW);
}
public static void SetBool(string saveBool,bool valueBool)
{
PlayerPrefs.SetInt(saveBool,valueBool?1:0);
}
public static bool GetBool(string loadBool)
{
return PlayerPrefs.GetInt(loadBool)==1?true:false;
}
}
======Example Script (JavaScript)
var example: boolean = true;
function Update ()
{
if(Input.GetKeyDown(KeyCode.A))
{
APlayerPrefs.SetBool("newbool",example);
}
if(Input.GetKeyDown(KeyCode.B))
{
print(APlayerPrefs.GetBool("newbool"));
}
if(Input.GetKeyDown(KeyCode.S))
{
APlayerPrefs.SaveObjectPosition("playerposition",transform.position);
APlayerPrefs.SaveObjectRotation("playerrotation",transform.rotation);
}
if(Input.GetKeyDown(KeyCode.L))
{
transform.position = APlayerPrefs.LoadObjectPosition("playerposition");
transform.rotation = APlayerPrefs.LoadObjectRotation("playerrotation");
}
}
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5