实例化所有img

This commit is contained in:
Soviby 2024-10-24 01:30:59 +08:00
parent 2d22ae3def
commit 7fe83e3342
20 changed files with 159 additions and 60 deletions

Binary file not shown.

View File

@ -1,4 +1,5 @@
namespace UguiToolkit.Editor
#if UNITY_EDITOR
namespace UguiToolkit.Editor
{
public class EditorConst
{
@ -9,4 +10,5 @@
public const string RotScaleInfoToolFilePath = "Packages/com.soviby.unity.ui.ugui-toolkit/.tools/result_rot_scale/main.exe";
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System;
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
namespace UguiToolkit.Editor
@ -16,4 +17,5 @@ namespace UguiToolkit.Editor
public float rot;
public float[] scale;
}
}
}
#endif

View File

@ -1,4 +1,4 @@
using System.Collections;
#if UNITY_EDITOR
using Unity.Mathematics;
using UnityEngine;
@ -19,6 +19,8 @@ namespace UguiToolkit.Editor
private float2 scale;
private bool needFillTransform;
protected T2 ElementInfo => m_elementInfo;
public void ApplyTransform(Transform transform)
{
if (needFillTransform)
@ -27,7 +29,7 @@ namespace UguiToolkit.Editor
transform.localScale = new Vector3(scale.x, scale.y, 0);
}
var position = m_elementInfo.Position;
transform.position = new Vector3(position.x, position.y, 0);
transform.localPosition = new Vector3(position.x, position.y, 0);
}
// 查找时调用
@ -55,4 +57,5 @@ namespace UguiToolkit.Editor
protected virtual void OnApplyData(T1 ui, T2 elementInfo) { }
}
}
}
#endif

View File

@ -1,13 +1,46 @@
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UguiToolkit.Editor;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class ImageEntity : BaseEntity<Image, LayoutInfo.ImageInfo>
{
private Image m_previewImage;
protected override void OnApplyData(Image ui, LayoutInfo.ImageInfo elementInfo)
{
}
public void InitPreviewImage() {
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))
{
byte[] fileData = System.IO.File.ReadAllBytes(path);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(fileData); // ¼ÓÔØÍ¼Æ¬Êý¾Ýµ½Texture2D
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
m_previewImage.sprite = sprite;
m_previewImage.SetNativeSize();
}
else
{
Debug.LogError("File not found at path: " + path);
}
}
}
#endif

View File

@ -1,3 +1,4 @@
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UguiToolkit.Editor;
@ -12,4 +13,5 @@ public class TextEntity : BaseEntity<Text, LayoutInfo.TextInfo>
ui.fontSize = (int)elementInfo.size;
ui.color = elementInfo.color;
}
}
}
#endif

View File

@ -1,4 +1,5 @@

#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEngine;
using System.IO;
@ -52,4 +53,5 @@ namespace UguiToolkit.Editor
Debug.Log("cmd error : " + error);
}
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using UnityEngine;
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.IO;
namespace UguiToolkit.Editor
@ -46,4 +47,5 @@ namespace UguiToolkit.Editor
G.cache = cacheScriptObject;
}
}
}
}
#endif

View File

@ -1,4 +1,4 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using System;
using Newtonsoft.Json;
@ -113,3 +113,5 @@ namespace UguiToolkit.Editor
public string font;
}
}
#endif

View File

@ -1,4 +1,5 @@
using UguiToolkit.Editor.Windows;
#if UNITY_EDITOR
using UguiToolkit.Editor.Windows;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
@ -32,4 +33,5 @@ namespace UguiToolkit.Editor
PanelCacheWindow.CloseWindow();
}
}
}
}
#endif

View File

@ -1,20 +0,0 @@
using System;
using UnityEngine;
namespace UguiToolkit
{
public class ActorManager : MonoBehaviour, IManager
{
private void Start()
{
// 初始化所有ImageActor
}
private void Update()
{
// 检测Image是否被选中并长按2秒进入选中状态
// 选中状态下每0.3秒检测
}
}
}

View File

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

View File

@ -1,10 +1,46 @@
//using System;
//using UnityEngine;
#if UNITY_EDITOR
using UnityEngine;
//namespace UguiToolkit
//{
// public class EntityManager : MonoBehaviour, IManager
// {
// private PanelCache panelCache;
// }
//}
namespace UguiToolkit.Editor
{
public class EntityManager : MonoBehaviour, IManager
{
private PanelCache m_panelCache;
private Transform entityRoot;
public void InitAllEntity(PanelCache panelCache)
{
this.m_panelCache = panelCache;
// 创建所有实例
CreateAllEntity();
}
private void CreateAllEntity()
{
if (this.m_panelCache == null) return;
var go = new GameObject();
entityRoot = go.transform;
entityRoot.SetParent(transform);
entityRoot.localPosition = Vector3.zero;
entityRoot.localRotation = Quaternion.identity;
entityRoot.localScale = Vector3.one;
// Image
foreach (var elementInfo in m_panelCache.GetLayoutElementInfos<LayoutInfo.ImageInfo>())
{
go = new GameObject();
var entity = go.AddComponent<ImageEntity>();
entity.transform.SetParent(entityRoot);
entity.transform.SetSiblingIndex(0);
entity.SetData(elementInfo);
entity.InitPreviewImage();
}
// Text
}
}
}
#endif

