支持九宫格

This commit is contained in:
Soviby 2024-12-02 00:33:27 +08:00
parent 708dc913d0
commit 74aa81a4ba
10 changed files with 63 additions and 89 deletions

View File

@ -109,19 +109,20 @@ function s2t(t) { return stringIDToTypeID(t) }
this.documentName = app.activeDocument.name.slice(0, -4); this.documentName = app.activeDocument.name.slice(0, -4);
// 导出整个psd作为png参考图
var i, j, k, len, ref1, removeFiles; var i, j, k, len, ref1, removeFiles;
var baseFolder = Folder(this.saveFolder + "/" + this.documentName); var baseFolder = Folder(this.saveFolder + "/" + this.documentName);
if (baseFolder.exists) { if (baseFolder.exists) {
removeFiles = baseFolder.getFiles(); removeFiles = baseFolder.getFiles();
for (i = j = 0, ref1 = removeFiles.length; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) { for (i = j = 0, ref1 = removeFiles.length; 0 <= ref1 ? j < ref1 : j > ref1; i = 0 <= ref1 ? ++j : --j) {
if (removeFiles[i].name.startsWith(this.documentName) && removeFiles[i].name.endsWith('.png')) { removeFiles[i].remove();
removeFiles[i].remove(); // if (removeFiles[i].name.startsWith(this.documentName) && removeFiles[i].name.endsWith('.png')) {
} // removeFiles[i].remove();
// }
} }
baseFolder.remove(); baseFolder.remove();
} }
baseFolder.create(); baseFolder.create();
// 导出整个psd作为png参考图
saveFile = new File(baseFolder.fsName + "/" + "__background__.png"); saveFile = new File(baseFolder.fsName + "/" + "__background__.png");
options = new ExportOptionsSaveForWeb(); options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.PNG; options.format = SaveDocumentType.PNG;

Binary file not shown.

View File

@ -11,6 +11,9 @@ namespace UguiToolkit.Editor
void ApplyTransform(Transform tf); void ApplyTransform(Transform tf);
bool IsInside(Transform tf); bool IsInside(Transform tf);
void ApplyData<T>(T ui) where T: MonoBehaviour; void ApplyData<T>(T ui) where T: MonoBehaviour;
void InitPreview();
void ShowSelectionImg(bool show);
GameObject gameObject { get; }
} }
public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo
@ -20,10 +23,6 @@ namespace UguiToolkit.Editor
private UnityEngine.UI.Image m_selectionImg; private UnityEngine.UI.Image m_selectionImg;
public T2 ElementInfo => m_elementInfo; public T2 ElementInfo => m_elementInfo;
public abstract void ApplyTransform(Transform tf);
protected abstract void OnApplyData(T1 ui);
public void ShowSelectionImg(bool show) public void ShowSelectionImg(bool show)
{ {
if (m_selectionImg) if (m_selectionImg)
@ -32,6 +31,12 @@ namespace UguiToolkit.Editor
} }
} }
public abstract void ApplyTransform(Transform tf);
public abstract void InitPreview();
protected abstract void OnApplyData(T1 ui);
public bool IsInside(Transform tf) public bool IsInside(Transform tf)
{ {
var rect = ElementInfo.Rect; var rect = ElementInfo.Rect;
@ -65,7 +70,7 @@ namespace UguiToolkit.Editor
rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h); rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h);
m_selectionImg = go.AddComponent<UnityEngine.UI.Image>(); m_selectionImg = go.AddComponent<UnityEngine.UI.Image>();
m_selectionImg.color = new Color(0, 1, 0, 0.2f); m_selectionImg.color = new Color(0, 1, 0, 0.15f);
} }
} }
} }

View File

