public void TraverseHierarchy(Transform root) { foreach (Transform child in root) { GameObject Go = child.gameObject; SpriteRenderer spriteRenderer = Go.transform.GetComponent <SpriteRenderer>(); if (spriteRenderer) { if (spriteRenderer.sprite.name.Contains("Control")) { _Controls.Add(spriteRenderer); } else if (spriteRenderer.sprite.name.Contains("Bone")) { _Bones.Add(spriteRenderer); } } Puppet2D_ParentControl newParentCtrl = Go.transform.GetComponent <Puppet2D_ParentControl>(); if (newParentCtrl) { _ParentControls.Add(newParentCtrl); } Puppet2D_IKHandle newIKCtrl = Go.transform.GetComponent <Puppet2D_IKHandle>(); if (newIKCtrl) { _Ikhandles.Add(newIKCtrl); } TraverseHierarchy(child); } }
public static void CreateOrientControl() { GameObject bone = Selection.activeObject as GameObject; if (bone) { if (bone.GetComponent <SpriteRenderer>()) { if (!bone.GetComponent <SpriteRenderer>().sprite.name.Contains("Bone")) { Debug.LogWarning("This is not a Puppet2D Bone"); return; } } else { Debug.LogWarning("This is not a Puppet2D Bone"); return; } } else { Debug.LogWarning("This is not a Puppet2D Bone"); return; } GameObject globalCtrl = CreateGlobalControl(); foreach (Puppet2D_IKHandle ikhandle in globalCtrl.GetComponent <Puppet2D_GlobalControl>()._Ikhandles) { if ((ikhandle.bottomJointTransform == bone.transform) || (ikhandle.middleJointTransform == bone.transform)) { Debug.LogWarning("Can't create a orient Control on Bone; it alreay has an IK handle"); return; } } foreach (Puppet2D_ParentControl parentControl in globalCtrl.GetComponent <Puppet2D_GlobalControl>()._ParentControls) { if ((parentControl.bone.transform == bone.transform)) { Debug.LogWarning("Can't create a Parent Control on Bone; it alreay has an Parent Control"); return; } } foreach (Puppet2D_SplineControl splineCtrl in globalCtrl.GetComponent <Puppet2D_GlobalControl>()._SplineControls) { foreach (GameObject splineBone in splineCtrl.bones) { if (splineBone.transform == bone.transform) { Debug.LogWarning(bone.transform.parent.transform.parent.name + " has a Spline control attached"); return; } } } GameObject control = new GameObject(); Undo.RegisterCreatedObjectUndo(control, "Created control"); control.name = (bone.name + "_CTRL"); GameObject controlGroup = new GameObject(); Undo.RegisterCreatedObjectUndo(controlGroup, "Created controlGroup"); controlGroup.name = (bone.name + "_CTRL_GRP"); control.transform.parent = controlGroup.transform; controlGroup.transform.position = bone.transform.position; controlGroup.transform.rotation = bone.transform.rotation; SpriteRenderer spriteRenderer = control.AddComponent <SpriteRenderer>(); string path = (Puppet2D_Editor._puppet2DPath + "/Textures/GUI/OrientControl.psd"); Sprite sprite = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite; spriteRenderer.sprite = sprite; spriteRenderer.sortingLayerName = Puppet2D_Editor._controlSortingLayer; Puppet2D_ParentControl parentConstraint = control.AddComponent <Puppet2D_ParentControl>(); parentConstraint.IsEnabled = true; parentConstraint.Orient = true; parentConstraint.Point = false; parentConstraint.bone = bone; Selection.activeObject = control; parentConstraint.OffsetScale = bone.transform.localScale; controlGroup.transform.parent = globalCtrl.transform; if (globalCtrl.GetComponent <Puppet2D_GlobalControl>().AutoRefresh) { globalCtrl.GetComponent <Puppet2D_GlobalControl>().Init(); } else { globalCtrl.GetComponent <Puppet2D_GlobalControl>()._ParentControls.Add(parentConstraint); } }