PosStateRecord.cs
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
using System.Text;
using System.IO;
using System;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.Generic;
[AddComponentMenu("状态记录器")]
public class PosStateRecord : MonoBehaviour
{
public string rcdName;
[System.Serializable]
public class PosState
{
public string name;
public Vector3 lpos;
public Vector3 lcSc;
public Quaternion rt;
public Transform parent;
public PosState(string _name, Vector3 _lpos, Vector3 _lcSc, Quaternion _rt, Transform _prt)
{
name = _name;
lpos = _lpos;
lcSc = _lcSc;
rt = _rt;
parent = _prt;
}
//用于重命名
public PosState(string _name, PosState _ps)
{
parent = _ps.parent;
name = _name;
lpos = _ps.lpos;
lcSc = _ps.lcSc;
rt = _ps.rt;
}
}
//void Start()
//{
// rcdName = gameObject.name + "_s" + "PosStateRecord";
//}
public List<PosState> pss = new List<PosState>();
public Vector3 getLocalPosition(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.lpos;
}
}
Debug.LogError("没找到该位置");
return Vector3.zero;
}
public bool setLocation(string _name)
{
for (int i = 0; i < pss.Count; ++i )
{
if (pss[i].name.CompareTo(_name) == 0)
{
pss[i] = new PosStateRecord.PosState(pss[i].name,
transform.localPosition,
transform.localScale,
transform.localRotation,
transform.parent);
return true;
}
}
return false;
}
public void setPosition(string _name, Vector3 _pos)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
Transform p = getParent(_name);
if (p == null)
{
ps.lpos = _pos;
}
else
{
ps.lpos = p.InverseTransformPoint(_pos);
}
}
}
}
public Vector3 getPosition(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
Transform p = getParent(_name);
if (p == null)
{
return getLocalPosition(_name);
}
else
{
return p.TransformPoint(ps.lpos);
}
}
}
Debug.LogError("没找到该位置");
return Vector3.zero;
}
public Vector3 getLocalScale(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.lcSc;
}
}
Debug.LogError("没找到该缩放");
return Vector3.one;
}
public Quaternion getLocalRotation(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.rt;
}
}
Debug.LogError("没找到该旋转");
return Quaternion.identity;
}
public Quaternion getRotation(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
Transform p = getParent(_name);
if (p == null)
{
return getLocalRotation(_name);
}
else
{
Transform tp = new GameObject().transform;
tp.name = "forRcdChange";
tp.parent = p;
tp.localRotation = ps.rt;
Quaternion res = tp.rotation;
DestroyImmediate(tp.gameObject);
return res;
}
}
}
Debug.LogError("没找到该旋转");
return Quaternion.identity;
}
public Transform getParent(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.parent;
}
}
Debug.LogError("没找到该父物体");
return null;
}
public Transform getLocation(string name)
{
Transform res = new GameObject().transform;
res.name = rcdName + "_s_" + name;
location(res, name);
return res;
}
public Transform getLocation(PosState _ps)
{
Transform res = new GameObject().transform;
location(res, _ps);
return res;
}
public bool location(string name)
{
return location(transform, name);
}
public void smLocation(string name, float tm)
{
StartCoroutine(curshionSmLocation(name, tm));
}
IEnumerator curshionSmLocation(string name, float tm)
{
Transform targetPt = new GameObject().transform;
targetPt.position = getPosition(name);
targetPt.rotation = getRotation(name);
targetPt.localScale = getLocalScale(name);
iTween.MoveTo(gameObject,
iTween.Hash("x", targetPt.position.x,
"y", targetPt.position.y,
"z", targetPt.position.z,
"time", tm,
"easetype", iTween.EaseType.linear));
iTween.RotateTo(gameObject, iTween.Hash("x", targetPt.eulerAngles.x,
"y", targetPt.eulerAngles.y,
"z", targetPt.eulerAngles.z,
"time", tm,
"easetype", iTween.EaseType.linear));
iTween.ScaleTo(gameObject, iTween.Hash("x", targetPt.localScale.x,
"y", targetPt.localScale.y,
"z", targetPt.localScale.z,
"time", tm,
"easetype", iTween.EaseType.linear));
yield return new WaitForSeconds(tm + 1.0f);
DestroyImmediate(targetPt.gameObject);
location(name);
}
public bool location(Transform t,string name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(name) == 0)
{
return location(t, ps);
}
}
return false;
}
public bool location(PosState _ps)
{
location(transform, _ps);
return true;
}
public bool location(Transform t ,PosState _ps)
{
t.parent = _ps.parent;
t.localPosition = _ps.lpos;
t.localRotation = _ps.rt;
t.localScale = _ps.lcSc;
return true;
}
}
PosStateRecordEditor.cs
using UnityEngine;
using UnityEditor;
using System.Text;
using System.IO;
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Collections.Generic;
[CustomEditor(typeof(PosStateRecord))] //对UIWidget对象编辑窗进行的重构
public class PosStateRecordEditor : Editor
{
static bool isShowDef = false;
public string stName;
public override void OnInspectorGUI()
{
PosStateRecord psr = (PosStateRecord)target;
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Wei");
psr.rcdName = EditorGUILayout.TextField(psr.rcdName);
GUILayout.Label("Create");
if (GUILayout.Button("Mingwei"))
{
psr.rcdName = psr.transform.name;
}
stName = EditorGUILayout.TextField(stName);
//GUILayout.Label("的状态");
if (GUILayout.Button("Save"))
{
if (!nameCheck(stName))
{
EditorUtility.DisplayDialog("错误",
"记录器不允许空名,重名等现象。因为这些名字最后是用来做检索的,取个有意义点的名字吧!以后好找!",
"这就改名");
return;
}
psr.pss.Add(new PosStateRecord.PosState(stName,
psr.transform.localPosition,
psr.transform.localScale,
psr.transform.localRotation,
psr.transform.parent));
}
GUILayout.EndHorizontal();
for (int i = 0; i < psr.pss.Count; ++i)
{
GUILayout.BeginHorizontal();
GUILayout.Label(i.ToString());
psr.pss[i] = new PosStateRecord.PosState(EditorGUILayout.TextField(psr.pss[i].name), psr.pss[i]);
if (GUILayout.Button("Reset"))
{
Undo.RegisterUndo(psr, "redefine state");
psr.pss[i] = new PosStateRecord.PosState(psr.pss[i].name,
psr.transform.localPosition,
psr.transform.localScale,
psr.transform.localRotation,
psr.transform.parent);
}
if (GUILayout.Button("GPS"))
{
psr.location(psr.pss[i]);
}
if (GUILayout.Button("Delete"))
{
Undo.RegisterUndo(psr, "del state");
psr.pss.RemoveAt(i);
//if (EditorUtility.DisplayDialog("注意",
// "不可逆操作,删除就回不来了。是的Ctrl+Z也回不来!",
// "确定删除", "放弃删除"))
//{
// psr.pss.RemoveAt(i);
//}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
if (isShowDef)
{
DrawDefaultInspector();
}
}
bool nameCheck(string _name)
{
if (_name == null || _name.Length <= 0)
{
return false;
}
return true;
}
}