@ -21,25 +21,17 @@ namespace UguiToolkit.Editor
private Image m_previewImage; private Image m_previewImage;
protected override void OnApplyData(Image ui) // ²éÕÒʱµ÷ÓÃ
public void SetTransform(float rotiation, float2 scale, bool similarityCalc)
{ {
ApplyTransform(ui.transform); this.rotiation = rotiation;
this.scale = scale;
this.similarityCalc = similarityCalc;
this.needFillTransform = true;
} }
public void InitPreviewImage() private void LoadImageFromFile(string path)
{
if (ElementInfo == null) return;
if (!TryGetComponent<Image>(out m_previewImage))
{
m_previewImage = gameObject.AddComponent<Image>();
}
LoadImageFromFile(ElementInfo.imgPath);
ApplyTransform(transform);
}
void LoadImageFromFile(string path)
{ {
if (System.IO.File.Exists(path)) if (System.IO.File.Exists(path))
{ {
@ -58,18 +50,29 @@ namespace UguiToolkit.Editor
} }
} }
// ²éÕÒʱµ÷Óà protected override void OnApplyData(Image ui)
public void SetTransform(float rotiation, float2 scale, bool similarityCalc)
{ {
this.rotiation = rotiation; ApplyTransform(ui.transform);
this.scale = scale;
this.similarityCalc = similarityCalc;
this.needFillTransform = true; if (ElementInfo.HaveSlice)
{
ui.type = Image.Type.Sliced;
}
}
public override void InitPreview()
{
if (ElementInfo == null) return;
if (!TryGetComponent<Image>(out m_previewImage))
{
m_previewImage = gameObject.AddComponent<Image>();
}
LoadImageFromFile(ElementInfo.imgPath);
ApplyTransform(transform);
} }
// ²éÕÒʱµ÷ÓÃ
public void ClearFillTransform() => this.needFillTransform = false;
public override void ApplyTransform(Transform tf) public override void ApplyTransform(Transform tf)
{ {

View File

@ -23,7 +23,7 @@ namespace UguiToolkit.Editor
rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10); rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10);
} }
public void InitPreviewText() public override void InitPreview()
{ {
if (ElementInfo == null) return; if (ElementInfo == null) return;

View File

@ -30,7 +30,7 @@ namespace UguiToolkit.Editor
rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10); rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10);
} }
public void InitPreviewText() public override void InitPreview()
{ {
if (ElementInfo == null) return; if (ElementInfo == null) return;

View File

@ -31,7 +31,8 @@ namespace UguiToolkit.Editor
y = layoutElementJsonData.y, y = layoutElementJsonData.y,
w = layoutElementJsonData.w, w = layoutElementJsonData.w,
h = layoutElementJsonData.h, h = layoutElementJsonData.h,
layoutInfo = layoutInfo layoutInfo = layoutInfo,
imgSlicePath = layoutElementJsonData.slice ? layoutElementJsonData.imageName + "-slice" : null
}); });
} }
else if (layoutElementJsonData.type == "Text") else if (layoutElementJsonData.type == "Text")
@ -104,6 +105,7 @@ namespace UguiToolkit.Editor
// ͼƬ // ͼƬ
public string imageName; public string imageName;
public bool slice; // 是否为九宫
// Îı¾ // Îı¾
public string text; public string text;

View File

