/// <summary> /// 换装备并且合并材质球 /// </summary> /// <param name="skeletonAnimation">spine 组件</param> /// <param name="atlasAsset"> 图片+atlas 生成的图集文件 </param> /// <param name="regionName"> 图集文件中的具体的一张图片 最好一个图片一个图集 </param> /// <param name="e"> 装备类型 </param> public void ExchangeEquipmentAndMerge(SkeletonAnimation skeletonAnimation, List <EquipmentItem> infos) { for (int i = 0; i < infos.Count; i++) { EquipmentItem info = infos[i]; string regionName = info.equipmentImageName; string defaultSkinName = info.defaultSkinName; string spineEquipmentType = info.spineEquipmentTypeName; AssetBundlePackage assetbundle = HFResourceManager.Instance.LoadAssetBundleFromFile(info.equipmentAssetbundleName); AtlasAsset atlasAsset = assetbundle.LoadAssetWithCache <AtlasAsset>(info.equipmentAtlasAssetName); float scale = skeletonAnimation.skeletonDataAsset.scale; Atlas atlas = atlasAsset.GetAtlas(); AtlasRegion region = atlas.FindRegion(regionName); Slot slot = skeletonAnimation.skeleton.FindSlot(info.slotName); Attachment originalAttachment = slot.Attachment; if (region == null) { slot.Attachment = null; } else if (originalAttachment != null) { slot.Attachment = originalAttachment.GetRemappedClone(region, true, true, 1); } else { var newRegionAttachment = region.ToRegionAttachment(region.name, scale); slot.Attachment = newRegionAttachment; } slot.Skeleton.Skin.SetAttachment(slot.Data.Index, info.slotPlaceholderName, slot.Attachment); } Skin repackedSkin = new Skin(RepackConst); repackedSkin.Append(skeletonAnimation.Skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders) repackedSkin.Append(skeletonAnimation.Skeleton.Skin); // Include your new custom skin. Texture2D runtimeAtlas = null; Material runtimeMaterial = null; repackedSkin = repackedSkin.GetRepackedSkin(RepackConst, skeletonAnimation.SkeletonDataAsset.atlasAssets[0].materials[0], out runtimeMaterial, out runtimeAtlas); // Pack all the items in the skin. skeletonAnimation.Skeleton.SetSkin(repackedSkin); // Assign the repacked skin to your Skeleton. skeletonAnimation.skeleton.SetSlotsToSetupPose(); // Use the pose from setup pose. skeletonAnimation.Update(0); // Use the pose in the currently active animation. }
void Apply(SkeletonRenderer skeletonRenderer) { atlas = atlasAsset.GetAtlas(); AtlasAttachmentLoader loader = new AtlasAttachmentLoader(atlas); float scaleMultiplier = skeletonRenderer.skeletonDataAsset.scale; var enumerator = attachments.GetEnumerator(); while (enumerator.MoveNext()) { var entry = (SlotRegionPair)enumerator.Current; var regionAttachment = loader.NewRegionAttachment(null, entry.region, entry.region); regionAttachment.Width = regionAttachment.RegionOriginalWidth * scaleMultiplier; regionAttachment.Height = regionAttachment.RegionOriginalHeight * scaleMultiplier; regionAttachment.SetColor(new Color(1, 1, 1, 1)); regionAttachment.UpdateOffset(); var slot = skeletonRenderer.skeleton.FindSlot(entry.slot); slot.Attachment = regionAttachment; } }
static AtlasAsset IngestSpineAtlas(TextAsset atlasText) { if (atlasText == null) { Debug.LogWarning("Atlas source cannot be null!"); return(null); } string primaryName = Path.GetFileNameWithoutExtension(atlasText.name).Replace(".atlas", ""); string assetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(atlasText)); string atlasPath = assetPath + "/" + primaryName + "_Atlas.asset"; AtlasAsset atlasAsset = (AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset)); List <Material> vestigialMaterials = new List <Material>(); if (atlasAsset == null) { atlasAsset = AtlasAsset.CreateInstance <AtlasAsset>(); } else { foreach (Material m in atlasAsset.materials) { vestigialMaterials.Add(m); } } atlasAsset.atlasFile = atlasText; //strip CR string atlasStr = atlasText.text; atlasStr = atlasStr.Replace("\r", ""); string[] atlasLines = atlasStr.Split('\n'); List <string> pageFiles = new List <string>(); for (int i = 0; i < atlasLines.Length - 1; i++) { if (atlasLines[i].Length == 0) { pageFiles.Add(atlasLines[i + 1]); } } atlasAsset.materials = new Material[pageFiles.Count]; for (int i = 0; i < pageFiles.Count; i++) { string texturePath = assetPath + "/" + pageFiles[i]; Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)); TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath); texImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; texImporter.mipmapEnabled = false; texImporter.alphaIsTransparency = false; texImporter.maxTextureSize = 2048; EditorUtility.SetDirty(texImporter); AssetDatabase.ImportAsset(texturePath); AssetDatabase.SaveAssets(); string pageName = Path.GetFileNameWithoutExtension(pageFiles[i]); //because this looks silly if (pageName == primaryName && pageFiles.Count == 1) { pageName = "Material"; } string materialPath = assetPath + "/" + primaryName + "_" + pageName + ".mat"; Material mat = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); if (mat == null) { mat = new Material(Shader.Find(defaultShader)); AssetDatabase.CreateAsset(mat, materialPath); } else { vestigialMaterials.Remove(mat); } mat.mainTexture = texture; EditorUtility.SetDirty(mat); AssetDatabase.SaveAssets(); atlasAsset.materials[i] = mat; } for (int i = 0; i < vestigialMaterials.Count; i++) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(vestigialMaterials[i])); } if (AssetDatabase.GetAssetPath(atlasAsset) == "") { AssetDatabase.CreateAsset(atlasAsset, atlasPath); } else { atlasAsset.Reset(); } EditorUtility.SetDirty(atlasAsset); AssetDatabase.SaveAssets(); //iterate regions and bake marked Atlas atlas = atlasAsset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas); string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); bool hasBakedRegions = false; for (int i = 0; i < regions.Count; i++) { AtlasRegion region = regions[i]; string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/"); GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject)); if (prefab != null) { BakeRegion(atlasAsset, region, false); hasBakedRegions = true; } } if (hasBakedRegions) { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return((AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset))); }
static List <AtlasAsset> MultiAtlasDialog(List <string> requiredPaths, string initialDirectory, string header = "") { List <AtlasAsset> atlasAssets = new List <AtlasAsset>(); bool resolved = false; string lastAtlasPath = initialDirectory; while (!resolved) { StringBuilder sb = new StringBuilder(); sb.AppendLine(header); sb.AppendLine("Atlases:"); if (atlasAssets.Count == 0) { sb.AppendLine("\t--none--"); } for (int i = 0; i < atlasAssets.Count; i++) { sb.AppendLine("\t" + atlasAssets[i].name); } sb.AppendLine(); sb.AppendLine("Missing Regions:"); List <string> missingRegions = new List <string>(requiredPaths); foreach (var atlasAsset in atlasAssets) { var atlas = atlasAsset.GetAtlas(); for (int i = 0; i < missingRegions.Count; i++) { if (atlas.FindRegion(missingRegions[i]) != null) { missingRegions.RemoveAt(i); i--; } } } if (missingRegions.Count == 0) { break; } for (int i = 0; i < missingRegions.Count; i++) { sb.AppendLine("\t" + missingRegions[i]); } int result = EditorUtility.DisplayDialogComplex("Atlas Selection", sb.ToString(), "Select", "Finish", "Abort"); switch (result) { case 0: AtlasAsset selectedAtlasAsset = GetAtlasDialog(lastAtlasPath); if (selectedAtlasAsset != null) { var atlas = selectedAtlasAsset.GetAtlas(); bool hasValidRegion = false; foreach (string str in missingRegions) { if (atlas.FindRegion(str) != null) { hasValidRegion = true; break; } } atlasAssets.Add(selectedAtlasAsset); } break; case 1: resolved = true; break; case 2: atlasAssets = null; resolved = true; break; } } return(atlasAssets); }
public static GameObject BakeRegion (AtlasAsset atlasAsset, AtlasRegion region, bool autoSave = true) { Atlas atlas = atlasAsset.GetAtlas(); string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/"); GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject)); GameObject root; Mesh mesh; bool isNewPrefab = false; if (!Directory.Exists(bakedDirPath)) Directory.CreateDirectory(bakedDirPath); if (prefab == null) { root = new GameObject("temp", typeof(MeshFilter), typeof(MeshRenderer)); prefab = (GameObject)PrefabUtility.CreatePrefab(bakedPrefabPath, root); isNewPrefab = true; Object.DestroyImmediate(root); } mesh = (Mesh)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(Mesh)); Material mat = null; mesh = atlasAsset.GenerateMesh(region.name, mesh, out mat); if (isNewPrefab) { AssetDatabase.AddObjectToAsset(mesh, prefab); prefab.GetComponent<MeshFilter>().sharedMesh = mesh; } EditorUtility.SetDirty(mesh); EditorUtility.SetDirty(prefab); if (autoSave) { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } prefab.GetComponent<MeshRenderer>().sharedMaterial = mat; return prefab; }
public SkeletonData GetSkeletonData(bool quiet) { if (atlasAsset == null) { if (!quiet) { Debug.LogError("Atlas not set for SkeletonData asset: " + name, this); } Reset(); return(null); } if (skeletonJSON == null && skeletonBinary == null) { if (!quiet) { Debug.LogError("Skeleton file not set for SkeletonData asset: " + name, this); } Reset(); return(null); } Atlas atlas = atlasAsset.GetAtlas(); if (atlas == null) { Reset(); return(null); } if (skeletonData != null) { return(skeletonData); } if (skeletonBinary != null) { SkeletonBin binary = new SkeletonBin(atlas); try { skeletonData = binary.LoadSkeletonBinary(skeletonBinary.bytes); }catch (Exception ex) { Debug.LogError(name + "\n" + ex.Message + "\n" + ex.StackTrace, this); return(null); } } else { SkeletonJson json = new SkeletonJson(atlas); json.Scale = scale; try { skeletonData = json.ReadSkeletonData(new StringReader(skeletonJSON.text)); } catch (Exception ex) { if (!quiet) { Debug.LogError("Error reading skeleton JSON file for SkeletonData asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this); } return(null); } } stateData = new AnimationStateData(skeletonData); stateData.DefaultMix = defaultMix; for (int i = 0, n = fromAnimation.Length; i < n; i++) { if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0) { continue; } stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]); } return(skeletonData); }
override public void OnInspectorGUI() { serializedObject.Update(); atlasAsset = atlasAsset ?? (AtlasAsset)target; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(materials, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); atlasAsset.Clear(); atlasAsset.GetAtlas(); } if (materials.arraySize == 0) { EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); return; } for (int i = 0; i < materials.arraySize; i++) { SerializedProperty prop = materials.GetArrayElementAtIndex(i); Material mat = (Material)prop.objectReferenceValue; if (mat == null) { EditorGUILayout.LabelField(new GUIContent("Error: Materials cannot be null", SpineEditorUtilities.Icons.warning)); return; } } EditorGUILayout.Space(); if (atlasFile.objectReferenceValue != null) { if (SpineInspectorUtility.LargeCenteredButton(SpriteSlicesLabel)) { var atlas = atlasAsset.GetAtlas(); foreach (var m in atlasAsset.materials) { UpdateSpriteSlices(m.mainTexture, atlas); } } } #if REGION_BAKING_MESH if (atlasFile.objectReferenceValue != null) { Atlas atlas = asset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas); EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon)); EditorGUI.indentLevel++; AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { GUILayout.BeginHorizontal(); { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.EndDisabledGroup(); } GUILayout.EndHorizontal(); } else { EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning)); } } GUILayout.BeginHorizontal(); { //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]); bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft(" " + regions[i].name, baked[i]); if (baked[i]) { EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250)); } if (result && !baked[i]) { //bake baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); EditorGUIUtility.PingObject(bakedObjects[i]); } else if (!result && baked[i]) { //unbake bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel"); switch (unbakeResult) { case true: //delete string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; break; case false: //do nothing break; } } } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; #if BAKE_ALL_BUTTON // Check state bool allBaked = true; bool allUnbaked = true; for (int i = 0; i < regions.Count; i++) { allBaked &= baked[i]; allUnbaked &= !baked[i]; } if (!allBaked && GUILayout.Button("Bake All")) { for (int i = 0; i < regions.Count; i++) { if (!baked[i]) { baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); } } } else if (!allUnbaked && GUILayout.Button("Unbake All")) { bool unbakeResult = EditorUtility.DisplayDialog("Delete All Baked Regions", "Are you sure you want to unbake all region prefabs? This cannot be undone.", "Yes", "Cancel"); switch (unbakeResult) { case true: //delete for (int i = 0; i < regions.Count; i++) { if (baked[i]) { string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; } } break; case false: //do nothing break; } } #endif } #else if (atlasFile.objectReferenceValue != null) { EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel); int baseIndent = EditorGUI.indentLevel; var regions = AtlasAssetInspector.GetRegions(atlasAsset.GetAtlas()); AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { EditorGUI.indentLevel = baseIndent; using (new GUILayout.HorizontalScope()) using (new EditorGUI.DisabledGroupScope(true)) EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.indentLevel = baseIndent + 1; } else { EditorGUILayout.HelpBox("Page missing material!", MessageType.Warning); } } EditorGUILayout.LabelField(new GUIContent(regions[i].name, SpineEditorUtilities.Icons.image)); } EditorGUI.indentLevel = baseIndent; } #endif if (serializedObject.ApplyModifiedProperties() || SpineInspectorUtility.UndoRedoPerformed(Event.current)) { atlasAsset.Clear(); } }
override public void OnInspectorGUI () { serializedObject.Update(); atlasAsset = atlasAsset ?? (AtlasAsset)target; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(materials, true); if (EditorGUI.EndChangeCheck()) serializedObject.ApplyModifiedProperties(); if (materials.arraySize == 0) { EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); return; } for (int i = 0; i < materials.arraySize; i++) { SerializedProperty prop = materials.GetArrayElementAtIndex(i); Material mat = (Material)prop.objectReferenceValue; if (mat == null) { EditorGUILayout.LabelField(new GUIContent("Error: Materials cannot be null", SpineEditorUtilities.Icons.warning)); return; } } if (atlasFile.objectReferenceValue != null) { if (GUILayout.Button( new GUIContent( "Apply Regions as Texture Sprite Slices", "Adds Sprite slices to atlas texture(s). " + "Updates existing slices if ones with matching names exist. \n\n" + "If your atlas was exported with Premultiply Alpha, " + "your SpriteRenderer should use the generated Spine _Material asset (or any Material with a PMA shader) instead of Sprites-Default.") , GUILayout.Height(70f))) { var atlas = atlasAsset.GetAtlas(); foreach (var m in atlasAsset.materials) UpdateSpriteSlices(m.mainTexture, atlas); } } #if REGION_BAKING_MESH if (atlasFile.objectReferenceValue != null) { Atlas atlas = asset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List<AtlasRegion> regions = (List<AtlasRegion>)field.GetValue(atlas); EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon)); EditorGUI.indentLevel++; AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { GUILayout.BeginHorizontal(); { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.EndDisabledGroup(); } GUILayout.EndHorizontal(); } else { EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning)); } } GUILayout.BeginHorizontal(); { //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]); bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft(" " + regions[i].name, baked[i]); if(baked[i]){ EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250)); } if (result && !baked[i]) { //bake baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); EditorGUIUtility.PingObject(bakedObjects[i]); } else if (!result && baked[i]) { //unbake bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel"); switch (unbakeResult) { case true: //delete string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; break; case false: //do nothing break; } } } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; #if BAKE_ALL_BUTTON // Check state bool allBaked = true; bool allUnbaked = true; for (int i = 0; i < regions.Count; i++) { allBaked &= baked[i]; allUnbaked &= !baked[i]; } if (!allBaked && GUILayout.Button("Bake All")) { for (int i = 0; i < regions.Count; i++) { if (!baked[i]) { baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); } } } else if (!allUnbaked && GUILayout.Button("Unbake All")) { bool unbakeResult = EditorUtility.DisplayDialog("Delete All Baked Regions", "Are you sure you want to unbake all region prefabs? This cannot be undone.", "Yes", "Cancel"); switch (unbakeResult) { case true: //delete for (int i = 0; i < regions.Count; i++) { if (baked[i]) { string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; } } break; case false: //do nothing break; } } #endif } #endif if (serializedObject.ApplyModifiedProperties() || (UnityEngine.Event.current.type == EventType.ValidateCommand && UnityEngine.Event.current.commandName == "UndoRedoPerformed") ) { atlasAsset.Reset(); } }
override public void OnInspectorGUI() { serializedObject.Update(); atlasAsset = atlasAsset ?? (AtlasAsset)target; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(materials, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } if (materials.arraySize == 0) { EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); return; } for (int i = 0; i < materials.arraySize; i++) { SerializedProperty prop = materials.GetArrayElementAtIndex(i); Material mat = (Material)prop.objectReferenceValue; if (mat == null) { EditorGUILayout.LabelField(new GUIContent("Error: Materials cannot be null", SpineEditorUtilities.Icons.warning)); return; } } if (atlasFile.objectReferenceValue != null) { if (GUILayout.Button( new GUIContent( "Apply Regions as Texture Sprite Slices", "Adds Sprite slices to atlas texture(s). " + "Updates existing slices if ones with matching names exist. \n\n" + "If your atlas was exported with Premultiply Alpha, " + "your SpriteRenderer should use the generated Spine _Material asset (or any Material with a PMA shader) instead of Sprites-Default.") , GUILayout.Height(70f))) { var atlas = atlasAsset.GetAtlas(); foreach (var m in atlasAsset.materials) { UpdateSpriteSlices(m.mainTexture, atlas); } } } #if REGION_BAKING_MESH if (atlasFile.objectReferenceValue != null) { Atlas atlas = asset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas); EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon)); EditorGUI.indentLevel++; AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { GUILayout.BeginHorizontal(); { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.EndDisabledGroup(); } GUILayout.EndHorizontal(); } else { EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning)); } } GUILayout.BeginHorizontal(); { //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]); bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft(" " + regions[i].name, baked[i]); if (baked[i]) { EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250)); } if (result && !baked[i]) { //bake baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); EditorGUIUtility.PingObject(bakedObjects[i]); } else if (!result && baked[i]) { //unbake bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel"); switch (unbakeResult) { case true: //delete string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; break; case false: //do nothing break; } } } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; #if BAKE_ALL_BUTTON // Check state bool allBaked = true; bool allUnbaked = true; for (int i = 0; i < regions.Count; i++) { allBaked &= baked[i]; allUnbaked &= !baked[i]; } if (!allBaked && GUILayout.Button("Bake All")) { for (int i = 0; i < regions.Count; i++) { if (!baked[i]) { baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); } } } else if (!allUnbaked && GUILayout.Button("Unbake All")) { bool unbakeResult = EditorUtility.DisplayDialog("Delete All Baked Regions", "Are you sure you want to unbake all region prefabs? This cannot be undone.", "Yes", "Cancel"); switch (unbakeResult) { case true: //delete for (int i = 0; i < regions.Count; i++) { if (baked[i]) { string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; } } break; case false: //do nothing break; } } #endif } #endif if (serializedObject.ApplyModifiedProperties() || (UnityEngine.Event.current.type == EventType.ValidateCommand && UnityEngine.Event.current.commandName == "UndoRedoPerformed") ) { atlasAsset.Reset(); } }
override public void OnInspectorGUI () { if (serializedObject.isEditingMultipleObjects) { DrawDefaultInspector(); return; } serializedObject.Update(); atlasAsset = atlasAsset ?? (AtlasAsset)target; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(materials, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); atlasAsset.Clear(); atlasAsset.GetAtlas(); } if (materials.arraySize == 0) { EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); return; } for (int i = 0; i < materials.arraySize; i++) { SerializedProperty prop = materials.GetArrayElementAtIndex(i); Material mat = (Material)prop.objectReferenceValue; if (mat == null) { EditorGUILayout.LabelField(new GUIContent("Error: Materials cannot be null", SpineEditorUtilities.Icons.warning)); return; } } EditorGUILayout.Space(); if (atlasFile.objectReferenceValue != null) { if (SpineInspectorUtility.LargeCenteredButton(SpriteSlicesLabel)) { var atlas = atlasAsset.GetAtlas(); foreach (var m in atlasAsset.materials) UpdateSpriteSlices(m.mainTexture, atlas); } } #if REGION_BAKING_MESH if (atlasFile.objectReferenceValue != null) { Atlas atlas = asset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List<AtlasRegion> regions = (List<AtlasRegion>)field.GetValue(atlas); EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon)); EditorGUI.indentLevel++; AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { GUILayout.BeginHorizontal(); { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.EndDisabledGroup(); } GUILayout.EndHorizontal(); } else { EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning)); } } GUILayout.BeginHorizontal(); { //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]); bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft(" " + regions[i].name, baked[i]); if(baked[i]){ EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250)); } if (result && !baked[i]) { //bake baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); EditorGUIUtility.PingObject(bakedObjects[i]); } else if (!result && baked[i]) { //unbake bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel"); switch (unbakeResult) { case true: //delete string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; break; case false: //do nothing break; } } } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; #if BAKE_ALL_BUTTON // Check state bool allBaked = true; bool allUnbaked = true; for (int i = 0; i < regions.Count; i++) { allBaked &= baked[i]; allUnbaked &= !baked[i]; } if (!allBaked && GUILayout.Button("Bake All")) { for (int i = 0; i < regions.Count; i++) { if (!baked[i]) { baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); } } } else if (!allUnbaked && GUILayout.Button("Unbake All")) { bool unbakeResult = EditorUtility.DisplayDialog("Delete All Baked Regions", "Are you sure you want to unbake all region prefabs? This cannot be undone.", "Yes", "Cancel"); switch (unbakeResult) { case true: //delete for (int i = 0; i < regions.Count; i++) { if (baked[i]) { string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; } } break; case false: //do nothing break; } } #endif } #else if (atlasFile.objectReferenceValue != null) { EditorGUILayout.LabelField("Atlas Regions", EditorStyles.boldLabel); int baseIndent = EditorGUI.indentLevel; var regions = AtlasAssetInspector.GetRegions(atlasAsset.GetAtlas()); AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { EditorGUI.indentLevel = baseIndent; using (new GUILayout.HorizontalScope()) using (new EditorGUI.DisabledGroupScope(true)) EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.indentLevel = baseIndent + 1; } else { EditorGUILayout.HelpBox("Page missing material!", MessageType.Warning); } } EditorGUILayout.LabelField(new GUIContent(regions[i].name, SpineEditorUtilities.Icons.image)); } EditorGUI.indentLevel = baseIndent; } #endif if (serializedObject.ApplyModifiedProperties() || SpineInspectorUtility.UndoRedoPerformed(Event.current)) atlasAsset.Clear(); }
override public void OnInspectorGUI() { serializedObject.Update(); AtlasAsset asset = (AtlasAsset)target; EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(atlasFile); EditorGUILayout.PropertyField(materials, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } if (materials.arraySize == 0) { EditorGUILayout.LabelField(new GUIContent("Error: Missing materials", SpineEditorUtilities.Icons.warning)); return; } for (int i = 0; i < materials.arraySize; i++) { SerializedProperty prop = materials.GetArrayElementAtIndex(i); Material mat = (Material)prop.objectReferenceValue; if (mat == null) { EditorGUILayout.LabelField(new GUIContent("Error: Materials cannot be null", SpineEditorUtilities.Icons.warning)); return; } } if (atlasFile.objectReferenceValue != null) { Atlas atlas = asset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas); EditorGUILayout.LabelField(new GUIContent("Region Baking", SpineEditorUtilities.Icons.unityIcon)); EditorGUI.indentLevel++; AtlasPage lastPage = null; for (int i = 0; i < regions.Count; i++) { if (lastPage != regions[i].page) { if (lastPage != null) { EditorGUILayout.Separator(); EditorGUILayout.Separator(); } lastPage = regions[i].page; Material mat = ((Material)lastPage.rendererObject); if (mat != null) { GUILayout.BeginHorizontal(); { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField(mat, typeof(Material), false, GUILayout.Width(250)); EditorGUI.EndDisabledGroup(); } GUILayout.EndHorizontal(); } else { EditorGUILayout.LabelField(new GUIContent("Page missing material!", SpineEditorUtilities.Icons.warning)); } } GUILayout.BeginHorizontal(); { //EditorGUILayout.ToggleLeft(baked[i] ? "" : regions[i].name, baked[i]); bool result = baked[i] ? EditorGUILayout.ToggleLeft("", baked[i], GUILayout.Width(24)) : EditorGUILayout.ToggleLeft(" " + regions[i].name, baked[i]); if (baked[i]) { EditorGUILayout.ObjectField(bakedObjects[i], typeof(GameObject), false, GUILayout.Width(250)); } if (result && !baked[i]) { //bake baked[i] = true; bakedObjects[i] = SpineEditorUtilities.BakeRegion(atlasAsset, regions[i]); EditorGUIUtility.PingObject(bakedObjects[i]); } else if (!result && baked[i]) { //unbake bool unbakeResult = EditorUtility.DisplayDialog("Delete Baked Region", "Do you want to delete the prefab for " + regions[i].name, "Yes", "Cancel"); switch (unbakeResult) { case true: //delete string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(regions[i]) + ".prefab").Replace("\\", "/"); AssetDatabase.DeleteAsset(bakedPrefabPath); baked[i] = false; break; case false: //do nothing break; } } } GUILayout.EndHorizontal(); } EditorGUI.indentLevel--; } if (serializedObject.ApplyModifiedProperties() || (UnityEngine.Event.current.type == EventType.ValidateCommand && UnityEngine.Event.current.commandName == "UndoRedoPerformed") ) { asset.Reset(); } }