public static void CreateControlPoints(SkinnedMeshRenderer skin) { if (skin.sharedMesh != null) { Skin2D skin2D = skin.GetComponent <Skin2D>(); if (skin2D != null) { skin2D.controlPoints = new ControlPoints.Point[skin.sharedMesh.vertices.Length]; if (skin2D.points == null) { skin2D.points = skin2D.gameObject.AddComponent <ControlPoints>(); } } for (int i = 0; i < skin.sharedMesh.vertices.Length; i++) { Vector3 originalPos = skin.sharedMesh.vertices[i]; if (skin2D != null) { skin2D.controlPoints[i] = new ControlPoints.Point(originalPos); skin2D.controlPoints[i].index = i; skin2D.points.SetPoint(skin2D.controlPoints[i]); } GameObject b = new GameObject(skin.name + " Control Point"); // Unparent the skin temporarily before adding the control point Transform skinParent = skin.transform.parent; skin.transform.parent = null; // Reset the rotation before creating the mesh so the UV's will align properly Quaternion localRotation = skin.transform.localRotation; skin.transform.localRotation = Quaternion.identity; b.transform.position = new Vector3(skin.transform.position.x + (skin.sharedMesh.vertices[i].x * skin.transform.localScale.x), skin.transform.position.y + (skin.sharedMesh.vertices[i].y * skin.transform.localScale.y), skin.transform.position.z + (skin.sharedMesh.vertices[i].z * skin.transform.localScale.z)); b.transform.parent = skin.transform; ControlPoint[] points = b.transform.parent.transform.GetComponentsInChildren <ControlPoint>(); if (points != null && points.Length > 0) { b.gameObject.name = b.gameObject.name + points.Length; } Undo.RegisterCreatedObjectUndo(b, "Add control point"); ControlPoint controlPoint = b.AddComponent <ControlPoint>(); controlPoint.index = i; controlPoint.skin = skin; controlPoint.originalPosition = b.transform.localPosition; // Reset the rotations of the object skin.transform.localRotation = localRotation; skin.transform.parent = skinParent; } } }
public static void Create() { // If we have a GameObject selected then use this to create the Skin2D if (Selection.activeGameObject != null) { GameObject o = Selection.activeGameObject; SkinnedMeshRenderer skin = o.GetComponent <SkinnedMeshRenderer>(); SpriteRenderer spriteRenderer = o.GetComponent <SpriteRenderer>(); // If we do not have a Skin2D already on this object and a SpriteRenderer then create it if (skin == null && spriteRenderer != null) { Sprite thisSprite = spriteRenderer.sprite; SpriteMesh spriteMesh = new SpriteMesh(); spriteMesh.spriteRenderer = spriteRenderer; spriteMesh.CreateSpriteMesh(); Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(spriteRenderer.sprite, false); // Copy the sprite material Material spriteMaterial = new Material(spriteRenderer.sharedMaterial); spriteMaterial.CopyPropertiesFromMaterial(spriteRenderer.sharedMaterial); spriteMaterial.mainTexture = spriteTexture; // Copy the sorting order and sorting layers of the Sprite string sortLayerName = spriteRenderer.sortingLayerName; int sortOrder = spriteRenderer.sortingOrder; // Kill the SpriteRenderer as we do not need it any more. DestroyImmediate(spriteRenderer); // Add the Skin2D to this object and assign the sprite Skin2D skin2D = o.AddComponent <Skin2D>(); skin2D.sprite = thisSprite; skin = o.GetComponent <SkinnedMeshRenderer>(); MeshFilter filter = o.GetComponent <MeshFilter>(); skin.material = spriteMaterial; // Save out the material from the sprite so we have a default material if (!Directory.Exists("Assets/Materials")) { AssetDatabase.CreateFolder("Assets", "Materials"); AssetDatabase.Refresh(); } AssetDatabase.CreateAsset(spriteMaterial, "Assets/Materials/" + spriteMaterial.mainTexture.name + ".mat"); Debug.Log("Created material " + spriteMaterial.mainTexture.name + " for " + skin.gameObject.name); skin2D.referenceMaterial = spriteMaterial; skin.sortingLayerName = sortLayerName; skin.sortingOrder = sortOrder; // Create the mesh from the selection filter.mesh = (Mesh)Selection.activeObject; if (filter.sharedMesh != null && skin.sharedMesh == null) { skin.sharedMesh = filter.sharedMesh; skin2D.referenceMesh = skin.sharedMesh; } // Recalculate the bone weights for the new mesh skin2D.RecalculateBoneWeights(); } else { // If there is a Skin2D or no SpriteRenderer then create a new GameObject with a Skin2D component o = new GameObject("Skin2D"); Undo.RegisterCreatedObjectUndo(o, "Create Skin2D"); o.AddComponent <Skin2D> (); } } else { // If there is no selected GameObject then create a new GameObject with a Skin2D component GameObject o = new GameObject("Skin2D"); Undo.RegisterCreatedObjectUndo(o, "Create Skin2D"); o.AddComponent <Skin2D> (); } }
void OnEnable() { skin = (Skin2D)target; skinnedMeshRenderer = skin.GetComponent <SkinnedMeshRenderer>(); skinnedMesh = skinnedMeshRenderer.sharedMesh; }