using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; namespace MetaClient { public class AniRoByTime : MonoBehaviour { public Dictionary dicRo = new Dictionary(); public Dictionary pos = new Dictionary(); private Quaternion initRo = new Quaternion(); private Vector3 initPos = new Vector3(); private int startTime = 0; private Vector3 startPos = new Vector3(); private Vector3 endPos = new Vector3(); private Quaternion startRo = new Quaternion(); private int endTime = 0; private Quaternion endRo = new Quaternion(); private List mList = new List(); private int lasttime = 0; public AniManager aniManager; public bool allBody=false; // Start is called before the first frame update void Start() { } public void Init() { if (this.gameObject.name == aniManager.nowAniName) { allBody = true; initRo = this.gameObject.transform.localRotation; initPos = this.gameObject.transform.localPosition; } else { initRo = this.gameObject.transform.parent.localRotation; } } public void DicOrderBy() { Dictionary newdic = dicRo.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); dicRo = newdic; Dictionary newpos=pos.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value); pos = newpos; } public void RoAllBodyChangeFun(int _time) { startTime = 0; startRo = initRo; startPos = initPos; endPos = initPos; endTime = 0; endRo = initRo; lasttime = -1; if (dicRo.Count == 0) { // return; this.gameObject.transform.localRotation = initRo; this.gameObject.transform.localPosition = initPos; return; } else if (dicRo.Count == 1) { mList = new List(); var value = dicRo.Values.GetEnumerator(); value.MoveNext(); var valuePos = pos.Values.GetEnumerator(); valuePos.MoveNext(); this.gameObject.transform.localRotation = value.Current; this.gameObject.transform.localPosition = valuePos.Current; } else { mList = new List(); var value = dicRo.Keys.GetEnumerator(); var valuePos = pos.Values.GetEnumerator(); while (value.MoveNext()) { valuePos.MoveNext(); int data = value.Current; if (_time < data) { endTime = data; endRo = dicRo[endTime]; endPos = pos[endTime]; if (lasttime != -1) { startTime = lasttime; startRo = dicRo[lasttime]; startPos = pos[lasttime]; RoAllBodyChange(_time); } else { if (dicRo.ContainsKey(lasttime)) { this.gameObject.transform.localRotation = dicRo[lasttime]; this.gameObject.transform.localPosition = pos[lasttime]; } else { this.gameObject.transform.localPosition = endPos; } return; //startTime = endTime; //startRo = endRo; } } else { lasttime = value.Current; } } } } public void RoChangeFun(int _time) { if (allBody) { RoAllBodyChangeFun(_time); return; } startTime = 0; startRo = initRo; endTime = 0; endRo = initRo; lasttime = -1; if (dicRo.Count == 0) { // return; this.gameObject.transform.parent.localRotation = initRo; return; } else if (dicRo.Count == 1) { //List mList; mList = new List(); var value = dicRo.Values.GetEnumerator(); value.MoveNext(); this.gameObject.transform.parent.localRotation = value.Current; //while (value.MoveNext()) //{ // Quaternion data = value.Current; // mList.Add(data); //} //this.gameObject.transform.localEulerAngles = dicRo; } else { mList = new List(); var value = dicRo.Keys.GetEnumerator(); while (value.MoveNext()) { int data = value.Current; if (_time < data) { endTime = data; endRo = dicRo[endTime]; if (lasttime != -1) { startTime = lasttime; startRo = dicRo[lasttime]; RoChange(_time); } else { if (dicRo.ContainsKey(lasttime)) { this.gameObject.transform.parent.localRotation = dicRo[lasttime]; } else { this.gameObject.transform.parent.localRotation = endRo; } return; //startTime = endTime; //startRo = endRo; } } else { lasttime = value.Current; } } } } public void RoAllBodyChange(int _time) { Quaternion _nextRo = new Quaternion(); Vector3 _nextPos = new Vector3(); if (endTime <= _time) { this.gameObject.transform.localRotation = endRo; this.gameObject.transform.localPosition = endPos; return; } else if (endTime == startTime) { this.gameObject.transform.localRotation = endRo; this.gameObject.transform.localPosition = endPos; return; } _nextRo = Quaternion.Lerp(endRo, startRo, (endTime - _time) / (endTime - startTime)); Debug.Log("结束旋转" + endRo + ""); Debug.Log("开始旋转" + startRo + ""); this.gameObject.transform.localRotation = _nextRo; _nextPos =Vector3.Lerp(endPos, startPos, (endTime - _time) / (endTime - startTime)); Debug.Log("结束距离" + endPos + ""); Debug.Log("开始距离" + startPos + ""); this.gameObject.transform.localPosition = _nextPos; } public void RoChange(int _time) { Quaternion _nextRo = new Quaternion(); if (endTime <= _time) { this.gameObject.transform.parent.localRotation = endRo; return; } else if (endTime == startTime) { this.gameObject.transform.parent.localRotation = endRo; return; } _nextRo = Quaternion.Lerp(endRo, startRo, (endTime - _time) / (endTime - startTime)); Debug.Log("结束距离" + endRo + ""); Debug.Log("开始距离" + startRo + ""); this.gameObject.transform.parent.localRotation = _nextRo; } public void DicAdd(int _time) { if (allBody) { if (dicRo.ContainsKey(_time)) { dicRo[_time] = this.gameObject.transform.localRotation; pos[_time] = this.gameObject.transform.localPosition; } else { dicRo.Add(_time, this.gameObject.transform.localRotation); pos[_time] = this.gameObject.transform.localPosition; } } else { if (dicRo.ContainsKey(_time)) { dicRo[_time] = this.gameObject.transform.parent.localRotation; } else { dicRo.Add(_time, this.gameObject.transform.parent.localRotation); } } } // Update is called once per frame void Update() { } } }