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("狀態(tài)記錄器")]
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("沒(méi)找到該位置");
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("沒(méi)找到該位置");
return Vector3.zero;
}
public Vector3 getLocalScale(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.lcSc;
}
}
Debug.LogError("沒(méi)找到該縮放");
return Vector3.one;
}
public Quaternion getLocalRotation(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.rt;
}
}
Debug.LogError("沒(méi)找到該旋轉(zhuǎn)");
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("沒(méi)找到該旋轉(zhuǎn)");
return Quaternion.identity;
}
public Transform getParent(string _name)
{
foreach (PosState ps in pss)
{
if (ps.name.CompareTo(_name) == 0)
{
return ps.parent;
}
}
Debug.LogError("沒(méi)找到該父物體");
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))] //對(duì)UIWidget對(duì)象編輯窗進(jìn)行的重構(gòu)
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("的狀態(tài)");
if (GUILayout.Button("Save"))
{
if (!nameCheck(stName))
{
EditorUtility.DisplayDialog("錯(cuò)誤",
"記錄器不允許空名趴腋,重名等現(xiàn)象宏所。因?yàn)檫@些名字最后是用來(lái)做檢索的,取個(gè)有意義點(diǎn)的名字吧怠益!以后好找!",
"這就改名");
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("注意",
// "不可逆操作硬爆,刪除就回不來(lái)了古瓤。是的Ctrl+Z也回不來(lái)!",
// "確定刪除", "放棄刪除"))
//{
// 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;
}
}