添加 entity 接口和 抽象类

This commit is contained in:
Soviby 2024-10-22 00:20:39 +08:00
parent 2d61e2d96f
commit 2d22ae3def
23 changed files with 289 additions and 86 deletions

View File

@ -944,28 +944,32 @@ function s2t(t) { return stringIDToTypeID(t) }
outputDoc.layers[1].remove()
layer = outputDoc.layers[0]
if (opt['九宫格']) {
if (!layer.isBackgroundLayer) {
var param;
if (opt['九宫格']) {
param = opt['九宫格'];
}
// if (opt['九宫格']) {
// if (!layer.isBackgroundLayer) {
// var param;
// if (opt['九宫格']) {
// param = opt['九宫格'];
// }
if (opt['九宫格'] === "true") {
this.snipImage(outputDoc, layer, opt);
fileName += "-slice";
}
else {
outputDoc = this.sliceCutImg(outputDoc, layerName, param);
fileName += "-noslice";
}
}
}
else {
fileName += "-noslice";
if (!layer.isBackgroundLayer) {
this.snipImage(outputDoc, layer, opt);
}
// if (opt['九宫格'] === "true") {
// this.snipImage(outputDoc, layer, opt);
// fileName += "-slice";
// }
// else {
// outputDoc = this.sliceCutImg(outputDoc, layerName, param);
// fileName += "-noslice";
// }
// }
// }
// else {
// fileName += "-noslice";
// if (!layer.isBackgroundLayer) {
// this.snipImage(outputDoc, layer, opt);
// }
// }
if (!layer.isBackgroundLayer) {
this.snipImage(outputDoc, layer, opt);
}
saveFile = new File(this.baseFolder.fsName + "/" + fileName + ".png");

Binary file not shown.

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 558d96c5ace2fc7408f8fa23ef43414e
guid: 704dd5babe586c04e966a731c5186616
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@ -0,0 +1,58 @@
using System.Collections;
using Unity.Mathematics;
using UnityEngine;
namespace UguiToolkit.Editor
{
public interface IEntity
{
void SetTransform(float rotiation, float2 scale);
void ApplyTransform(Transform transform);
}
public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : MonoBehaviour where T2 : LayoutInfo.ElementInfo
{
// ElementInfo
private T2 m_elementInfo;
private float rotiation;
private float2 scale;
private bool needFillTransform;
public void ApplyTransform(Transform transform)
{
if (needFillTransform)
{
transform.rotation = Quaternion.Euler(0, 0, rotiation);
transform.localScale = new Vector3(scale.x, scale.y, 0);
}
var position = m_elementInfo.Position;
transform.position = new Vector3(position.x, position.y, 0);
}
// 查找时调用
public void SetTransform(float rotiation, float2 scale)
{
this.rotiation = rotiation;
this.scale = scale;
this.needFillTransform = true;
}
// 查找时调用
public void ClearFillTransform() => this.needFillTransform = false;
// 创建时调用
public void SetData(T2 elementInfo)
{
this.m_elementInfo = elementInfo;
}
public void ApplyData(T1 ui)
{
OnApplyData(ui, m_elementInfo);
}
protected virtual void OnApplyData(T1 ui, T2 elementInfo) { }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d91868a964b23744f80d13a618ee6ec1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UguiToolkit.Editor;
using UnityEngine;
using UnityEngine.UI;
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
{
protected override void OnApplyData(Image ui, LayoutInfo.ImageInfo elementInfo)
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 21bdf5cdb7e38cf45bc06153662310b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UguiToolkit.Editor;
using UnityEngine;
using UnityEngine.UI;
public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
{
protected override void OnApplyData(Text ui, LayoutInfo.TextInfo elementInfo)
{
ui.text = elementInfo.text;
ui.fontSize = (int)elementInfo.size;
ui.color = elementInfo.color;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d5791996f4b6cd4f9f2f6ee325c1701
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -25,6 +25,7 @@ namespace UguiToolkit.Editor
{
elementInfos.Add(new LayoutInfo.ImageInfo()
{
name = layoutElementJsonData.name,
imgPath = layoutElementJsonData.imageName,
x = layoutElementJsonData.x,
y = layoutElementJsonData.y,
@ -35,8 +36,22 @@ namespace UguiToolkit.Editor
}
else if (layoutElementJsonData.type == "Text")
{
// TODO
ColorUtility.TryParseHtmlString(layoutElementJsonData.color, out var color);
elementInfos.Add(new LayoutInfo.TextInfo()
{
name = layoutElementJsonData.name,
text = layoutElementJsonData.text,
font = layoutElementJsonData.font,
size = layoutElementJsonData.size,
align = layoutElementJsonData.align,
color = color,
x = layoutElementJsonData.x,
y = layoutElementJsonData.y,
w = layoutElementJsonData.w,
h = layoutElementJsonData.h,
layoutInfo = layoutInfo
});
}
if (layoutElementJsonData.elements != null)
@ -87,7 +102,14 @@ namespace UguiToolkit.Editor
public float h;
public List<LayoutElementJsonData> elements;
// ͟Ə
public string imageName;
// ÎÄąž
public string text;
public float size;
public string color;
public string align;
public string font;
}
}

View File

@ -3,7 +3,7 @@ using UnityEngine;
namespace UguiToolkit
{
public class ImageActorManager : MonoBehaviour
public class ActorManager : MonoBehaviour, IManager
{
private void Start()
{

View File

@ -0,0 +1,10 @@
//using System;
//using UnityEngine;
//namespace UguiToolkit
//{
// public class EntityManager : MonoBehaviour, IManager
// {
// private PanelCache panelCache;
// }
//}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e8be088e252d5d94995caae74d690cc6
timeCreated: 1719934421

View File

@ -1,4 +1,5 @@
using UguiToolkit.Editor;
using UnityEditor;
namespace UguiToolkit
{
@ -20,5 +21,11 @@ namespace UguiToolkit
public SettingScriptObject setting;
public CacheScriptObject cache;
public void SaveCache()
{
EditorUtility.SetDirty(cache);
AssetDatabase.SaveAssetIfDirty(cache);
}
}
}

View File

@ -2,6 +2,7 @@
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
namespace UguiToolkit
{
@ -12,6 +13,8 @@ namespace UguiToolkit
public Scene Scene => m_scene;
private GameObject m_prefabContentsRoot;
public GameObject PrefabContentsRoot => m_prefabContentsRoot;
private Dictionary<Type, IManager> m_mngMap = new Dictionary<Type, IManager>();
private static StageManager m_instance;
public static StageManager Instance
@ -27,7 +30,7 @@ namespace UguiToolkit
}
}
public static void CreateStageManager(Scene scene, GameObject prefabContentsRoot)
public static StageManager CreateStageManager(Scene scene, GameObject prefabContentsRoot)
{
var go = new GameObject();
go.name = "StageManager";
@ -37,22 +40,28 @@ namespace UguiToolkit
m_instance.m_prefabContentsRoot = prefabContentsRoot;
go.hideFlags = HideFlags.DontSave;
return m_instance;
}
private T CreateSubManager<T>() where T : MonoBehaviour
public T CreateSubManager<T>() where T : MonoBehaviour, IManager
{
var go = new GameObject();
go.name = typeof(T).Name;
go.transform.parent = PrefabContentsRoot.transform;
var t = go.AddComponent<T>();
go.hideFlags = HideFlags.DontSave;
var t = gameObject.AddComponent<T>();
m_mngMap[typeof(T)] = t;
return t;
}
private void Start()
public T GetManager<T>() where T : MonoBehaviour, IManager
{
CreateSubManager<ImageActorManager>();
if (m_mngMap.TryGetValue(typeof(T), out var mng))
return mng as T;
return null;
}
}
}
public interface IManager {
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using Sirenix.OdinInspector;
using static UguiToolkit.Editor.LayoutInfo;
namespace UguiToolkit.Editor
{
@ -21,29 +22,55 @@ namespace UguiToolkit.Editor
public string layoutInfoFilePath; // Sample.layout.txt
[LabelText("目标图片文件夹路径")]
public string TargetImgDirPath
{
get
{
if (string.IsNullOrWhiteSpace(layoutInfoFilePath)) return "";
var dirName = System.IO.Path.GetDirectoryName(layoutInfoFilePath);
var fileName = System.IO.Path.GetFileName(layoutInfoFilePath);
var split = fileName.Split(".");
if (split.Length == 0) return "";
public string TargetImgDirPath => GetTargetImgDirPath(layoutInfoFilePath);
return System.IO.Path.Join(dirName, split[0]);
}
}
// 通过读取layout.txt 获得 position 图片路径(源图) -> Transform信息(效果图)
public Dictionary<string, List<TargetImageTransformInfo>> targetImageTransformInfos = new();
// 通过ElementInfo创建所有Actor
// 1. 鼠标选中img或者text,1s 后触发助手
// img
// 2. 通过img的图片路径查找rotScaleInfos获得匹配的旋转缩放信息. 如果没有查找到则return
// 3. 通过img的图片路径, 查找ActorManager 获得所有和图片路径匹配的Actor,并显示所有匹配的Actor
// 4. 每帧刷新如果鼠标进入Actor的rect中则播放scale动效。鼠标松开则apply 该actor的 trasform
// text
// 2. 每帧刷新查找圆形范围内所有text Actor,并显示所有匹配的Actor
// 3. 每帧刷新如果鼠标进入Actor的rect中则播放scale动效。鼠标松开则apply 该actor的 trasform
// 通过cmd计算获得 图片路径(源图) -> 旋转缩放信息(效果图)
public Dictionary<string, List<RotScaleInfoItem>> rotScaleInfos = new();
public LayoutInfo layoutInfo;
public IEnumerator<T> GetLayoutElementInfos<T>() where T : LayoutInfo.ElementInfo
{
if (layoutInfo == null) yield break;
foreach (var e in layoutInfo)
{
if (e is T)
{
yield return e as T;
}
}
}
public static string GetTargetImgDirPath(in string layoutInfoFilePath)
{
if (string.IsNullOrWhiteSpace(layoutInfoFilePath)) return "";
var dirName = System.IO.Path.GetDirectoryName(layoutInfoFilePath);
var fileName = System.IO.Path.GetFileName(layoutInfoFilePath);
var split = fileName.Split(".");
if (split.Length == 0) return "";
return System.IO.Path.Join(dirName, split[0]);
}
}
[Serializable]
public class LayoutInfo
public class LayoutInfo: IEnumerable<ElementInfo>
{
private List<ElementInfo> m_elementInfos;
private float m_w;
@ -59,8 +86,21 @@ namespace UguiToolkit.Editor
this.m_h = h;
}
public IEnumerator<ElementInfo> GetEnumerator()
{
return ((IEnumerable<ElementInfo>)m_elementInfos).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)m_elementInfos).GetEnumerator();
}
[Serializable]
public class ElementInfo
{
public string name;
public float x;
public float y;
public float w;
@ -68,7 +108,8 @@ namespace UguiToolkit.Editor
public LayoutInfo layoutInfo;
public float2 Position {
public float2 Position
{
get {
float x = (layoutInfo.W / 2f) * -1 + this.x + this.w / 2f;
float y = layoutInfo.H / 2f - this.y - this.h / 2f;
@ -76,16 +117,29 @@ namespace UguiToolkit.Editor
return new float2(x, y);
}
}
public Rect Rect {
get {
var pos = Position - new float2(this.w / 2f, this.h / 2f);
return new(pos.x, pos.y, this.w, this.h);
}
}
}
[Serializable]
public class ImageInfo : ElementInfo
{
public string imgPath;
}
[Serializable]
public class TextInfo : ElementInfo
{
public string text;
public float size;
public Color color;
public string align;
public string font;
}
}

View File

@ -2,8 +2,7 @@
"name": "UguiToolkit.Editor",
"rootNamespace": "",
"references": [
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:f5bcd4641e14bde4c8ba640a2285abc5"
"GUID:d8b63aba1907145bea998dd612889d6b"
],
"includePlatforms": [
"Editor"

View File

@ -32,15 +32,16 @@ namespace UguiToolkit.Editor.Windows
panelCache.layoutInfoFilePath = m_layoutInfoFilePath;
if (!m_cacheExist)
{
panelCache.rotScaleInfos = new();
CalcRotScaleInfos();
PaserLayout();
}
var cache = GlobalManager.Instance.cache;
cache.panelCaches[m_prefab] = panelCache;
UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot);
GlobalManager.Instance.SaveCache();
var stageManager = UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot);
var actorManager = stageManager.CreateSubManager<ActorManager>();
CloseWindow();
}
@ -56,7 +57,9 @@ namespace UguiToolkit.Editor.Windows
using (StreamReader reader = File.OpenText(m_layoutInfoFilePath))
{
var jsonData = reader.ReadToEnd();
layoutParser.Parser(jsonData);
var layoutInfo = layoutParser.Parser(jsonData);
panelCache.layoutInfo = layoutInfo;
}
}
@ -67,6 +70,7 @@ namespace UguiToolkit.Editor.Windows
var jsonData = CommandHelper.CalcRotScale(panelCache.srcImgDirPath, panelCache.TargetImgDirPath, panelCache.rotScaleInfos);
if (jsonData == null || jsonData.data == null) return;
var rotScaleInfos = panelCache.rotScaleInfos;
rotScaleInfos.Clear();
foreach (var kv in jsonData.data)
{
List<RotScaleInfoItem> rotScaleItemList = new();
@ -75,7 +79,7 @@ namespace UguiToolkit.Editor.Windows
{
rotScaleItemList.Add(new ()
{
imgPath = jsonItemData.targetPath,
imgPath = Path.GetRelativePath(Application.dataPath, jsonItemData.targetPath),
rotiation = jsonItemData.rot,
scale = new float2(jsonItemData.scale[0], jsonItemData.scale[1])
});

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: d62d1ab93d3c4beaa6f0d6cf680583da
timeCreated: 1718725983

View File

@ -1,18 +0,0 @@
{
"name": "UguiToolkit",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor",
"WindowsStandalone32",
"WindowsStandalone64"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: f5bcd4641e14bde4c8ba640a2285abc5
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: