static void Update() { EditorUpdaterProxy.Instance.onLateUpdate -= OnLateUpdate; EditorUpdaterProxy.Instance.onLateUpdate += OnLateUpdate; if (s_DraggingATool) { s_DraggingATool = false; string undoName = "Move"; if (Tools.current == Tool.Rotate) { undoName = "Rotate"; } if (Tools.current == Tool.Scale) { undoName = "Scale"; } for (int i = 0; i < Selection.transforms.Length; i++) { Transform transform = Selection.transforms[i]; Control control = transform.GetComponent <Control>(); if (control && control.isActiveAndEnabled && control.bone) { Undo.RecordObject(control.bone.transform, undoName); control.bone.transform.position = control.transform.position; control.bone.transform.rotation = control.transform.rotation; BoneUtils.OrientToChild(control.bone.parentBone, false, undoName, true); } Ik2D ik2D = transform.GetComponent <Ik2D>(); if (ik2D && ik2D.record) { IkUtils.UpdateIK(ik2D, undoName, true); } } SetDirty(); } AnimationModeCheck(); AnimationWindowTimeCheck(); IkUtils.UpdateAttachedIKs(s_Ik2Ds); UpdateIKs(); }
void OnSceneGUI() { if (Tools.current == Tool.Move) { Tools.hidden = true; float size = HandleUtility.GetHandleSize(m_Bone.transform.position) / 5f; Quaternion rotation = m_Bone.transform.rotation; EditorGUI.BeginChangeCheck(); Quaternion cameraRotation = Camera.current.transform.rotation; if (Event.current.type == EventType.Repaint) { Camera.current.transform.rotation = m_Bone.transform.rotation; } #if UNITY_5_6_OR_NEWER Vector3 newPosition = Handles.FreeMoveHandle(m_Bone.transform.position, rotation, size, Vector3.zero, Handles.RectangleHandleCap); #else Vector3 newPosition = Handles.FreeMoveHandle(m_Bone.transform.position, rotation, size, Vector3.zero, Handles.RectangleCap); #endif if (Event.current.type == EventType.Repaint) { Camera.current.transform.rotation = cameraRotation; } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(m_Bone.transform, "Move"); m_Bone.transform.position = newPosition; BoneUtils.OrientToChild(m_Bone.parentBone, Event.current.shift, Undo.GetCurrentGroupName(), true); EditorUtility.SetDirty(m_Bone.transform); EditorUpdater.SetDirty("Move"); } } else { Tools.hidden = false; } }
override public void OnInspectorGUI() { bool childChanged = false; serializedObject.Update(); EditorGUILayout.PropertyField(m_ColorProperty); EditorGUILayout.Slider(m_AlphaProperty, 0f, 1f, new GUIContent("Alpha")); Transform childTransform = null; if (m_Bone.child) { childTransform = m_Bone.child.transform; } EditorGUI.BeginDisabledGroup(targets.Length > 1); EditorGUI.showMixedValue = targets.Length > 1; EditorGUI.BeginChangeCheck(); Transform newChildTransform = EditorGUILayout.ObjectField(new GUIContent("Child"), childTransform, typeof(Transform), true) as Transform; if (EditorGUI.EndChangeCheck()) { if (newChildTransform && (newChildTransform.parent != m_Bone.transform || !newChildTransform.GetComponent <Bone2D>())) { newChildTransform = null; } m_ChildTransformProperty.objectReferenceValue = newChildTransform; childChanged = true; } EditorGUI.EndDisabledGroup(); EditorGUILayout.PropertyField(m_LengthProperty); serializedObject.ApplyModifiedProperties(); if (childChanged) { BoneUtils.OrientToChild(m_Bone, true, "set child", false); } }
public void BindBone(Bone2D bone) { if (spriteMeshInstance && bone) { BindInfo bindInfo = new BindInfo(); bindInfo.bindPose = bone.transform.worldToLocalMatrix * spriteMeshInstance.transform.localToWorldMatrix; bindInfo.boneLength = bone.localLength; bindInfo.path = BoneUtils.GetBonePath(bone); bindInfo.name = bone.name; bindInfo.color = ColorRing.GetColor(bindPoses.Count); if (!bindPoses.Contains(bindInfo)) { bindPoses.Add(bindInfo); isDirty = true; } } }
static Bone2D GetClosestBone(Vector2 mousePosition, float minDistance) { Bone2D closestBone = null; float minSqrDistance = float.MaxValue; List <Bone2D> l_bones = unlockedVisibleBones; for (int i = 0; i < l_bones.Count; i++) { Bone2D bone = l_bones[i]; if (bone) { float radius = BoneUtils.GetBoneRadius(bone); Vector3 direction = (bone.endPosition - bone.transform.position).normalized; Vector2 screenPosition = HandleUtility.WorldToGUIPoint(bone.transform.position); Vector3 endPoint = bone.endPosition - direction * radius; if (bone.child) { endPoint -= direction * BoneUtils.GetBoneRadius(bone.child); } Vector2 screenEndPosition = HandleUtility.WorldToGUIPoint(endPoint); float sqrDistance = MathUtils.SegmentSqrtDistance(mousePosition, screenPosition, screenEndPosition); if (sqrDistance < minSqrDistance && sqrDistance < minDistance * minDistance) { minSqrDistance = sqrDistance; closestBone = bone; } } } return(closestBone); }
public void BindBones() { if (spriteMeshInstance) { bindPoses.Clear(); foreach (Bone2D bone in spriteMeshInstance.bones) { if (bone) { BindInfo bindInfo = new BindInfo(); bindInfo.bindPose = bone.transform.worldToLocalMatrix * spriteMeshInstance.transform.localToWorldMatrix; bindInfo.boneLength = bone.localLength; bindInfo.path = BoneUtils.GetBonePath(bone); bindInfo.name = bone.name; bindPoses.Add(bindInfo); } } isDirty = true; } }
static void OnCurveWasModified(AnimationClip clip, EditorCurveBinding binding, AnimationUtility.CurveModifiedType deleted) { AnimationUtility.onCurveWasModified -= OnCurveWasModified; bool flag = Event.current == null || (Event.current != null && Event.current.type != EventType.ExecuteCommand); var rootGameOject = AnimationWindowExtra.rootGameObject; if (flag && rootGameOject && deleted == AnimationUtility.CurveModifiedType.CurveModified && binding.type == typeof(Transform) && binding.propertyName.Contains("localEulerAnglesRaw")) { Transform transform = AnimationWindowExtra.rootGameObject.transform.Find(binding.path); Vector3 eulerAngles = BoneUtils.GetLocalEulerAngles(transform); int frame = AnimationWindowExtra.frame; AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding); for (int i = 0; i < curve.length; i++) { Keyframe keyframe = curve[i]; int keyframeFrame = (int)AnimationWindowExtra.TimeToFrame(keyframe.time); if (frame == keyframeFrame) { if (binding.propertyName.Contains(".x")) { if (keyframe.value != eulerAngles.x) { //Debug.Log(binding.propertyName + " " + keyframe.value + " -> " + eulerAngles.x.ToString()); keyframe.value = eulerAngles.x; } } else if (binding.propertyName.Contains(".y")) { if (keyframe.value != eulerAngles.y) { //Debug.Log(binding.propertyName + " " + keyframe.value + " -> " + eulerAngles.y.ToString()); keyframe.value = eulerAngles.y; } } else if (binding.propertyName.Contains(".z")) { if (keyframe.value != eulerAngles.z) { //Debug.Log(binding.propertyName + " " + keyframe.value + " -> " + eulerAngles.z.ToString()); keyframe.value = eulerAngles.z; } } curve.MoveKey(i, keyframe); CurveUtility.UpdateTangentsFromModeSurrounding(curve, i); break; } } AnimationUtility.SetEditorCurve(clip, binding, curve); } AnimationUtility.onCurveWasModified += OnCurveWasModified; }
public static void OnSceneGUI(SceneView sceneview) { for (int i = 0; i < s_Bones.Count; i++) { Bone2D bone = s_Bones[i]; if (bone && IsVisible(bone)) { int controlID = GUIUtility.GetControlID("BoneHandle".GetHashCode(), FocusType.Passive); EventType eventType = Event.current.GetTypeForControl(controlID); if (!IsLocked(bone)) { if (eventType == EventType.MouseDown) { if (HandleUtility.nearestControl == controlID && Event.current.button == 0) { GUIUtility.hotControl = controlID; Event.current.Use(); } } if (eventType == EventType.MouseUp) { if (GUIUtility.hotControl == controlID && Event.current.button == 0) { if (EditorGUI.actionKey) { List <Object> objects = new List <Object>(Selection.objects); objects.Add(bone.gameObject); Selection.objects = objects.ToArray(); } else { Selection.activeObject = bone; } GUIUtility.hotControl = 0; Event.current.Use(); } } if (eventType == EventType.MouseDrag) { if (GUIUtility.hotControl == controlID && Event.current.button == 0) { Handles.matrix = bone.transform.localToWorldMatrix; Vector3 position = HandlesExtra.GUIToWorld(Event.current.mousePosition); BoneUtils.OrientToLocalPosition(bone, position, Event.current.shift, "Rotate", true); EditorUpdater.SetDirty("Rotate"); GUI.changed = true; Event.current.Use(); } } } if (eventType == EventType.Repaint) { if ((HandleUtility.nearestControl == controlID && GUIUtility.hotControl == 0) || GUIUtility.hotControl == controlID || Selection.gameObjects.Contains(bone.gameObject)) { Color color = Color.yellow; float outlineSize = HandleUtility.GetHandleSize(bone.transform.position) * 0.015f * bone.color.a; BoneUtils.DrawBoneOutline(bone, outlineSize, color); Bone2D outlineBone = bone.child; color.a *= 0.5f; while (outlineBone) { if (Selection.gameObjects.Contains(outlineBone.gameObject)) { outlineBone = null; } else { if (outlineBone.color.a == 0f) { outlineSize = HandleUtility.GetHandleSize(outlineBone.transform.position) * 0.015f * outlineBone.color.a; BoneUtils.DrawBoneOutline(outlineBone, outlineSize, color); outlineBone = outlineBone.child; color.a *= 0.5f; } else { outlineBone = null; } } } } if (bone.parentBone && !bone.linkedParentBone) { Color color = bone.color; color.a *= 0.25f; Handles.matrix = Matrix4x4.identity; BoneUtils.DrawBoneBody(bone.transform.position, bone.parentBone.transform.position, BoneUtils.GetBoneRadius(bone), color); } BoneUtils.DrawBoneBody(bone); Color innerColor = bone.color * 0.25f; if (bone.attachedIK && bone.attachedIK.isActiveAndEnabled) { innerColor = new Color(0f, 0.75f, 0.75f, 1f); } innerColor.a = bone.color.a; BoneUtils.DrawBoneCap(bone, innerColor); } if (!IsLocked(bone) && eventType == EventType.Layout) { HandleUtility.AddControl(controlID, HandleUtility.DistanceToLine(bone.transform.position, bone.endPosition)); } } } foreach (Control control in s_Controls) { if (control && control.isActiveAndEnabled && IsVisible(control.gameObject)) { Transform transform = control.transform; if (Selection.activeTransform != transform) { if (!control.bone || (control.bone && !Selection.transforms.Contains(control.bone.transform))) { Handles.matrix = Matrix4x4.identity; Handles.color = control.color; if (Tools.current == Tool.Move) { EditorGUI.BeginChangeCheck(); Quaternion cameraRotation = UnityEngine.Camera.current.transform.rotation; if (Event.current.type == EventType.Repaint) { UnityEngine.Camera.current.transform.rotation = transform.rotation; } float size = HandleUtility.GetHandleSize(transform.position) / 5f; //Handles.DrawLine(transform.position + transform.rotation * new Vector3(size,0f,0f), transform.position + transform.rotation * new Vector3(-size,0f,0f)); //Handles.DrawLine(transform.position + transform.rotation * new Vector3(0f,size,0f), transform.position + transform.rotation * new Vector3(0f,-size,0f)); bool guiEnabled = GUI.enabled; GUI.enabled = !IsLocked(control.gameObject); #if UNITY_5_6_OR_NEWER Vector3 newPosition = Handles.FreeMoveHandle(transform.position, transform.rotation, size, Vector3.zero, Handles.RectangleHandleCap); #else Vector3 newPosition = Handles.FreeMoveHandle(transform.position, transform.rotation, size, Vector3.zero, Handles.RectangleCap); #endif GUI.enabled = guiEnabled; if (Event.current.type == EventType.Repaint) { UnityEngine.Camera.current.transform.rotation = cameraRotation; } if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(transform, "Move"); transform.position = newPosition; if (control.bone) { Undo.RecordObject(control.bone.transform, "Move"); control.bone.transform.position = newPosition; BoneUtils.OrientToChild(control.bone.parentBone, Event.current.shift, "Move", true); EditorUpdater.SetDirty("Move"); } } } else if (Tools.current == Tool.Rotate) { EditorGUI.BeginChangeCheck(); float size = HandleUtility.GetHandleSize(transform.position) * 0.5f; //Handles.DrawLine(transform.position + transform.rotation * new Vector3(size,0f,0f), transform.position + transform.rotation * new Vector3(-size,0f,0f)); //Handles.DrawLine(transform.position + transform.rotation * new Vector3(0f,size,0f), transform.position + transform.rotation * new Vector3(0f,-size,0f)); bool guiEnabled = GUI.enabled; GUI.enabled = !IsLocked(control.gameObject); Quaternion newRotation = Handles.Disc(transform.rotation, transform.position, transform.forward, size, false, 0f); GUI.enabled = guiEnabled; if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(transform, "Rotate"); transform.rotation = newRotation; if (control.bone) { Undo.RecordObject(control.bone.transform, "Rotate"); control.bone.transform.rotation = newRotation; EditorUpdater.SetDirty("Rotate"); } } } } } } } }
static void DrawBoneGizmos() { Color blue = new Color(0f, 0.75f, 0.75f, 1f); List <Bone2D> l_bones = visibleBones; if (s_HoveredBone) { float outlineSize = HandleUtility.GetHandleSize(s_HoveredBone.transform.position) * 0.015f * s_HoveredBone.color.a; BoneUtils.DrawBoneOutline(s_HoveredBone, outlineSize, Color.yellow); } for (int j = 0; j < l_bones.Count; j++) { Bone2D bone = l_bones[j]; if (bone && bone.color.a == 0f) { continue; } if (bone && bone.gameObject.activeInHierarchy && bone.parentBone && bone.parentBone.child != bone) { Handles.matrix = Matrix4x4.identity; BoneUtils.DrawBoneBody(bone.transform.position, bone.parentBone.transform.position, BoneUtils.GetBoneRadius(bone), bone.color * new Color(1f, 1f, 1f, 0.5f)); } } for (int j = 0; j < l_bones.Count; j++) { Bone2D bone = l_bones[j]; if (bone && bone.color.a == 0f) { continue; } if (bone && bone.gameObject.activeInHierarchy) { BoneUtils.DrawBoneBody(bone); } } for (int j = 0; j < l_bones.Count; j++) { Bone2D bone = l_bones[j]; if (bone && bone.color.a == 0f) { continue; } if (bone && bone.gameObject.activeInHierarchy) { Color innerColor = bone.color * 0.25f; if (bone.attachedIK && bone.attachedIK.isActiveAndEnabled) { innerColor = blue; } innerColor.a = bone.color.a; BoneUtils.DrawBoneCap(bone, bone.color, innerColor); } } }
public static void OnSceneGUI(SceneView sceneview) { int dragBoneControlId = GUIUtility.GetControlID(FocusType.Passive); switch (Event.current.GetTypeForControl(dragBoneControlId)) { case EventType.MouseMove: Bone2D bone = GetClosestBone(Event.current.mousePosition, 10f); if (bone != s_HoveredBone) { s_HoveredBone = bone; SceneView.RepaintAll(); } break; case EventType.MouseDown: if (!ToolsExtra.viewToolActive && HandleUtility.nearestControl == dragBoneControlId && Event.current.button == 0) { s_SelectedBone = s_HoveredBone; Undo.IncrementCurrentGroup(); GUIUtility.hotControl = dragBoneControlId; Event.current.Use(); } break; case EventType.MouseUp: if (s_SelectedBone && GUIUtility.hotControl == dragBoneControlId && Event.current.button == 0) { Selection.activeGameObject = s_SelectedBone.gameObject; GUIUtility.hotControl = 0; s_SelectedBone = null; Event.current.Use(); } break; case EventType.MouseDrag: if (s_SelectedBone && GUIUtility.hotControl == dragBoneControlId && Event.current.button == 0) { Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); Plane plane = new Plane(s_SelectedBone.transform.forward, s_SelectedBone.transform.position); float distance; if (plane.Raycast(ray, out distance)) { Vector3 point = ray.GetPoint(distance); float angle = AngleAroundAxis(s_SelectedBone.transform.right, point - s_SelectedBone.transform.position, s_SelectedBone.transform.forward); Vector3 eulerAngles = s_SelectedBone.transform.localEulerAngles; eulerAngles.z += angle; Undo.RecordObject(s_SelectedBone.transform, "Rotate"); s_SelectedBone.transform.localRotation = Quaternion.Euler(eulerAngles); IkUtils.UpdateIK(s_SelectedBone, "Rotate"); } GUI.changed = true; Event.current.Use(); } break; case EventType.Repaint: DrawBoneGizmos(); break; case EventType.Layout: if (!ToolsExtra.viewToolActive && s_HoveredBone) { float radius = BoneUtils.GetBoneRadius(s_HoveredBone); Vector3 direction = (s_HoveredBone.endPosition - s_HoveredBone.transform.position).normalized; Vector2 screenPosition = HandleUtility.WorldToGUIPoint(s_HoveredBone.transform.position); Vector3 endPoint = s_HoveredBone.endPosition - direction * radius; if (s_HoveredBone.child) { endPoint -= direction * BoneUtils.GetBoneRadius(s_HoveredBone.child); } Vector2 screenEndPosition = HandleUtility.WorldToGUIPoint(endPoint); float distance = MathUtils.SegmentDistance(Event.current.mousePosition, screenPosition, screenEndPosition); HandleUtility.AddControl(dragBoneControlId, distance * 0.49f); } break; } }