void RefreshRects() { m_RectsCache = null; if (IsSpriteDataProviderValid()) { m_RectsCache = m_SpriteDataProvider.GetSpriteRects().ToList(); } InitSelectedSpriteRect(); }
public override bool ApplyRevert(bool apply) { if (apply) { if (containsMultipleSprites) { var oldNames = new List <string>(); var newNames = new List <string>(); for (int i = 0; i < m_RectsCache.spriteRects.Count; i++) { SpriteRect spriteRect = (SpriteRect)m_RectsCache.spriteRects[i]; if (string.IsNullOrEmpty(spriteRect.name)) { spriteRect.name = "Empty"; } if (!string.IsNullOrEmpty(spriteRect.originalName)) { oldNames.Add(spriteRect.originalName); newNames.Add(spriteRect.name); } } var so = new SerializedObject(m_SpriteDataProvider.targetObject); if (oldNames.Count > 0) { PatchImportSettingRecycleID.PatchMultiple(so, 213, oldNames.ToArray(), newNames.ToArray()); } so.ApplyModifiedPropertiesWithoutUndo(); } m_SpriteDataProvider.SetSpriteRects(m_RectsCache?.spriteRects.ToArray()); if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); } } else { if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); m_RectsCache.spriteRects = m_SpriteDataProvider.GetSpriteRects().ToList(); spriteEditor.spriteRects = m_RectsCache.spriteRects; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } } } return(true); }
private void UnifiedValues(out bool name, out bool alignment, out bool border) { name = true; alignment = true; border = true; if (base.targets.Length >= 2) { string assetPath = AssetDatabase.GetAssetPath(this.sprite); ISpriteEditorDataProvider spriteEditorDataProvider = AssetImporter.GetAtPath(assetPath) as ISpriteEditorDataProvider; if (spriteEditorDataProvider != null) { spriteEditorDataProvider.InitSpriteEditorDataProvider(); SpriteRect[] spriteRects = spriteEditorDataProvider.GetSpriteRects(); string text = null; int num = -1; Vector4? vector = null; for (int i = 0; i < base.targets.Length; i++) { Sprite sprite = base.targets[i] as Sprite; for (int j = 0; j < spriteRects.Length; j++) { if (spriteRects[j].name.Equals(sprite.name)) { if (spriteRects[j].alignment != (SpriteAlignment)num && num > 0) { alignment = false; } else { num = (int)spriteRects[j].alignment; } if (spriteRects[j].name != text && text != null) { name = false; } else { text = spriteRects[j].name; } if (spriteRects[j].border != vector && vector.HasValue) { border = false; } else { vector = new Vector4?(spriteRects[j].border); } } } } } } }
protected virtual void LoadOutline() { this.m_Outline = ScriptableObject.CreateInstance <SpriteOutlineModel>(); ISpriteEditorDataProvider dataProvider = this.spriteEditorWindow.GetDataProvider <ISpriteEditorDataProvider>(); ISpriteOutlineDataProvider dataProvider2 = this.spriteEditorWindow.GetDataProvider <ISpriteOutlineDataProvider>(); SpriteRect[] spriteRects = dataProvider.GetSpriteRects(); for (int i = 0; i < spriteRects.Length; i++) { SpriteRect spriteRect = spriteRects[i]; List <Vector2[]> outlines = dataProvider2.GetOutlines(spriteRect.spriteID); this.m_Outline.AddListVector2(spriteRect.spriteID, outlines); this.m_Outline[this.m_Outline.Count - 1].tessellationDetail = dataProvider2.GetTessellationDetail(spriteRect.spriteID); } }
static bool PostProcessBoneData(ISpriteEditorDataProvider spriteDataProvider, float definitionScale, Sprite[] sprites) { var boneDataProvider = spriteDataProvider.GetDataProvider <ISpriteBoneDataProvider>(); var textureDataProvider = spriteDataProvider.GetDataProvider <ITextureDataProvider>(); if (sprites == null || sprites.Length == 0 || boneDataProvider == null || textureDataProvider == null) { return(false); } bool dataChanged = false; var spriteRects = spriteDataProvider.GetSpriteRects(); foreach (var sprite in sprites) { var guid = sprite.GetSpriteID(); { var spriteBone = boneDataProvider.GetBones(guid); if (spriteBone == null) { continue; } var spriteBoneCount = spriteBone.Count; if (spriteBoneCount == 0) { continue; } var spriteRect = spriteRects.First(s => { return(s.spriteID == guid); }); var bindPose = new NativeArray <Matrix4x4>(spriteBoneCount, Allocator.Temp); var outputSpriteBones = new UnityEngine.U2D.SpriteBone ? [spriteBoneCount]; for (int i = 0; i < spriteBoneCount; ++i) { CalculateLocaltoWorldMatrix(i, spriteRect, definitionScale, sprite.pixelsPerUnit, spriteBone, ref outputSpriteBones, ref bindPose); } sprite.SetBindPoses(bindPose); sprite.SetBones(outputSpriteBones.Select(x => x.Value).ToArray()); bindPose.Dispose(); dataChanged = true; } } return(dataChanged); }
// implements ISpriteEditorModule public override void OnModuleActivate() { spriteImportMode = SpriteUtility.GetSpriteImportMode(spriteEditor.GetDataProvider <ISpriteEditorDataProvider>()); m_TextureDataProvider = spriteEditor.GetDataProvider <ITextureDataProvider>(); m_SpriteDataProvider = spriteEditor.GetDataProvider <ISpriteEditorDataProvider>(); int width, height; m_TextureDataProvider.GetTextureActualWidthAndHeight(out width, out height); textureActualWidth = width; textureActualHeight = height; m_RectsCache = ScriptableObject.CreateInstance <SpriteRectModel>(); m_RectsCache.spriteRects = m_SpriteDataProvider.GetSpriteRects().ToList(); spriteEditor.spriteRects = m_RectsCache.spriteRects; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } }
// implements ISpriteEditorModule public override void OnModuleActivate() { spriteImportMode = SpriteFrameModule.GetSpriteImportMode(spriteEditor.GetDataProvider <ISpriteEditorDataProvider>()); m_TextureDataProvider = spriteEditor.GetDataProvider <ITextureDataProvider>(); m_SpriteDataProvider = spriteEditor.GetDataProvider <ISpriteEditorDataProvider>(); int width, height; m_TextureDataProvider.GetTextureActualWidthAndHeight(out width, out height); textureActualWidth = width; textureActualHeight = height; m_RectsCache = ScriptableObject.CreateInstance <SpriteRectModel>(); m_RectsCache.hideFlags = HideFlags.HideAndDontSave; m_RectsCache.spriteRects = m_SpriteDataProvider.GetSpriteRects().ToList(); spriteEditor.spriteRects = m_RectsCache.spriteRects; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } AddMainUI(spriteEditor.GetMainVisualContainer()); undoSystem.RegisterUndoCallback(UndoCallback); }
static bool PostProcessSpriteMeshData(ISpriteEditorDataProvider spriteDataProvider, Sprite[] sprites) { var spriteMeshDataProvider = spriteDataProvider.GetDataProvider <ISpriteMeshDataProvider>(); var boneDataProvider = spriteDataProvider.GetDataProvider <ISpriteBoneDataProvider>(); var textureDataProvider = spriteDataProvider.GetDataProvider <ITextureDataProvider>(); if (sprites == null || sprites.Length == 0 || spriteMeshDataProvider == null || textureDataProvider == null) { return(false); } bool dataChanged = false; float definitionScale = CalculateDefinitionScale(textureDataProvider); foreach (var sprite in sprites) { var guid = sprite.GetSpriteID(); var spriteRect = spriteDataProvider.GetSpriteRects().First(s => { return(s.spriteID == guid); }); var spriteBone = boneDataProvider.GetBones(guid); var hasBones = spriteBone != null && spriteBone.Count > 0; var hasInvalidWeights = false; var vertices = spriteMeshDataProvider.GetVertices(guid); var indices = spriteMeshDataProvider.GetIndices(guid); if (vertices.Length > 2 && indices.Length > 2) { var vertexArray = new NativeArray <Vector3>(vertices.Length, Allocator.Temp); var boneWeightArray = new NativeArray <BoneWeight>(vertices.Length, Allocator.Temp); for (int i = 0; i < vertices.Length; ++i) { var boneWeight = vertices[i].boneWeight; vertexArray[i] = (Vector3)(vertices[i].position - Vector2.Scale(spriteRect.rect.size, spriteRect.pivot)) * definitionScale / sprite.pixelsPerUnit; boneWeightArray[i] = boneWeight; if (hasBones && !hasInvalidWeights) { var sum = boneWeight.weight0 + boneWeight.weight1 + boneWeight.weight2 + boneWeight.weight3; hasInvalidWeights = sum < 0.999f; } } var indicesArray = new NativeArray <ushort>(indices.Length, Allocator.Temp); for (int i = 0; i < indices.Length; ++i) { indicesArray[i] = (ushort)indices[i]; } sprite.SetVertexCount(vertices.Length); sprite.SetVertexAttribute <Vector3>(VertexAttribute.Position, vertexArray); sprite.SetIndices(indicesArray); sprite.SetBoneWeights(boneWeightArray); vertexArray.Dispose(); boneWeightArray.Dispose(); indicesArray.Dispose(); dataChanged = true; if (hasBones && hasInvalidWeights) { Debug.LogWarning("Sprite \"" + spriteRect.name + "\" contains bone weights which sum zero or are not normalized. To avoid visual artifacts please consider fixing them."); } } else { var boneWeightArray = new NativeArray <BoneWeight>(sprite.GetVertexCount(), Allocator.Temp); var defaultBoneWeight = new BoneWeight() { weight0 = 1f }; for (var i = 0; i < boneWeightArray.Length; ++i) { boneWeightArray[i] = defaultBoneWeight; } sprite.SetBoneWeights(boneWeightArray); } } return(dataChanged); }
public override bool ApplyRevert(bool apply) { if (apply) { var array = m_RectsCache != null?m_RectsCache.spriteRects.ToArray() : null; m_SpriteDataProvider.SetSpriteRects(array); var spriteNames = m_RectsCache?.spriteNames; var spriteFileIds = m_RectsCache?.spriteFileIds; if (spriteNames != null && spriteFileIds != null) { var pairList = new List <SpriteNameFileIdPair>(spriteNames.Count); for (var i = 0; i < spriteNames.Count; ++i) { pairList.Add(new SpriteNameFileIdPair(spriteNames[i], spriteFileIds[i])); } m_NameFileIdDataProvider.SetNameFileIdPairs(pairList.ToArray()); } var outlineDataProvider = m_SpriteDataProvider.GetDataProvider <ISpriteOutlineDataProvider>(); var physicsDataProvider = m_SpriteDataProvider.GetDataProvider <ISpritePhysicsOutlineDataProvider>(); foreach (var rect in array) { if (rect is OutlineSpriteRect outlineRect) { if (outlineRect.outlines.Count > 0) { outlineDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines); physicsDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines); } } } if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); } } else { if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); var spriteList = m_SpriteDataProvider.GetSpriteRects().ToList(); m_RectsCache.SetSpriteRects(spriteList); var nameFileIdPairs = m_NameFileIdDataProvider.GetNameFileIdPairs(); m_RectsCache.SetNameFileIdPairs(nameFileIdPairs); spriteEditor.spriteRects = spriteList; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } } } return(true); }
public override bool ApplyRevert(bool apply) { if (apply) { if (containsMultipleSprites) { var oldNames = new List <string>(); var newNames = new List <string>(); for (int i = 0; i < m_RectsCache.spriteRects.Count; i++) { SpriteRect spriteRect = (SpriteRect)m_RectsCache.spriteRects[i]; if (string.IsNullOrEmpty(spriteRect.name)) { spriteRect.name = "Empty"; } if (!string.IsNullOrEmpty(spriteRect.originalName)) { oldNames.Add(spriteRect.originalName); newNames.Add(spriteRect.name); } } var so = new SerializedObject(m_SpriteDataProvider.targetObject); if (oldNames.Count > 0) { AssetImporter assetImporter = m_SpriteDataProvider.targetObject as AssetImporter; if (assetImporter != null) { UnityType spriteType = UnityType.FindTypeByName("Sprite"); assetImporter.RenameSubAssets(spriteType.persistentTypeID, oldNames.ToArray(), newNames.ToArray()); } } so.ApplyModifiedPropertiesWithoutUndo(); } var array = m_RectsCache != null?m_RectsCache.spriteRects.ToArray() : null; m_SpriteDataProvider.SetSpriteRects(array); if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); } } else { if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); m_RectsCache.spriteRects = m_SpriteDataProvider.GetSpriteRects().ToList(); spriteEditor.spriteRects = m_RectsCache.spriteRects; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } } } return(true); }
public override bool ApplyRevert(bool apply) { if (apply) { if (containsMultipleSprites) { var oldNames = new List <string>(); var newNames = new List <string>(); var ids = new List <long>(); var names = new List <string>(); foreach (var spriteRect in m_RectsCache.spriteRects) { if (string.IsNullOrEmpty(spriteRect.name)) { spriteRect.name = "Empty"; } if (!string.IsNullOrEmpty(spriteRect.originalName)) { oldNames.Add(spriteRect.originalName); newNames.Add(spriteRect.name); } if (spriteRect.m_RegisterInternalID) { ids.Add(spriteRect.internalID); names.Add(spriteRect.name); } spriteRect.m_RegisterInternalID = false; } var so = new SerializedObject(m_SpriteDataProvider.targetObject); if (so.isValid && ids.Count > 0) { ImportSettingInternalID.RegisterInternalID(so, spriteType, ids, names); so.ApplyModifiedPropertiesWithoutUndo(); } AssetImporter assetImporter = m_SpriteDataProvider.targetObject as AssetImporter; if (oldNames.Count > 0 && assetImporter != null) { assetImporter.RenameSubAssets(spriteType.persistentTypeID, oldNames.ToArray(), newNames.ToArray()); so.ApplyModifiedPropertiesWithoutUndo(); } } var array = m_RectsCache != null?m_RectsCache.spriteRects.ToArray() : null; m_SpriteDataProvider.SetSpriteRects(array); var outlineDataProvider = m_SpriteDataProvider.GetDataProvider <ISpriteOutlineDataProvider>(); var physicsDataProvider = m_SpriteDataProvider.GetDataProvider <ISpritePhysicsOutlineDataProvider>(); foreach (var rect in array) { if (rect is OutlineSpriteRect outlineRect) { if (outlineRect.outlines.Count > 0) { outlineDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines); physicsDataProvider.SetOutlines(outlineRect.spriteID, outlineRect.outlines); } } } if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); } } else { if (m_RectsCache != null) { undoSystem.ClearUndo(m_RectsCache); var spriteList = m_SpriteDataProvider.GetSpriteRects().ToList(); m_RectsCache.SetSpriteRects(spriteList); spriteEditor.spriteRects = spriteList; if (spriteEditor.selectedSpriteRect != null) { spriteEditor.selectedSpriteRect = m_RectsCache.spriteRects.FirstOrDefault(x => x.spriteID == spriteEditor.selectedSpriteRect.spriteID); } } } return(true); }
static bool PostProcessBoneData(ISpriteEditorDataProvider spriteDataProvider, Sprite[] sprites) { var boneDataProvider = spriteDataProvider.GetDataProvider <ISpriteBoneDataProvider>(); var textureDataProvider = spriteDataProvider.GetDataProvider <ITextureDataProvider>(); if (sprites == null || sprites.Length == 0 || boneDataProvider == null || textureDataProvider == null) { return(false); } bool dataChanged = false; float definitionScale = CalculateDefinitionScale(textureDataProvider); foreach (var sprite in sprites) { var guid = sprite.GetSpriteID(); { SpriteRect spriteRect = spriteDataProvider.GetSpriteRects().First(s => { return(s.spriteID == guid); }); var spriteBone = boneDataProvider.GetBones(guid); if (spriteBone == null) { continue; } var spriteBoneCount = spriteBone.Count; var pivotPointInPixels = sprite.pivot; var bindPose = new NativeArray <Matrix4x4>(spriteBoneCount, Allocator.Temp); var outputSpriteBones = new SpriteBone[spriteBoneCount];// NativeArray<SpriteBone>(spriteBone.Count, Allocator.Temp); for (int i = 0; i < spriteBoneCount; ++i) { // Convert position to world unit. SpriteBone sp = spriteBone[i]; var isRoot = sp.parentId == -1; var position = isRoot ? (spriteBone[i].position - Vector3.Scale(spriteRect.rect.size, spriteRect.pivot)) : spriteBone[i].position; position.z = 0f; sp.position = position * definitionScale / sprite.pixelsPerUnit; sp.length = spriteBone[i].length * definitionScale / sprite.pixelsPerUnit; outputSpriteBones[i] = sp; // Calculate bind poses var worldPosition = Vector3.zero; var worldRotation = Quaternion.identity; if (sp.parentId == -1) { worldPosition = sp.position; worldRotation = sp.rotation; } else { var parentBindPose = bindPose[sp.parentId]; var invParentBindPose = Matrix4x4.Inverse(parentBindPose); worldPosition = invParentBindPose.MultiplyPoint(sp.position); worldRotation = sp.rotation * invParentBindPose.rotation; } // Practically Matrix4x4.SetTRInverse var rot = Quaternion.Inverse(worldRotation); Matrix4x4 mat = Matrix4x4.identity; mat = Matrix4x4.Rotate(rot); mat = mat * Matrix4x4.Translate(-worldPosition); bindPose[i] = mat; } sprite.SetBindPoses(bindPose); sprite.SetBones(outputSpriteBones); bindPose.Dispose(); dataChanged = true; } } return(dataChanged); }