@ -5,7 +5,6 @@ using System.IO;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement; using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
using static UguiToolkit.Editor.LayoutInfo;
namespace UguiToolkit.Editor namespace UguiToolkit.Editor
{ {
@ -19,8 +18,7 @@ namespace UguiToolkit.Editor
private GameObject m_curSelectionGo; private GameObject m_curSelectionGo;
private List<ImageEntity> m_imageEntities; private List<ImageEntity> m_imageEntities;
private List<TextEntity> m_textEntities; private List<IEntity> m_textEntities;
private List<TextMeshProEntity> m_tmpEntities;
private List<IEntity> m_selectionEntities; private List<IEntity> m_selectionEntities;
[LabelText("脱离选择控制"), ShowInInspector] [LabelText("脱离选择控制"), ShowInInspector]
@ -189,7 +187,7 @@ namespace UguiToolkit.Editor
foreach (var rotScale in rotScaleInfoItems) foreach (var rotScale in rotScaleInfoItems)
{ {
var imgInfo = imgEntity.ElementInfo; var imgInfo = imgEntity.ElementInfo;
if (imgInfo.imgPath == rotScale.imgPath) if ( (imgInfo.HaveSlice && imgInfo.imgSlicePath == rotScale.imgPath) || imgInfo.imgPath == rotScale.imgPath)
{ {
if (rotScale.similarityCalc) if (rotScale.similarityCalc)
{ {
@ -226,10 +224,6 @@ namespace UguiToolkit.Editor
{ {
textEntity.gameObject.SetActive(false); textEntity.gameObject.SetActive(false);
} }
foreach (var textEntity in m_tmpEntities)
{
textEntity.gameObject.SetActive(false);
}
} }
else if (activeGameObject.TryGetComponent<UnityEngine.UI.Text>(out var text)) else if (activeGameObject.TryGetComponent<UnityEngine.UI.Text>(out var text))
{ {
@ -261,45 +255,6 @@ namespace UguiToolkit.Editor
{ {
imgEntity.gameObject.SetActive(false); imgEntity.gameObject.SetActive(false);
} }
foreach (var textEntity in m_tmpEntities)
{
textEntity.gameObject.SetActive(false);
}
}
else if (activeGameObject.TryGetComponent<TMPro.TextMeshProUGUI>(out var tmp))
{
bool IsInside = false;
m_entityRoot.gameObject.SetActive(true);
foreach (var tmpEntity in m_tmpEntities)
{
tmpEntity.ShowSelectionImg(true);
tmpEntity.gameObject.SetActive(true);
m_selectionEntities.Add(tmpEntity);
if (!IsInside && tmpEntity.IsInside(activeGameObject.transform)) IsInside = true;
}
if (IsInside)
{
if (m_lastSelectionGo && m_lastSelectionGo == activeGameObject)
{
m_curSelectionGo = activeGameObject;
}
}
else
{
m_curSelectionGo = activeGameObject;
}
foreach (var imgEntity in m_imageEntities)
{
imgEntity.gameObject.SetActive(false);
}
foreach (var textEntity in m_textEntities)
{
textEntity.gameObject.SetActive(false);
}
} }
} }
} }
@ -318,7 +273,6 @@ namespace UguiToolkit.Editor
UpdateHierarchyOfEntity(show, m_entityRoot.gameObject); UpdateHierarchyOfEntity(show, m_entityRoot.gameObject);
foreach (var entity in m_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject); foreach (var entity in m_imageEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject); foreach (var entity in m_textEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
foreach (var entity in m_tmpEntities) UpdateHierarchyOfEntity(show, entity.gameObject);
} }
private void UpdateHierarchyOfEntity(in bool show, in GameObject entity) private void UpdateHierarchyOfEntity(in bool show, in GameObject entity)
@ -339,7 +293,6 @@ namespace UguiToolkit.Editor
m_imageEntities = new(m_panelCache.layoutInfo.Count); m_imageEntities = new(m_panelCache.layoutInfo.Count);
m_textEntities = new(m_panelCache.layoutInfo.Count); m_textEntities = new(m_panelCache.layoutInfo.Count);
m_tmpEntities = new(m_panelCache.layoutInfo.Count);
m_selectionEntities = new(m_panelCache.layoutInfo.Count); m_selectionEntities = new(m_panelCache.layoutInfo.Count);
foreach (var elementInfo in m_panelCache.GetLayoutElementInfos<LayoutInfo.ElementInfo>()) foreach (var elementInfo in m_panelCache.GetLayoutElementInfos<LayoutInfo.ElementInfo>())
@ -353,10 +306,12 @@ namespace UguiToolkit.Editor
entity.transform.SetSiblingIndex(0); entity.transform.SetSiblingIndex(0);
entity.SetData(imgInfo); entity.SetData(imgInfo);
entity.InitPreviewImage(); entity.InitPreview();
m_imageEntities.Add(entity); m_imageEntities.Add(entity);
UpdateHierarchyOfEntity(false, entity.gameObject); UpdateHierarchyOfEntity(false, entity.gameObject);
// 创建九宫格
continue; continue;
} }
var textInfo = elementInfo as LayoutInfo.TextInfo; var textInfo = elementInfo as LayoutInfo.TextInfo;
@ -370,9 +325,9 @@ namespace UguiToolkit.Editor
entity.transform.SetSiblingIndex(0); entity.transform.SetSiblingIndex(0);
entity.SetData(textInfo); entity.SetData(textInfo);
entity.InitPreviewText(); entity.InitPreview();
m_tmpEntities.Add(entity); m_textEntities.Add(entity);
UpdateHierarchyOfEntity(false, entity.gameObject); UpdateHierarchyOfEntity(false, entity.gameObject);
continue; continue;
} }
@ -384,7 +339,7 @@ namespace UguiToolkit.Editor
entity.transform.SetSiblingIndex(0); entity.transform.SetSiblingIndex(0);
entity.SetData(textInfo); entity.SetData(textInfo);
entity.InitPreviewText(); entity.InitPreview();
m_textEntities.Add(entity); m_textEntities.Add(entity);
UpdateHierarchyOfEntity(false, entity.gameObject); UpdateHierarchyOfEntity(false, entity.gameObject);

View File

@ -39,6 +39,12 @@ namespace UguiToolkit.Editor
{ {
imgInfo.imgPath = System.IO.Path.Join(targetImgDirPath, imgInfo.imgPath) + ".png"; imgInfo.imgPath = System.IO.Path.Join(targetImgDirPath, imgInfo.imgPath) + ".png";
imgInfo.imgPath = Path.GetRelativePath(projectPath, imgInfo.imgPath).Replace("\\", "/"); imgInfo.imgPath = Path.GetRelativePath(projectPath, imgInfo.imgPath).Replace("\\", "/");
if (imgInfo.HaveSlice)
{
imgInfo.imgSlicePath = System.IO.Path.Join(targetImgDirPath, imgInfo.imgSlicePath) + ".png";
imgInfo.imgSlicePath = Path.GetRelativePath(projectPath, imgInfo.imgSlicePath).Replace("\\", "/");
}
} }
} }
@ -210,6 +216,8 @@ namespace UguiToolkit.Editor
public class ImageInfo : ElementInfo public class ImageInfo : ElementInfo
{ {
public string imgPath; public string imgPath;
public string imgSlicePath; // 九宫格图片路径
public bool HaveSlice => imgSlicePath != null; // 是否有九宫格
} }
[Serializable] [Serializable]