View File

@ -1,4 +1,6 @@
using UguiToolkit.Editor;
#if UNITY_EDITOR
using UguiToolkit.Editor;
using UnityEditor;
namespace UguiToolkit
@ -28,4 +30,6 @@ namespace UguiToolkit
AssetDatabase.SaveAssetIfDirty(cache);
}
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System;
#if UNITY_EDITOR
using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
@ -6,7 +7,6 @@ using System.Collections.Generic;
namespace UguiToolkit
{
[ExecuteAlways]
public class StageManager : MonoBehaviour
{
private Scene m_scene;
@ -35,6 +35,10 @@ namespace UguiToolkit
var go = new GameObject();
go.name = "StageManager";
go.transform.parent = prefabContentsRoot.transform;
go.transform.localPosition = Vector3.zero;
go.transform.localRotation= Quaternion.identity;
go.transform.localScale = Vector3.one;
m_instance = go.AddComponent<StageManager>();
m_instance.m_scene = scene;
m_instance.m_prefabContentsRoot = prefabContentsRoot;
@ -64,4 +68,6 @@ namespace UguiToolkit
public interface IManager {
}
}
#endif

View File

@ -1,3 +1,4 @@
#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
@ -8,8 +9,10 @@ using static UguiToolkit.Editor.LayoutInfo;
namespace UguiToolkit.Editor
{
[Serializable]
public class CacheScriptObject : SerializedScriptableObject
{
[SerializeField]
public Dictionary<GameObject, PanelCache> panelCaches = new();
}
@ -41,10 +44,12 @@ namespace UguiToolkit.Editor
// 通过cmd计算获得 图片路径(源图) -> 旋转缩放信息(效果图)
[SerializeField]
public Dictionary<string, List<RotScaleInfoItem>> rotScaleInfos = new();
[SerializeField]
public LayoutInfo layoutInfo;
public IEnumerator<T> GetLayoutElementInfos<T>() where T : LayoutInfo.ElementInfo
public IEnumerable<T> GetLayoutElementInfos<T>() where T : LayoutInfo.ElementInfo
{
if (layoutInfo == null) yield break;
@ -72,8 +77,11 @@ namespace UguiToolkit.Editor
[Serializable]
public class LayoutInfo: IEnumerable<ElementInfo>
{
[SerializeField]
private List<ElementInfo> m_elementInfos;
[SerializeField]
private float m_w;
[SerializeField]
private float m_h;
public float W => m_w;
@ -160,4 +168,5 @@ namespace UguiToolkit.Editor
public float2 scale;
}
}
#endif

View File

@ -1,4 +1,5 @@
using Sirenix.OdinInspector;
#if UNITY_EDITOR
using Sirenix.OdinInspector;
using UnityEngine;
namespace UguiToolkit.Editor
@ -8,4 +9,5 @@ namespace UguiToolkit.Editor
[LabelText("ui预制体存放的路径"), FolderPath]
public string prefabForUIDirPath;
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
@ -41,7 +42,8 @@ namespace UguiToolkit.Editor.Windows
GlobalManager.Instance.SaveCache();
var stageManager = UguiToolkit.StageManager.CreateStageManager(m_prefabStage.scene, m_prefabStage.prefabContentsRoot);
var actorManager = stageManager.CreateSubManager<ActorManager>();
var entityManager = stageManager.CreateSubManager<EntityManager>();
entityManager.InitAllEntity(panelCache);
CloseWindow();
}
@ -58,6 +60,16 @@ namespace UguiToolkit.Editor.Windows
{
var jsonData = reader.ReadToEnd();
var layoutInfo = layoutParser.Parser(jsonData);
// 对img路径进行修正
foreach (var elementInfo in layoutInfo)
{
var imgInfo = elementInfo as LayoutInfo.ImageInfo;
if (imgInfo != null)
{
imgInfo.imgPath = System.IO.Path.Join(panelCache.TargetImgDirPath, imgInfo.imgPath) + ".png";
}
}
panelCache.layoutInfo = layoutInfo;
}
@ -127,4 +139,5 @@ namespace UguiToolkit.Editor.Windows
}
}
}
}
#endif

View File

@ -5,7 +5,9 @@
"GUID:d8b63aba1907145bea998dd612889d6b"
],
"includePlatforms": [
"Editor"
"Editor",
"WindowsStandalone32",
"WindowsStandalone64"
],
"excludePlatforms": [],
"allowUnsafeCode": false,