void OnPostprocessSprites(Texture2D texture, Sprite[] sprites) { if (!s_Initialized) { return; } string guid = AssetDatabase.AssetPathToGUID(assetPath); if (s_SpriteMeshToTextureCache.ContainsValue(guid)) { foreach (Sprite sprite in sprites) { foreach (KeyValuePair <string, string> pair in s_SpriteMeshToTextureCache) { if (pair.Value == guid) { SpriteMesh spriteMesh = LoadSpriteMesh(AssetDatabase.GUIDToAssetPath(pair.Key)); if (spriteMesh && spriteMesh.sprite && sprite.name == spriteMesh.sprite.name) { DoSpriteOverride(spriteMesh, sprite); break; } } } } } }
static void UpdateCachedSpriteMesh(SpriteMesh spriteMesh) { if (spriteMesh) { string key = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteMesh)); if (spriteMesh.sprite) { SpriteMesh spriteMeshFromSprite = GetSpriteMeshFromSprite(spriteMesh.sprite); if (!spriteMeshFromSprite || spriteMesh == spriteMeshFromSprite) { string value = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(SpriteUtility.GetSpriteTexture(spriteMesh.sprite, false))); s_SpriteMeshToTextureCache[key] = value; } else { Debug.LogWarning("Anima2D: SpriteMesh " + spriteMesh.name + " uses the same Sprite as " + spriteMeshFromSprite.name + ". Use only one SpriteMesh per Sprite."); } } else if (s_SpriteMeshToTextureCache.ContainsKey(key)) { s_SpriteMeshToTextureCache.Remove(key); } } }
public void Update() { m_TransformTrackers.Clear(); m_BlendShapeWeightTracker.Clear(); m_SpriteMesh = null; if (m_SpriteMeshInstance && m_SpriteMeshInstance.spriteMesh) { m_SpriteMesh = m_SpriteMeshInstance.spriteMesh; m_TransformTrackers.Add(new TransformTracker(m_SpriteMeshInstance.transform)); foreach (Bone2D bone in m_SpriteMeshInstance.bones) { m_TransformTrackers.Add(new TransformTracker(bone.transform)); } if (m_SpriteMeshInstance.cachedSkinnedRenderer) { int blendShapeCount = m_SpriteMeshInstance.sharedMesh.blendShapeCount; for (int i = 0; i < blendShapeCount; ++i) { m_BlendShapeWeightTracker.Add(i, m_SpriteMeshInstance.cachedSkinnedRenderer.GetBlendShapeWeight(i)); } } } }
static void UpgradeSpriteMesh(SpriteMesh spriteMesh) { if (spriteMesh) { SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); SerializedProperty apiVersionProp = spriteMeshSO.FindProperty("m_ApiVersion"); if (apiVersionProp.intValue < SpriteMesh.api_version) { if (apiVersionProp.intValue < 1) { Upgrade_001(spriteMeshSO); } if (apiVersionProp.intValue < 2) { Upgrade_002(spriteMeshSO); } if (apiVersionProp.intValue < 3) { Upgrade_003(spriteMeshSO); } spriteMeshSO.Update(); apiVersionProp.intValue = SpriteMesh.api_version; spriteMeshSO.ApplyModifiedProperties(); AssetDatabase.SaveAssets(); } } }
public void SetSpriteMesh(SpriteMesh _spriteMesh, SpriteMeshInstance _spriteMeshInstance) { spriteMesh = _spriteMesh; spriteMeshInstance = _spriteMeshInstance; spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(_spriteMesh); RevertChanges(); }
public static SpriteMesh CreateSpriteMesh(Sprite sprite) { SpriteMesh spriteMesh = SpriteMeshPostprocessor.GetSpriteMeshFromSprite(sprite); SpriteMeshData spriteMeshData = null; if (!spriteMesh && sprite) { string spritePath = AssetDatabase.GetAssetPath(sprite); string directory = Path.GetDirectoryName(spritePath); string assetPath = AssetDatabase.GenerateUniqueAssetPath(directory + Path.DirectorySeparatorChar + sprite.name + ".asset"); spriteMesh = ScriptableObject.CreateInstance <SpriteMesh>(); InitFromSprite(spriteMesh, sprite); AssetDatabase.CreateAsset(spriteMesh, assetPath); spriteMeshData = ScriptableObject.CreateInstance <SpriteMeshData>(); spriteMeshData.name = spriteMesh.name + "_Data"; spriteMeshData.hideFlags = HideFlags.HideInHierarchy; InitFromSprite(spriteMeshData, sprite); AssetDatabase.AddObjectToAsset(spriteMeshData, assetPath); UpdateAssets(spriteMesh, spriteMeshData); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(assetPath); Selection.activeObject = spriteMesh; } return(spriteMesh); }
public static bool NeedsOverride(SpriteMesh spriteMesh) { if (!spriteMesh || !spriteMesh.sprite) { return(false); } SpriteMeshData spriteMeshData = LoadSpriteMeshData(spriteMesh); if (!spriteMeshData) { return(false); } ushort[] triangles = spriteMesh.sprite.triangles; if (triangles.Length != spriteMeshData.indices.Length) { return(true); } for (int i = 0; i < triangles.Length; i++) { if (spriteMeshData.indices[i] != triangles[i]) { return(true); } } return(false); }
public static SpriteMesh CreateSpriteMesh(Sprite sprite) { SpriteMesh spriteMesh = null; if (sprite) { string spritePath = AssetDatabase.GetAssetPath(sprite); string directory = Path.GetDirectoryName(spritePath); string prefabPath = AssetDatabase.GenerateUniqueAssetPath(directory + Path.DirectorySeparatorChar + sprite.name + ".prefab"); GameObject go = new GameObject(sprite.name); GameObject prefab = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab); GameObject.DestroyImmediate(go); spriteMesh = ScriptableObject.CreateInstance <SpriteMesh>(); spriteMesh.name = sprite.name; InitFromSprite(spriteMesh, sprite); AssetDatabase.AddObjectToAsset(spriteMesh, prefab); UpdateAssets(spriteMesh); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(prefabPath); Selection.activeObject = prefab; } return(spriteMesh); }
static void UpgradeSpriteMesh(SpriteMesh spriteMesh) { if (spriteMesh) { SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); SerializedProperty apiVersionProp = spriteMeshSO.FindProperty("m_ApiVersion"); if (apiVersionProp.intValue < SpriteMesh.api_version) { if (apiVersionProp.intValue < 2) { Debug.LogError("SpriteMesh " + spriteMesh + " was created using an ancient version of Anima2D which can't be upgraded anymore.\n" + "The last version that can upgrade this asset is Anima2D 1.1.5"); } if (apiVersionProp.intValue < 3) { Upgrade_003(spriteMeshSO); } if (apiVersionProp.intValue < 4) { Upgrade_004(spriteMeshSO); } spriteMeshSO.Update(); apiVersionProp.intValue = SpriteMesh.api_version; spriteMeshSO.ApplyModifiedProperties(); AssetDatabase.SaveAssets(); } } }
public static BlendShape CreateBlendShape(SpriteMesh spriteMesh, string blendshapeName) { BlendShape l_blendshape = null; SpriteMeshData spriteMeshData = LoadSpriteMeshData(spriteMesh); if (spriteMeshData) { l_blendshape = BlendShape.Create(blendshapeName); l_blendshape.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(l_blendshape, spriteMeshData); List <BlendShape> l_blendshapes = new List <BlendShape>(spriteMeshData.blendshapes); l_blendshapes.Add(l_blendshape); spriteMeshData.blendshapes = l_blendshapes.ToArray(); EditorUtility.SetDirty(spriteMeshData); EditorUtility.SetDirty(l_blendshape); } return(l_blendshape); }
public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, GameObject gameObject, bool undo = true) { SpriteMeshInstance spriteMeshInstance = null; if (spriteMesh && gameObject) { if (undo) { spriteMeshInstance = Undo.AddComponent <SpriteMeshInstance>(gameObject); } else { spriteMeshInstance = gameObject.AddComponent <SpriteMeshInstance>(); } spriteMeshInstance.spriteMesh = spriteMesh; spriteMeshInstance.sharedMaterial = defaultMaterial; SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); List <Bone2D> bones = new List <Bone2D>(); List <string> paths = new List <string>(); Vector4 zero = new Vector4(0f, 0f, 0f, 1f); foreach (BindInfo bindInfo in spriteMeshData.bindPoses) { Matrix4x4 m = spriteMeshInstance.transform.localToWorldMatrix * bindInfo.bindPose.inverse; GameObject bone = new GameObject(bindInfo.name); if (undo) { Undo.RegisterCreatedObjectUndo(bone, Undo.GetCurrentGroupName()); } Bone2D boneComponent = bone.AddComponent <Bone2D>(); boneComponent.localLength = bindInfo.boneLength; bone.transform.position = m * zero; bone.transform.rotation = m.GetRotation(); bone.transform.parent = gameObject.transform; bones.Add(boneComponent); paths.Add(bindInfo.path); } BoneUtils.ReconstructHierarchy(bones, paths); spriteMeshInstance.bones = bones; SpriteMeshUtils.UpdateRenderer(spriteMeshInstance, undo); EditorUtility.SetDirty(spriteMeshInstance); } return(spriteMeshInstance); }
void ResetSpriteMesh() { if (m_SpriteMeshOriginal != null && m_SpriteMeshOriginal.IsTemplate) { m_SpriteMesh = m_SpriteMeshOriginal; m_SpriteMeshOriginal = null; } }
static void Cleanup() { init = false; spriteMesh = null; instance = null; currentDestination = null; parentTransform = null; s_InstanceBones.Clear(); }
static void HandleDragAndDrop(bool createOnEnter, Transform parent) { switch (Event.current.type) { case EventType.DragUpdated: if (!init) { spriteMesh = GetSpriteMesh(); if (createOnEnter) { instance = CreateInstance(parent); } if (instance) { Event.current.Use(); } init = true; } if (instance) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; instance.transform.position = instancePosition; } break; case EventType.DragExited: if (instance) { GameObject.DestroyImmediate(instance); Event.current.Use(); } Cleanup(); break; case EventType.DragPerform: if (!createOnEnter) { instance = CreateInstance(parent); } if (instance) { Event.current.Use(); } Cleanup(); break; } }
static void InitFromSprite(SpriteMesh spriteMesh, Sprite sprite) { SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); SerializedProperty spriteProp = spriteMeshSO.FindProperty("m_Sprite"); SerializedProperty apiProp = spriteMeshSO.FindProperty("m_ApiVersion"); spriteMeshSO.Update(); apiProp.intValue = SpriteMesh.api_version; spriteProp.objectReferenceValue = sprite; spriteMeshSO.ApplyModifiedProperties(); }
public static Vector2 VertexToTexCoord(SpriteMesh spriteMesh, Vector2 pivotPoint, Vector3 vertex, float pixelsPerUnit) { Vector2 texCoord = Vector3.zero; if (spriteMesh != null) { texCoord = (Vector2)vertex * pixelsPerUnit + pivotPoint; } return(texCoord); }
static SpriteMesh.BindInfo[] GetBindPoses(SpriteMesh spriteMesh) { SpriteMesh.BindInfo[] result = null; FieldInfo fieldInfo = typeof(SpriteMesh).GetField("bindPoses", BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { result = (SpriteMesh.BindInfo[])fieldInfo.GetValue(spriteMesh); } return(result); }
public static Vector2 VertexToTexCoord(SpriteMesh spriteMesh, Vector3 vertex) { Vector2 texCoord = Vector3.zero; if (spriteMesh != null) { Vector3 pivotPoint = (Vector3)GetPivotPoint(spriteMesh.sprite); texCoord = vertex * spriteMesh.sprite.pixelsPerUnit + pivotPoint; } return(texCoord); }
public static Vector3 TexCoordToVertex(SpriteMesh spriteMesh, Vector2 texVertex) { Vector3 vertex = Vector3.zero; if (spriteMesh != null) { Vector3 pivotPoint = (Vector3)GetPivotPoint(spriteMesh.sprite); vertex = ((Vector3)texVertex - pivotPoint) / spriteMesh.sprite.pixelsPerUnit; } return(vertex); }
static void HideMaterials(SpriteMesh spriteMesh) { for (int i = 0; i < spriteMesh.sharedMaterials.Length; i++) { Material material = spriteMesh.sharedMaterials [i]; if (material) { material.hideFlags = HideFlags.HideInHierarchy; EditorUtility.SetDirty(material); } } }
public void Clear() { spriteMesh = null; spriteMeshInstance = null; selectedBone = null; selectedEdge = null; selectedVertices.Clear(); texVertices.Clear(); indexedEdges.Clear(); indices.Clear(); isDirty = false; }
static SpriteMesh GetSpriteMesh() { SpriteMesh l_spriteMesh = null; if (DragAndDrop.objectReferences.Length > 0) { Object obj = DragAndDrop.objectReferences[0]; l_spriteMesh = obj as SpriteMesh; } return(l_spriteMesh); }
void DoSetSkinnedRenderer() { SpriteMesh spriteMesh = mSpriteMeshInstance.spriteMesh; if (spriteMesh) { MeshFilter meshFilter = mSpriteMeshInstance.GetComponent <MeshFilter>(); MeshRenderer meshRenderer = mSpriteMeshInstance.GetComponent <MeshRenderer>(); Material material = null; if (meshFilter) { DestroyImmediate(meshFilter); } if (meshRenderer) { material = meshRenderer.sharedMaterial; DestroyImmediate(meshRenderer); } string path = AssetDatabase.GetAssetPath(spriteMesh); if (!material) { material = AssetDatabase.LoadAssetAtPath(path, typeof(Material)) as Material; } Mesh mesh = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh; SkinnedMeshRenderer skinnedMeshRenderer = mSpriteMeshInstance.GetComponent <SkinnedMeshRenderer>(); if (!skinnedMeshRenderer) { skinnedMeshRenderer = mSpriteMeshInstance.gameObject.AddComponent <SkinnedMeshRenderer>(); } if (skinnedMeshRenderer) { skinnedMeshRenderer.sharedMesh = mesh; skinnedMeshRenderer.sharedMaterial = material; skinnedMeshRenderer.bones = mSpriteMeshInstance.bones.ConvertAll(bone => bone.transform).ToArray(); if (mSpriteMeshInstance.bones.Count > 0) { skinnedMeshRenderer.rootBone = mSpriteMeshInstance.bones[0].transform; } } } mSpriteMeshInstance.UpdateRenderer(); }
void OnPreprocessTexture() { if (!s_Initialized) { return; } string guid = AssetDatabase.AssetPathToGUID(assetPath); if (s_SpriteMeshToTextureCache.ContainsValue(guid)) { TextureImporter textureImporter = (TextureImporter)assetImporter; SerializedObject textureImporterSO = new SerializedObject(textureImporter); SerializedProperty textureImporterSprites = textureImporterSO.FindProperty("m_SpriteSheet.m_Sprites"); foreach (KeyValuePair <string, string> pair in s_SpriteMeshToTextureCache) { if (pair.Value == guid) { SpriteMesh spriteMesh = LoadSpriteMesh(AssetDatabase.GUIDToAssetPath(pair.Key)); SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); if (spriteMesh && spriteMeshData && spriteMesh.sprite && spriteMeshData.vertices.Length > 0) { SerializedProperty spriteProp = null; int i = 0; string name = ""; while (i < textureImporterSprites.arraySize && name != spriteMesh.sprite.name) { spriteProp = textureImporterSprites.GetArrayElementAtIndex(i); name = spriteProp.FindPropertyRelative("m_Name").stringValue; ++i; } if (name == spriteMesh.sprite.name) { Rect textureRect = SpriteMeshUtils.CalculateSpriteRect(spriteMesh, 5); spriteProp.FindPropertyRelative("m_Rect").rectValue = textureRect; spriteProp.FindPropertyRelative("m_Alignment").intValue = 9; spriteProp.FindPropertyRelative("m_Pivot").vector2Value = Vector2.Scale(spriteMeshData.pivotPoint - textureRect.position, new Vector2(1f / textureRect.size.x, 1f / textureRect.size.y)); textureImporter.userData = textureImporter.assetTimeStamp.ToString(); } } } } textureImporterSO.ApplyModifiedProperties(); } }
static void ContextCreateSpriteMesh(MenuCommand menuCommand) { GameObject spriteRendererGO = Selection.activeGameObject; SpriteRenderer spriteRenderer = null; SpriteMesh spriteMesh = null; int sortingLayerID = 0; int sortingOrder = 0; if (spriteRendererGO) { spriteRenderer = spriteRendererGO.GetComponent <SpriteRenderer>(); } if (spriteRenderer && spriteRenderer.sprite) { sortingLayerID = spriteRenderer.sortingLayerID; sortingOrder = spriteRenderer.sortingOrder; SpriteMesh overrideSpriteMesh = SpriteMeshPostprocessor.GetSpriteMeshFromSprite(spriteRenderer.sprite); if (overrideSpriteMesh) { spriteMesh = overrideSpriteMesh; } else { spriteMesh = SpriteMeshUtils.CreateSpriteMesh(spriteRenderer.sprite); } } if (spriteMesh) { Undo.SetCurrentGroupName("create SpriteMeshInstance"); Undo.DestroyObjectImmediate(spriteRenderer); SpriteMeshInstance spriteMeshInstance = SpriteMeshUtils.CreateSpriteMeshInstance(spriteMesh, spriteRendererGO, true); spriteMeshInstance.sortingLayerID = sortingLayerID; spriteMeshInstance.sortingOrder = sortingOrder; Selection.activeGameObject = spriteRendererGO; } else { Debug.Log("Select a SpriteRenderer with a Sprite to convert to SpriteMesh"); } }
static void Upgrade_003(SerializedObject spriteMeshSO) { SpriteMesh spriteMesh = spriteMeshSO.targetObject as SpriteMesh; SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); if (spriteMesh.sprite && spriteMeshData) { TextureImporter textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(spriteMesh.sprite)) as TextureImporter; float maxImporterSize = textureImporter.maxTextureSize; int width = 1; int height = 1; SpriteMeshUtils.GetWidthAndHeight(textureImporter, ref width, ref height); int maxSize = Mathf.Max(width, height); float factor = maxSize / maxImporterSize; if (factor > 1f) { SerializedObject spriteMeshDataSO = new SerializedObject(spriteMeshData); SerializedProperty smdPivotPointProp = spriteMeshDataSO.FindProperty("m_PivotPoint"); SerializedProperty smdVerticesProp = spriteMeshDataSO.FindProperty("m_Vertices"); SerializedProperty smdHolesProp = spriteMeshDataSO.FindProperty("m_Holes"); spriteMeshDataSO.Update(); smdPivotPointProp.vector2Value = spriteMeshData.pivotPoint * factor; for (int i = 0; i < spriteMeshData.vertices.Length; ++i) { smdVerticesProp.GetArrayElementAtIndex(i).vector2Value = spriteMeshData.vertices[i] * factor; } for (int i = 0; i < spriteMeshData.holes.Length; ++i) { smdHolesProp.GetArrayElementAtIndex(i).vector2Value = spriteMeshData.holes[i] * factor; } spriteMeshDataSO.ApplyModifiedProperties(); EditorUtility.SetDirty(spriteMeshData); } } }
void DoSetMeshRenderer() { SpriteMesh spriteMesh = mSpriteMeshInstance.spriteMesh; if (spriteMesh) { SkinnedMeshRenderer skinnedMeshRenderer = mSpriteMeshInstance.GetComponent <SkinnedMeshRenderer>(); MeshFilter meshFilter = mSpriteMeshInstance.GetComponent <MeshFilter>(); MeshRenderer meshRenderer = mSpriteMeshInstance.GetComponent <MeshRenderer>(); Material material = null; if (skinnedMeshRenderer) { material = skinnedMeshRenderer.sharedMaterial; DestroyImmediate(skinnedMeshRenderer); } else if (meshRenderer) { material = meshRenderer.sharedMaterial; } string path = AssetDatabase.GetAssetPath(spriteMesh); Mesh mesh = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh; if (!meshFilter) { meshFilter = mSpriteMeshInstance.gameObject.AddComponent <MeshFilter>(); } if (meshFilter) { meshFilter.sharedMesh = mesh; } if (!meshRenderer) { meshRenderer = mSpriteMeshInstance.gameObject.AddComponent <MeshRenderer>(); } if (meshRenderer) { meshRenderer.sharedMaterial = material; } } mSpriteMeshInstance.UpdateRenderer(); }
public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, bool undo = true) { if (spriteMesh) { GameObject gameObject = new GameObject(spriteMesh.name); if (undo) { Undo.RegisterCreatedObjectUndo(gameObject, Undo.GetCurrentGroupName()); } return(CreateSpriteMeshInstance(spriteMesh, gameObject, undo)); } return(null); }
static void InitFromSprite(SpriteMesh spriteMesh, Sprite sprite) { SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); spriteMeshSO.Update(); SerializedProperty spriteProp = spriteMeshSO.FindProperty("sprite"); spriteProp.objectReferenceValue = sprite; spriteMeshSO.ApplyModifiedProperties(); GetInfoFromSprite(sprite, spriteMesh.texVertices, spriteMesh.edges, spriteMesh.indices); EditorUtility.SetDirty(spriteMesh); }
static void DoSpriteOverride(SpriteMesh spriteMesh, Sprite sprite) { SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); if (spriteMeshData) { Rect rect = SpriteMeshUtils.CalculateSpriteRect(spriteMesh, 5); Rect spriteRect = sprite.rect; Vector2 factor = new Vector2(spriteRect.width / rect.width, spriteRect.height / rect.height); Vector2[] newVertices = new List <Vector2>(spriteMeshData.vertices).ConvertAll(v => MathUtils.ClampPositionInRect(Vector2.Scale(v, factor), spriteRect) - spriteRect.position).ToArray(); ushort[] newIndices = new List <int>(spriteMeshData.indices).ConvertAll <ushort>(i => (ushort)i).ToArray(); sprite.OverrideGeometry(newVertices, newIndices); } }
static void DoSpriteOverride(SpriteMesh spriteMesh, Sprite sprite) { SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); if(spriteMeshData) { Rect rect = SpriteMeshUtils.CalculateSpriteRect(spriteMesh,5); Rect spriteRect = sprite.rect; Vector2 factor = new Vector2(spriteRect.width/rect.width,spriteRect.height/rect.height); Vector2[] newVertices = new List<Vector2>(spriteMeshData.vertices).ConvertAll( v => MathUtils.ClampPositionInRect(Vector2.Scale(v,factor),spriteRect) - spriteRect.position ).ToArray(); ushort[] newIndices = new List<int>(spriteMeshData.indices).ConvertAll<ushort>( i => (ushort)i ).ToArray(); sprite.OverrideGeometry(newVertices, newIndices); } }
static void UpdateCachedSpriteMesh(SpriteMesh spriteMesh) { if(spriteMesh) { string key = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteMesh)); if(spriteMesh.sprite) { SpriteMesh spriteMeshFromSprite = GetSpriteMeshFromSprite(spriteMesh.sprite); if(!spriteMeshFromSprite || spriteMesh == spriteMeshFromSprite) { string value = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(SpriteUtility.GetSpriteTexture(spriteMesh.sprite,false))); s_SpriteMeshToTextureCache[key] = value; }else{ Debug.LogWarning("Anima2D: SpriteMesh " + spriteMesh.name + " uses the same Sprite as " + spriteMeshFromSprite.name + ". Use only one SpriteMesh per Sprite."); } }else if(s_SpriteMeshToTextureCache.ContainsKey(key)) { s_SpriteMeshToTextureCache.Remove(key); } } }
static void HandleDragAndDrop(bool createOnEnter, Transform parent) { switch(Event.current.type) { case EventType.DragUpdated: if(!init) { spriteMesh = GetSpriteMesh(); if(createOnEnter) { parentTransform = null; CreateInstance(); } Event.current.Use(); init = true; } if(instance) { instance.transform.position = instancePosition; SpriteMeshInstance l_currentDestination = GetClosestBindeableIntersectingSpriteMeshInstance(); if(currentDestination != l_currentDestination) { currentDestination = l_currentDestination; if(currentDestination) { List<Bone2D> destinationBones = currentDestination.bones; List<Bone2D> newBones = new List<Bone2D>(); SpriteMeshData data = SpriteMeshUtils.LoadSpriteMeshData(instance.spriteMesh); for(int i = 0; i < data.bindPoses.Length; ++i) { BindInfo bindInfo = data.bindPoses[i]; int index = FindBindInfo(bindInfo,currentDestination); if(index >= 0) { newBones.Add(destinationBones[index]); } } instance.transform.parent = currentDestination.transform.parent; instance.bones = newBones; SpriteMeshUtils.UpdateRenderer(instance,false); foreach(Bone2D bone in s_InstanceBones) { bone.hideFlags = HideFlags.HideAndDontSave; bone.gameObject.SetActive(false); } }else{ foreach(Bone2D bone in s_InstanceBones) { bone.hideFlags = HideFlags.None; bone.gameObject.SetActive(true); } instance.transform.parent = null; instance.bones = s_InstanceBones; SpriteMeshUtils.UpdateRenderer(instance,false); } SceneView.RepaintAll(); } } break; case EventType.DragExited: if(instance) { GameObject.DestroyImmediate(instance.gameObject); Event.current.Use(); } Cleanup(); break; case EventType.DragPerform: if(!createOnEnter) { CreateInstance(); } if(instance) { if(currentDestination) { foreach(Bone2D bone in s_InstanceBones) { if(bone) { GameObject.DestroyImmediate(bone.gameObject); } } } Undo.RegisterCreatedObjectUndo(instance.gameObject,"create SpriteMeshInstance"); } Cleanup(); break; } if(instance) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } }
public static bool NeedsOverride(SpriteMesh spriteMesh) { if(!spriteMesh || !spriteMesh.sprite) return false; SpriteMeshData spriteMeshData = LoadSpriteMeshData(spriteMesh); if(!spriteMeshData) return false; ushort[] triangles = spriteMesh.sprite.triangles; if(triangles.Length != spriteMeshData.indices.Length) return true; for(int i = 0; i < triangles.Length; i++) { if(spriteMeshData.indices[i] != triangles[i]) { return true; } } return false; }
public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, GameObject gameObject, bool undo = true) { SpriteMeshInstance spriteMeshInstance = null; if(spriteMesh && gameObject) { if(undo) { spriteMeshInstance = Undo.AddComponent<SpriteMeshInstance>(gameObject); }else{ spriteMeshInstance = gameObject.AddComponent<SpriteMeshInstance>(); } spriteMeshInstance.spriteMesh = spriteMesh; spriteMeshInstance.sharedMaterial = defaultMaterial; SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh); List<Bone2D> bones = new List<Bone2D>(); List<string> paths = new List<string>(); Vector4 zero = new Vector4 (0f, 0f, 0f, 1f); foreach(BindInfo bindInfo in spriteMeshData.bindPoses) { Matrix4x4 m = spriteMeshInstance.transform.localToWorldMatrix * bindInfo.bindPose.inverse; GameObject bone = new GameObject(bindInfo.name); if(undo) { Undo.RegisterCreatedObjectUndo(bone,Undo.GetCurrentGroupName()); } Bone2D boneComponent = bone.AddComponent<Bone2D>(); boneComponent.localLength = bindInfo.boneLength; bone.transform.position = m * zero; bone.transform.rotation = m.GetRotation(); bone.transform.parent = gameObject.transform; bones.Add(boneComponent); paths.Add(bindInfo.path); } BoneUtils.ReconstructHierarchy(bones,paths); spriteMeshInstance.bones = bones; SpriteMeshUtils.UpdateRenderer(spriteMeshInstance, undo); EditorUtility.SetDirty(spriteMeshInstance); } return spriteMeshInstance; }
public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, bool undo = true) { if(spriteMesh) { GameObject gameObject = new GameObject(spriteMesh.name); if(undo) { Undo.RegisterCreatedObjectUndo(gameObject,Undo.GetCurrentGroupName()); } return CreateSpriteMeshInstance(spriteMesh, gameObject, undo); } return null; }
public static Rect CalculateSpriteRect(SpriteMesh spriteMesh, int padding) { int width = 0; int height = 0; GetSpriteTextureSize(spriteMesh.sprite,ref width,ref height); SpriteMeshData spriteMeshData = LoadSpriteMeshData(spriteMesh); Rect rect = spriteMesh.sprite.rect; if(spriteMeshData) { Vector2 min = new Vector2(float.MaxValue,float.MaxValue); Vector2 max = new Vector2(float.MinValue,float.MinValue); for (int i = 0; i < spriteMeshData.vertices.Length; i++) { Vector2 v = spriteMeshData.vertices[i]; if(v.x < min.x) min.x = v.x; if(v.y < min.y) min.y = v.y; if(v.x > max.x) max.x = v.x; if(v.y > max.y) max.y = v.y; } rect.position = min - Vector2.one * padding; rect.size = (max - min) + Vector2.one * padding * 2f; rect = MathUtils.ClampRect(rect,new Rect(0f,0f,width,height)); } return rect; }
public static SpriteMeshData LoadSpriteMeshData(SpriteMesh spriteMesh) { if(spriteMesh) { UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(spriteMesh)); foreach(UnityEngine.Object asset in assets) { SpriteMeshData data = asset as SpriteMeshData; if(data) { return data; } } } return null; }
static void SetBindPoses(SpriteMeshData spriteMeshData, SpriteMesh.BindInfo[] bindPoses) { if(bindPoses != null && bindPoses.Length > 0) { BindInfo[] array = new BindInfo[bindPoses.Length]; for(int i = 0; i < array.Length; ++i) { BindInfo b = new BindInfo(); b.bindPose = bindPoses[i].bindPose; b.boneLength = bindPoses[i].boneLength; b.color = bindPoses[i].color; b.name = bindPoses[i].name; b.path = bindPoses[i].path; b.zOrder = bindPoses[i].zOrder; array[i] = b; } FieldInfo fieldInfo = typeof(SpriteMeshData).GetField("m_BindPoses",BindingFlags.Instance | BindingFlags.NonPublic); if(fieldInfo != null) { fieldInfo.SetValue(spriteMeshData,array); } EditorUtility.SetDirty(spriteMeshData); } }
static SpriteMesh.BindInfo[] GetBindPoses(SpriteMesh spriteMesh) { SpriteMesh.BindInfo[] result = null; FieldInfo fieldInfo = typeof(SpriteMesh).GetField("bindPoses",BindingFlags.Instance | BindingFlags.NonPublic); if(fieldInfo != null) { result = (SpriteMesh.BindInfo[])fieldInfo.GetValue(spriteMesh); } return result; }
public static void UpdateAssets(SpriteMesh spriteMesh, SpriteMeshData spriteMeshData) { if(spriteMesh && spriteMeshData) { string spriteMeshPath = AssetDatabase.GetAssetPath(spriteMesh); SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); SerializedProperty sharedMeshProp = spriteMeshSO.FindProperty("m_SharedMesh"); SerializedProperty sharedMaterialsProp = spriteMeshSO.FindProperty("m_SharedMaterials"); if(!spriteMesh.sharedMesh) { Mesh mesh = new Mesh(); mesh.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(mesh,spriteMeshPath); spriteMeshSO.Update(); sharedMeshProp.objectReferenceValue = mesh; spriteMeshSO.ApplyModifiedProperties(); EditorUtility.SetDirty(mesh); } spriteMesh.sharedMesh.name = spriteMesh.name; if(spriteMesh.sharedMaterials.Length == 0) { Material material = new Material(Shader.Find("Sprites/Default")); material.mainTexture = SpriteUtility.GetSpriteTexture(spriteMesh.sprite,false); AssetDatabase.AddObjectToAsset(material,spriteMeshPath); spriteMeshSO.Update(); sharedMaterialsProp.arraySize = 1; sharedMaterialsProp.GetArrayElementAtIndex(0).objectReferenceValue = material; spriteMeshSO.ApplyModifiedProperties(); } for (int i = 0; i < spriteMesh.sharedMaterials.Length; i++) { Material material = spriteMesh.sharedMaterials [i]; if(material) { if(spriteMesh.sprite) { material.mainTexture = SpriteUtility.GetSpriteTexture(spriteMesh.sprite,false); } material.name = spriteMesh.name + "_" + i; material.hideFlags = HideFlags.HideInHierarchy; EditorUtility.SetDirty(material); } } spriteMeshData.hideFlags = HideFlags.HideInHierarchy; EditorUtility.SetDirty(spriteMeshData); int width = 0; int height = 0; GetSpriteTextureSize(spriteMesh.sprite,ref width,ref height); float pixelsPerUnit = GetSpritePixelsPerUnit(spriteMesh.sprite); Vector2 textureWidthHeightInv = new Vector2(1f/width,1f/height); Vector3[] vertices = (new List<Vector2>(spriteMeshData.vertices)).ConvertAll( v => TexCoordToVertex(spriteMeshData.pivotPoint,v,pixelsPerUnit)).ToArray(); Vector2[] uvs = (new List<Vector2>(spriteMeshData.vertices)).ConvertAll( v => Vector2.Scale(v,textureWidthHeightInv)).ToArray(); Vector3[] normals = (new List<Vector3>(vertices)).ConvertAll( v => Vector3.back ).ToArray(); BoneWeight[] boneWeightsData = spriteMeshData.boneWeights; if(boneWeightsData.Length != spriteMeshData.vertices.Length) { boneWeightsData = new BoneWeight[spriteMeshData.vertices.Length]; } List<UnityEngine.BoneWeight> boneWeights = new List<UnityEngine.BoneWeight>(boneWeightsData.Length); List<float> verticesOrder = new List<float>(spriteMeshData.vertices.Length); for (int i = 0; i < boneWeightsData.Length; i++) { BoneWeight boneWeight = boneWeightsData[i]; List< KeyValuePair<int,float> > pairs = new List<KeyValuePair<int, float>>(); pairs.Add(new KeyValuePair<int, float>(boneWeight.boneIndex0,boneWeight.weight0)); pairs.Add(new KeyValuePair<int, float>(boneWeight.boneIndex1,boneWeight.weight1)); pairs.Add(new KeyValuePair<int, float>(boneWeight.boneIndex2,boneWeight.weight2)); pairs.Add(new KeyValuePair<int, float>(boneWeight.boneIndex3,boneWeight.weight3)); pairs = pairs.OrderByDescending(s=>s.Value).ToList(); UnityEngine.BoneWeight boneWeight2 = new UnityEngine.BoneWeight(); boneWeight2.boneIndex0 = Mathf.Max(0,pairs[0].Key); boneWeight2.boneIndex1 = Mathf.Max(0,pairs[1].Key); boneWeight2.boneIndex2 = Mathf.Max(0,pairs[2].Key); boneWeight2.boneIndex3 = Mathf.Max(0,pairs[3].Key); boneWeight2.weight0 = pairs[0].Value; boneWeight2.weight1 = pairs[1].Value; boneWeight2.weight2 = pairs[2].Value; boneWeight2.weight3 = pairs[3].Value; boneWeights.Add(boneWeight2); float vertexOrder = i; if(spriteMeshData.bindPoses.Length > 0) { vertexOrder = spriteMeshData.bindPoses[boneWeight2.boneIndex0].zOrder * boneWeight2.weight0 + spriteMeshData.bindPoses[boneWeight2.boneIndex1].zOrder * boneWeight2.weight1 + spriteMeshData.bindPoses[boneWeight2.boneIndex2].zOrder * boneWeight2.weight2 + spriteMeshData.bindPoses[boneWeight2.boneIndex3].zOrder * boneWeight2.weight3; } verticesOrder.Add(vertexOrder); } List<WeightedTriangle> weightedTriangles = new List<WeightedTriangle>(spriteMeshData.indices.Length / 3); for(int i = 0; i < spriteMeshData.indices.Length; i+=3) { int p1 = spriteMeshData.indices[i]; int p2 = spriteMeshData.indices[i+1]; int p3 = spriteMeshData.indices[i+2]; weightedTriangles.Add(new WeightedTriangle(p1,p2,p3, verticesOrder[p1], verticesOrder[p2], verticesOrder[p3])); } weightedTriangles = weightedTriangles.OrderBy( t => t.weight ).ToList(); List<int> indices = new List<int>(spriteMeshData.indices.Length); for(int i = 0; i < weightedTriangles.Count; ++i) { WeightedTriangle t = weightedTriangles[i]; indices.Add(t.p1); indices.Add(t.p2); indices.Add(t.p3); } spriteMesh.sharedMesh.Clear(); spriteMesh.sharedMesh.vertices = vertices; spriteMesh.sharedMesh.uv = uvs; spriteMesh.sharedMesh.triangles = indices.ToArray(); spriteMesh.sharedMesh.normals = normals; spriteMesh.sharedMesh.boneWeights = boneWeights.ToArray(); spriteMesh.sharedMesh.bindposes = (new List<BindInfo>(spriteMeshData.bindPoses)).ConvertAll( p => p.bindPose ).ToArray(); spriteMesh.sharedMesh.RecalculateBounds(); } }
public static Vector2 VertexToTexCoord(SpriteMesh spriteMesh, Vector2 pivotPoint, Vector3 vertex, float pixelsPerUnit) { Vector2 texCoord = Vector3.zero; if(spriteMesh != null) { texCoord = (Vector2)vertex * pixelsPerUnit + pivotPoint; } return texCoord; }
public static void UpdateAssets(SpriteMesh spriteMesh) { UpdateAssets(spriteMesh, SpriteMeshUtils.LoadSpriteMeshData(spriteMesh)); }
static void UpgradeSpriteMesh(SpriteMesh spriteMesh) { if(spriteMesh) { SerializedObject spriteMeshSO = new SerializedObject(spriteMesh); SerializedProperty apiVersionProp = spriteMeshSO.FindProperty("m_ApiVersion"); if(apiVersionProp.intValue < SpriteMesh.api_version) { if(apiVersionProp.intValue < 1) { Upgrade_001(spriteMeshSO); } if(apiVersionProp.intValue < 2) { Upgrade_002(spriteMeshSO); } if(apiVersionProp.intValue < 3) { Upgrade_003(spriteMeshSO); } spriteMeshSO.Update(); apiVersionProp.intValue = SpriteMesh.api_version; spriteMeshSO.ApplyModifiedProperties(); AssetDatabase.SaveAssets(); } } }