private void KeyFrameGUI() { for (int i = 0; i < _morphAnimation.AnimationFrames.Count; i++) { SetGUIColor(Color.white, _currentFrame == _morphAnimation.AnimationFrames[i] ? Color.cyan : Color.white); Rect rect = new Rect(_morphAnimation.AnimationFrames[i].EditorPosition.x - _buttonSize.x / 2 , _morphAnimation.AnimationFrames[i].EditorPosition.y - _buttonSize.y / 2, _buttonSize.x, _buttonSize.y); if (GUI.RepeatButton(rect, _morphAnimation.AnimationFrames[i].name)) { _currentFrame = _morphAnimation.AnimationFrames[i]; SetFrameData(_currentFrame); _frameEdit = true; _morphAnimation.AnimationFrames[i].EditorPosition = Event.current.mousePosition; Repaint(); } if (_isRename && _currentFrame == _morphAnimation.AnimationFrames[i]) { SetGUIColor(Color.white, Color.red); rect = new Rect(_currentFrame.EditorPosition.x + _buttonSize.x / 2 + 5 , _currentFrame.EditorPosition.y - 10, _labelSize.x, _labelSize.y); _newName = GUI.TextField(rect, _newName); rect = new Rect(_currentFrame.EditorPosition.x + _buttonSize.x / 2 + _labelSize.x + 5 , _currentFrame.EditorPosition.y - 10, _labelSize.x / 2, _labelSize.y); if (GUI.Button(rect, "Sure")) { _isRename = false; _currentFrame.name = _newName; } } } }
private void SetFrameData(MorphAnimationFrame maf) { for (int i = 0; i < _morphAnimation.Bones.Count; i++) { _morphAnimation.Bones[i].localPosition = maf.Positions[i]; _morphAnimation.Bones[i].localRotation = maf.Rotations[i]; _morphAnimation.Bones[i].localScale = maf.Scales[i]; } }
private void Update() { if (_isPlaying) { MorphAnimationFrame currentMaf = Clip.AnimationFrames[_playIndex]; MorphAnimationFrame lastMaf; if (_playIndex + 1 >= Clip.AnimationFrames.Count) { lastMaf = Clip.AnimationFrames[0]; } else { lastMaf = Clip.AnimationFrames[_playIndex + 1]; } if (_playLocation <= currentMaf.Time) { _playLocation += Speed * Time.deltaTime; } else { _playIndex += 1; _playLocation = 0f; if (_playIndex >= Clip.AnimationFrames.Count) { _playIndex = 0; } if (!Loop && _playIndex == 0) { Stop(); } return; } float location = _playLocation / currentMaf.Time; for (int i = 0; i < _bones.Length; i++) { if (Clip.IsPosition) { _bones[i].localPosition = Vector3.Lerp(currentMaf.Positions[i], lastMaf.Positions[i], location); } if (Clip.IsRotation) { _bones[i].localRotation = Quaternion.Lerp(currentMaf.Rotations[i], lastMaf.Rotations[i], location); } if (Clip.IsScale) { _bones[i].localScale = Vector3.Lerp(currentMaf.Scales[i], lastMaf.Scales[i], location); } } } }
private void GetFrameData(MorphAnimationFrame maf) { maf.Positions.Clear(); maf.Rotations.Clear(); maf.Scales.Clear(); for (int i = 0; i < _morphAnimation.Bones.Count; i++) { maf.Positions.Add(_morphAnimation.Bones[i].localPosition); maf.Rotations.Add(_morphAnimation.Bones[i].localRotation); maf.Scales.Add(_morphAnimation.Bones[i].localScale); } }
private void PreviewAnimation() { if (_isPreview) { MorphAnimationFrame maf = _morphAnimation.TimeLine[_previewIndex]; MorphAnimationFrame lastmaf; if (_previewIndex + 1 >= _morphAnimation.TimeLine.Count) { lastmaf = _morphAnimation.TimeLine[0]; } else { lastmaf = _morphAnimation.TimeLine[_previewIndex + 1]; } if (_previewLocation <= maf.Time) { _previewLocation += Time.deltaTime; } else { _previewIndex += 1; _previewLocation = 0f; if (_previewIndex >= _morphAnimation.TimeLine.Count) { _previewIndex = 0; } return; } float location = _previewLocation / maf.Time; for (int i = 0; i < _morphAnimation.Bones.Count; i++) { if (_morphAnimation.IsPosition) { _morphAnimation.Bones[i].localPosition = Vector3.Lerp(maf.Positions[i], lastmaf.Positions[i], location); } if (_morphAnimation.IsRotation) { _morphAnimation.Bones[i].localRotation = Quaternion.Lerp(maf.Rotations[i], lastmaf.Rotations[i], location); } if (_morphAnimation.IsScale) { _morphAnimation.Bones[i].localScale = Vector3.Lerp(maf.Scales[i], lastmaf.Scales[i], location); } } } }
public void Init(MorphAnimation ma) { _morphAnimation = ma; _currentFrame = null; _currentBone = null; _currentBoneParent = null; _boneDistance = 0f; _frameEdit = false; _isPreview = false; _previewIndex = 0; _previewLocation = 0f; _buttonSize = new Vector2(100, 30); _labelSize = new Vector2(100, 20); _isRename = false; _newName = ""; _previewButton = new GUIContent("", "Preview Animation"); _addFrameButton = new GUIContent("", "Add Frame To TimeLine"); _height = 100; _space = 2; EditorApplication.update += Update; }
private void TimeLineGUI() { _scorll1 = EditorGUILayout.BeginScrollView(_scorll1); EditorGUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("Time Line:", "PreLabel", GUILayout.Width(100)); for (int i = 0; i < _morphAnimation.TimeLine.Count; i++) { SetGUIColor(Color.white, _currentFrame == _morphAnimation.TimeLine[i] ? Color.cyan : Color.white); string content = (i == 0 ? "GUIEditor.BreadcrumbLeft" : "GUIEditor.BreadcrumbMid"); if (GUILayout.Button(_morphAnimation.TimeLine[i].name, content, GUILayout.Width(50))) { _currentFrame = _morphAnimation.TimeLine[i]; SetFrameData(_currentFrame); _frameEdit = false; } } GUILayout.Space(10); SetGUIColor(Color.white, Color.white); if (GUILayout.Button(_addFrameButton, "OL Plus", GUILayout.Width(20))) { GenericMenu gm = new GenericMenu(); for (int i = 0; i < _morphAnimation.AnimationFrames.Count; i++) { int s = i; gm.AddItem(new GUIContent(_morphAnimation.AnimationFrames[s].name), false, delegate() { _morphAnimation.TimeLine.Add(_morphAnimation.AnimationFrames[s]); _morphAnimation.AnimationFrames.RemoveAt(s); _currentFrame = null; }); } gm.ShowAsContext(); } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndScrollView(); }
private void TitleGUI() { SetGUIColor(Color.white, Color.white); EditorGUILayout.BeginHorizontal("Toolbar"); if (GUILayout.Button(_morphAnimation.name, "Toolbarbutton")) { Selection.activeGameObject = _morphAnimation.gameObject; _currentFrame = null; _currentBone = null; } SetGUIColor(_isPreview ? Color.red : Color.white, Color.white); if (GUILayout.Button(_previewButton, "ProfilerTimelineFoldout")) { if (_morphAnimation.TimeLine.Count < 2) { Debug.LogWarning("请确保时间线上存在至少两个关键帧!"); return; } _isPreview = !_isPreview; if (_isPreview) { _previewIndex = 0; _previewLocation = 0f; } else { SetFrameData(_morphAnimation.TimeLine[0]); } } SetGUIColor(Color.white, Color.white); if (GUILayout.Button("Save Clip", "Toolbarbutton")) { string path = EditorUtility.SaveFilePanel("Save Clip", Application.dataPath, "New Clip", "asset"); if (path.Length != 0) { string subPath = path.Substring(0, path.IndexOf("Asset")); path = path.Replace(subPath, ""); MorphAnimationClip mac = new MorphAnimationClip(); mac.AnimationFrames = _morphAnimation.TimeLine; mac.IsPosition = _morphAnimation.IsPosition; mac.IsRotation = _morphAnimation.IsRotation; mac.IsScale = _morphAnimation.IsScale; AssetDatabase.CreateAsset(mac, path); AssetDatabase.SaveAssets(); } } GUILayout.FlexibleSpace(); SetGUIColor(Color.white, Color.white); SetGUIEnabled(!_isPreview); if (GUILayout.Button("Add Key Frame", "Toolbarbutton")) { MorphAnimationFrame maf = new MorphAnimationFrame("NewKeyFrame" + _morphAnimation.AnimationFrames.Count, new Vector2(position.width / 2, position.height / 2)); GetFrameData(maf); _morphAnimation.AnimationFrames.Add(maf); } #region Edit Key Frame if (_currentFrame != null) { SetGUIColor(Color.cyan, Color.white); if (_frameEdit) { if (GUILayout.Button("Rename", "Toolbarbutton")) { _isRename = !_isRename; if (_isRename) { _newName = _currentFrame.name; } } if (GUILayout.Button("Delete", "Toolbarbutton")) { _morphAnimation.AnimationFrames.Remove(_currentFrame); _currentFrame = null; } } else { if (GUILayout.Button("Leave", "Toolbarbutton")) { _morphAnimation.TimeLine.Remove(_currentFrame); _morphAnimation.AnimationFrames.Add(_currentFrame); _currentFrame = null; } if (GUILayout.Button("Insert", "Toolbarbutton")) { GenericMenu gm = new GenericMenu(); for (int i = 0; i < _morphAnimation.AnimationFrames.Count; i++) { int s = i; gm.AddItem(new GUIContent(_morphAnimation.AnimationFrames[s].name), false, delegate() { int index = _morphAnimation.TimeLine.IndexOf(_currentFrame); _morphAnimation.TimeLine.Insert(index, _morphAnimation.AnimationFrames[s]); _morphAnimation.AnimationFrames.RemoveAt(s); _currentFrame = null; }); } gm.ShowAsContext(); } if (GUILayout.Button("Replace", "Toolbarbutton")) { GenericMenu gm = new GenericMenu(); for (int i = 0; i < _morphAnimation.AnimationFrames.Count; i++) { int s = i; gm.AddItem(new GUIContent(_morphAnimation.AnimationFrames[s].name), false, delegate() { int index = _morphAnimation.TimeLine.IndexOf(_currentFrame); _morphAnimation.TimeLine.Insert(index, _morphAnimation.AnimationFrames[s]); _morphAnimation.AnimationFrames.RemoveAt(s); _morphAnimation.TimeLine.Remove(_currentFrame); _morphAnimation.AnimationFrames.Add(_currentFrame); _currentFrame = null; }); } gm.ShowAsContext(); } } } #endregion SetGUIColor(Color.white, Color.white); GUILayout.Label("position"); _morphAnimation.IsPosition = EditorGUILayout.Toggle(_morphAnimation.IsPosition, GUILayout.Width(15)); GUILayout.Label("rotation"); _morphAnimation.IsRotation = EditorGUILayout.Toggle(_morphAnimation.IsRotation, GUILayout.Width(15)); GUILayout.Label("scale"); _morphAnimation.IsScale = EditorGUILayout.Toggle(_morphAnimation.IsScale, GUILayout.Width(15)); EditorGUILayout.EndHorizontal(); }