Skip to content

Commit c8c5fa1

Browse files
mjholtzemBanbury
authored andcommitted
Streamlined the IK Helper creation process
Modified the IK Editor script with a function to automatically add a helper Added a new Create Helper button to the editor that... - Creates Helper named in relation to the bone - Sets it as the IK Components target - sets the skeleton as a parent - Positions the helper at the bone - automatically selects the helper
1 parent b206a7a commit c8c5fa1

7 files changed

+102
-79
lines changed

Assets/SpritesAndBones/Scripts/Editor/InverseKinematicsEditor.cs

+38
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,20 @@ THE SOFTWARE.
2828

2929
[CustomEditor(typeof(InverseKinematics))]
3030
public class InverseKinematicsEditor : Editor {
31+
32+
private InverseKinematics ik;
33+
34+
void OnEnable() {
35+
ik = (InverseKinematics)target;
36+
}
37+
3138
public override void OnInspectorGUI() {
3239
DrawDefaultInspector();
3340

41+
if (GUILayout.Button("Create Target Helper")) {
42+
CreateHelper();
43+
}
44+
3445
if (((InverseKinematics)target).target == null) {
3546
EditorGUILayout.HelpBox("Please select a target.", MessageType.Error);
3647
}
@@ -40,4 +51,31 @@ public override void OnInspectorGUI() {
4051
static void DrawIKGizmo(InverseKinematics ik, GizmoType gizmoType) {
4152
Handles.Label(ik.transform.position + new Vector3(0.1f, 0), "IK");
4253
}
54+
55+
//Create a Helper for the IK Component and sets it as the IK's Target
56+
private void CreateHelper(){
57+
//Create the Helper GameObject named after the bone
58+
GameObject o = new GameObject (ik.name + "_IK");
59+
Undo.RegisterCreatedObjectUndo (o, "Create helper");
60+
o.AddComponent<Helper>();
61+
o.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
62+
63+
//set the helpers position to match the bones position
64+
Bone b = ik.GetComponent<Bone>();
65+
if (b != null) {
66+
o.transform.position = b.Head;
67+
}
68+
else {
69+
o.transform.position = ik.transform.position;
70+
}
71+
72+
//set the helper as a child of the skeleton
73+
o.transform.parent = ik.transform.root.GetComponentInChildren<Skeleton>().transform;
74+
75+
//set the helper as the target
76+
ik.target = o.transform;
77+
78+
//selects the transform of the Helper
79+
Selection.activeTransform = o.transform;
80+
}
4381
}

Assets/SpritesAndBones/Scripts/Editor/SkeletonEditor.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[CustomEditor(typeof(Skeleton))]
88
public class SkeletonEditor : Editor {
99
private Skeleton skeleton;
10-
private string poseFileName = "";
10+
private string poseFileName = "New Pose";
1111
void OnEnable() {
1212
skeleton = (Skeleton)target;
1313
}
@@ -34,9 +34,6 @@ public override void OnInspectorGUI() {
3434
}
3535

3636
GUILayout.EndHorizontal();
37-
if (poseFileName==null || poseFileName.Trim ()==""){
38-
EditorGUILayout.HelpBox("Give your pose a valid filename before saving", MessageType.Warning);
39-
}
4037
if (skeleton.basePose == null) {
4138
EditorGUILayout.HelpBox("You have not selected a base pose.", MessageType.Error);
4239
}
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,59 @@
1-
/*
2-
The MIT License (MIT)
3-
4-
Copyright (c) 2013 Banbury
5-
6-
Permission is hereby granted, free of charge, to any person obtaining a copy
7-
of this software and associated documentation files (the "Software"), to deal
8-
in the Software without restriction, including without limitation the rights
9-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
copies of the Software, and to permit persons to whom the Software is
11-
furnished to do so, subject to the following conditions:
12-
13-
The above copyright notice and this permission notice shall be included in
14-
all copies or substantial portions of the Software.
15-
16-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
THE SOFTWARE.
23-
*/
24-
using UnityEngine;
25-
#if UNITY_EDITOR
26-
using UnityEditor;
27-
#endif
28-
using System.IO;
29-
30-
public static class ScriptableObjectUtility {
31-
/// <summary>
32-
// This makes it easy to create, name and place unique new ScriptableObject asset files.
33-
/// </summary>
34-
#if UNITY_EDITOR
35-
public static void CreateAsset (Object asset)
36-
{
37-
string path = AssetDatabase.GetAssetPath (Selection.activeObject);
38-
if (path == "") {
39-
path = "Assets";
40-
} else if (Path.GetExtension (path) != "") {
41-
path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
42-
}
43-
44-
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/New " + asset.GetType ().ToString () + ".asset");
45-
46-
AssetDatabase.CreateAsset (asset, assetPathAndName);
47-
48-
49-
AssetDatabase.SaveAssets ();
50-
51-
Selection.activeObject = asset;
52-
}
53-
54-
public static void CreateAsset (Object asset, string filename)
55-
{
56-
string path = AssetDatabase.GetAssetPath (Selection.activeObject);
57-
if (path == "") {
58-
path = "Assets";
59-
} else if (Path.GetExtension (path) != "") {
60-
path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
61-
}
62-
63-
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/" + filename + ".asset");
64-
65-
AssetDatabase.CreateAsset (asset, assetPathAndName);
66-
67-
68-
AssetDatabase.SaveAssets ();
69-
70-
Selection.activeObject = asset;
71-
}
72-
#endif
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2013 Banbury
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
using UnityEngine;
25+
#if UNITY_EDITOR
26+
using UnityEditor;
27+
#endif
28+
using System.IO;
29+
30+
public static class ScriptableObjectUtility {
31+
/// <summary>
32+
// This makes it easy to create, name and place unique new ScriptableObject asset files.
33+
/// </summary>
34+
#if UNITY_EDITOR
35+
public static void CreateAsset (Object asset)
36+
{
37+
CreateAsset(asset, "/New " + asset.GetType().ToString());
38+
}
39+
40+
public static void CreateAsset (Object asset, string filename)
41+
{
42+
string path = AssetDatabase.GetAssetPath (Selection.activeObject);
43+
if (path == "") {
44+
path = "Assets";
45+
} else if (Path.GetExtension (path) != "") {
46+
path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
47+
}
48+
49+
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/" + filename + ".asset");
50+
51+
AssetDatabase.CreateAsset (asset, assetPathAndName);
52+
53+
54+
AssetDatabase.SaveAssets ();
55+
56+
Selection.activeObject = asset;
57+
}
58+
#endif
7359
}

AssetsTestPose.asset

-4.12 KB
Binary file not shown.

Assetsnew pose 4.asset

-4.12 KB
Binary file not shown.

Assetsnew pose.asset

-4.12 KB
Binary file not shown.

UnitySpritesAndBones.userprefs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<Properties>
2-
<MonoDevelop.Ide.Workspace />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\SpritesAndBones\Scripts\Editor\SkeletonEditor.cs">
2+
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3+
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\SpritesAndBones\Scripts\ScriptableObjectUtility.cs">
44
<Files>
5-
<File FileName="Assets\SpritesAndBones\Scripts\Editor\SkeletonEditor.cs" Line="1" Column="1" />
5+
<File FileName="Assets\SpritesAndBones\Scripts\Skeleton.cs" Line="140" Column="4" />
6+
<File FileName="Assets\SpritesAndBones\Scripts\Editor\SkeletonEditor.cs" Line="27" Column="36" />
7+
<File FileName="Assets\SpritesAndBones\Scripts\ScriptableObjectUtility.cs" Line="63" Column="103" />
68
</Files>
79
</MonoDevelop.Ide.Workbench>
810
<MonoDevelop.Ide.DebuggingService.Breakpoints>

0 commit comments

Comments
 (0)