public bool Render() { target.Update(); EditorGUI.BeginChangeCheck(); var tex = EditorGUILayout.ObjectField("Sprite", null, typeof(Texture2D), false); if (tex != null) { var assetpath = AssetDatabase.GetAssetPath(tex); var new_crc = HUDHelper.GetCRC32(assetpath); Rect uv_rect; if (HUDManager.Instance.Setting.atlas.QueryUV(new_crc, out uv_rect)) { var uv_property = sprite.FindPropertyRelative("uv0_rect").FindPropertyRelative("Array.data[0]"); uv_property.FindPropertyRelative("x").floatValue = uv_rect.xMin; uv_property.FindPropertyRelative("y").floatValue = uv_rect.yMin; uv_property.FindPropertyRelative("z").floatValue = uv_rect.width; uv_property.FindPropertyRelative("w").floatValue = uv_rect.height; } else { var w = EditorWindow.mouseOverWindow; w.ShowNotification(new GUIContent("不在图集中", EditorGUIUtility.Load(EditorResources.iconsPath + "console.erroricon.png") as Texture2D)); } } EditorGUILayout.PropertyField(position, new GUIContent("position")); EditorGUILayout.PropertyField(scale, new GUIContent("scale")); EditorGUILayout.PropertyField(size, new GUIContent("size")); EditorGUILayout.PropertyField(color, new GUIContent("color")); if (GUILayout.Button("NativeSize")) { var tex_size = HUDManager.Instance.Setting.atlas.size; var uv_property = sprite.FindPropertyRelative("uv0_rect").FindPropertyRelative("Array.data[0]"); float width = uv_property.FindPropertyRelative("z").floatValue; float height = uv_property.FindPropertyRelative("w").floatValue; float n_width = Mathf.Floor(tex_size.x * width); float n_height = Mathf.Floor(tex_size.y * height); size.FindPropertyRelative("x").floatValue = n_width; size.FindPropertyRelative("y").floatValue = n_height; } if (EditorGUI.EndChangeCheck()) { target.ApplyModifiedProperties(); return(true); } return(false); }
public static void CreateAtlats(string[] textures, string path) { HUDAtlas atlas = AssetDatabase.LoadAssetAtPath <HUDAtlas>(path); if (atlas == null) { atlas = ScriptableObject.CreateInstance <HUDAtlas>(); AssetDatabase.CreateAsset(atlas, path); AssetDatabase.Refresh(); } List <Texture2D> texture2ds = new List <Texture2D>(); List <uint> crcids = new List <uint>(); foreach (var p in textures) { var tex = AssetDatabase.LoadAssetAtPath <Texture2D>(p); if (tex != null) { var u_tex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false, false); RenderTexture tmp = RenderTexture.GetTemporary(tex.width, tex.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default); Graphics.Blit(tex, tmp); RenderTexture.active = tmp; u_tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); u_tex.Apply(); texture2ds.Add(u_tex); crcids.Add(HUDHelper.GetCRC32(p)); } } var alta_tex = new Texture2D(1, 1, TextureFormat.RGBA32, false, false); var uvs = alta_tex.PackTextures(texture2ds.ToArray(), 4, 2048, false); if (uvs == null) { Debug.LogError("package error"); return; } var datas = alta_tex.EncodeToPNG(); var atlas_tex_path = System.IO.Path.GetDirectoryName(path) + "/" + System.IO.Path.GetFileNameWithoutExtension(path) + ".png"; System.IO.File.WriteAllBytes(atlas_tex_path, datas); AssetDatabase.ImportAsset(atlas_tex_path); atlas.crc32ids = crcids.ToArray(); atlas.uvs = uvs; atlas.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(atlas_tex_path); AssetDatabase.SaveAssets(); }