狀態(tài)記錄器(ITween)

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("沒找到該位置");
        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("沒找到該旋轉(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("沒找到該旋轉(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("沒找到該父物體");
        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對象編輯窗進(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)檫@些名字最后是用來做檢索的,取個(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("注意",
                //                           "不可逆操作,刪除就回不來了。是的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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近發(fā)現(xiàn)自己總是睡不好,每天醒得很早,醒來之后腦袋里就不停地轉(zhuǎn)動(dòng),在腦袋里播放的全是一些無關(guān)緊要的事,而且這樣的行...
    文字海洋里的一條魚閱讀 187評論 0 1
  • 『把大象放到冰箱里,需要哪三步?』——這是源于春晚小品的一個(gè)段子。 如果我們用編程語言Java來表達(dá)這個(gè)過程,那么...
    匿蟒閱讀 3,524評論 1 4
  • 手繪鑒賞 · 豆?jié){油條 作者:多一顆檸檬 法律 · 如果我被“兩規(guī)”,我會(huì)怎么辦? 作者:搬磚的蘆花 我曾是反貪局...
    簡黛玉閱讀 7,782評論 6 74
  • 她坐在燈下,拿著書看著。眼睛看著書,卻什么都沒有看進(jìn)去。已經(jīng)翻過去好幾頁了,可是看的什么內(nèi)容,她根本就不知道,只是...
    一縷情絲閱讀 329評論 0 4
  • 這周是我的陪護(hù)假第二周,一直在家里照顧妻子和孩子,還有做些家務(wù)。沒有執(zhí)行之前制定的計(jì)劃。 明天25號(hào)周一開始正式上...
    KUNGFUBUNNY閱讀 152評論 0 1

友情鏈接更多精彩內(nèi)容