Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
add function: create refer picture when open view
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhaopen committed Jul 18, 2018
1 parent 3ea6e29 commit fd91bdf
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 20 deletions.
19 changes: 17 additions & 2 deletions Assets/UIEditor/Common/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,28 @@ static public void AddPriorityMenu()
//增加UI控件菜单
static public void AddUIMenu()
{
AddItem("添加控件/Empty", false, UIEditorHelper.CreateEmptyObj, null);
AddItem("添加控件/Image", false, UIEditorHelper.CreateImageObj, null);
AddItem("添加控件/RawImage", false, UIEditorHelper.CreateRawImageObj, null);
AddItem("添加控件/Button", false, UIEditorHelper.CreateButtonObj, null);
AddItem("添加控件/Text", false, UIEditorHelper.CreateTextObj, null);
}

//增加显示隐藏菜单
static public void AddShowOrHideMenu()
//增加UI组件菜单
static public void AddUIComponentMenu()
{
AddItem("添加组件/Image", false, UIEditorHelper.AddImageComponent, null);
//AddItem("添加组件/RawImage", false, UIEditorHelper.CreateRawImageObj, null);
//AddItem("添加组件/Button", false, UIEditorHelper.CreateButtonObj, null);
//AddItem("添加组件/Text", false, UIEditorHelper.CreateTextObj, null);
AddItem("添加组件/HLayout", false, UIEditorHelper.AddHorizontalLayoutComponent, null);
AddItem("添加组件/VLayout", false, UIEditorHelper.AddVerticalLayoutComponent, null);
AddItem("添加组件/GridLayout", false, UIEditorHelper.AddGridLayoutGroupComponent, null);

}

//增加显示隐藏菜单
static public void AddShowOrHideMenu()
{
bool hasHideWidget = false;
foreach (var item in Selection.gameObjects)
Expand Down Expand Up @@ -114,6 +128,7 @@ static public void AddCommonItems(GameObject[] targets)
if (targets.Length == 1)
{
AddUIMenu();
AddUIComponentMenu();
AddPriorityMenu();

if (UIEditorHelper.IsNodeCanDivide(targets[0]))
Expand Down
21 changes: 21 additions & 0 deletions Assets/UIEditor/Common/Decorate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Decorate : MonoBehaviour
[HideInInspector]
private Image _image;

Vector3 _lastPos = new Vector3(-1, -1);
Vector2 _lastSize = Vector2.zero;

public string SprPath
{
get { return spr_path; }
Expand All @@ -31,6 +34,7 @@ public void LoadSpr(string path)
_image.sprite = UIEditorHelper.LoadSpriteInLocal(path);
_image.SetNativeSize();
gameObject.name = CommonHelper.GetFileNameByPath(path);
//Debug.Log("_image.sprite :" + (_image.sprite != null).ToString());
}
}

Expand All @@ -44,6 +48,23 @@ protected void InitComponent()
if (_image == null)
_image = GetComponent<Image>();
}

public bool IsChangedTrans()
{
RectTransform curTrans = transform as RectTransform;
if (curTrans.localPosition == _lastPos && curTrans.sizeDelta == _lastSize)
return false;
else
return true;
}

public void SaveTrans()
{
RectTransform rectTrans = transform as RectTransform;
_lastPos = rectTrans.localPosition;
_lastSize = rectTrans.sizeDelta;
}

}
}
#endif
145 changes: 144 additions & 1 deletion Assets/UIEditor/Common/LayoutInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#if UNITY_EDITOR
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace U3DExtends
Expand All @@ -10,6 +14,22 @@ public class LayoutInfo : MonoBehaviour
[SerializeField]
private string _layoutPath = string.Empty;

Vector3 _lastRealLayoutPos = new Vector3(-1, -1);
Vector2 _lastRealLayoutSize = Vector2.zero;
const string RealPosStartStr = "RealLayoutPosStart ";
const string RealPosEndStr = " RealLayoutPosEnd\n";

static string configPath = string.Empty;
static string ConfigPath
{
get
{
if (configPath == string.Empty)
configPath = Application.temporaryCachePath + "/Decorates";
return configPath;
}
}

public string LayoutPath
{
get
Expand All @@ -23,6 +43,129 @@ public string LayoutPath
}
}

//本来坐标或大小变更时也需要再保存一下,但是需要监听pos size等变更又要io保存,怕太多参照图时影响性能,所以还是界面保存时再一起保存吧
public bool SaveToConfigFile()
{
string select_path = FileUtil.GetProjectRelativePath(LayoutPath);
string layout_path_md5 = UIEditorHelper.GenMD5String(select_path);
RectTransform real_layout = UIEditorHelper.GetRealLayout(gameObject) as RectTransform;//先拿到真实的界面prefab
if (select_path == "" || real_layout == null)
{
//界面还未保存,等保存时再调用本函数
return false;
}
RectTransform curTrans = transform as RectTransform;
bool hadDecorateTransChanged = false;
bool hadTransChanged = true;
if (real_layout.localPosition == _lastRealLayoutPos && real_layout.sizeDelta == _lastRealLayoutSize)
hadTransChanged = false;
_lastRealLayoutPos = real_layout.localPosition;
_lastRealLayoutSize = real_layout.sizeDelta;
if (!Directory.Exists(ConfigPath))
Directory.CreateDirectory(ConfigPath);
string savePath = ConfigPath + "/" + layout_path_md5 + ".txt";
StringBuilder content = new StringBuilder();
content.Append(RealPosStartStr);
content.Append(real_layout.localPosition.x.ToString());
content.Append(' ');
content.Append(real_layout.localPosition.y.ToString());
content.Append(RealPosEndStr);
Decorate[] decorates = transform.GetComponentsInChildren<Decorate>();
for (int i = 0; i < decorates.Length; i++)
{
RectTransform rectTrans = decorates[i].GetComponent<RectTransform>();
if (rectTrans != null)
{
content.Append(decorates[i].SprPath);
content.Append('#');
content.Append(rectTrans.localPosition.x.ToString());
content.Append(' ');
content.Append(rectTrans.localPosition.y.ToString());
content.Append('#');
content.Append(rectTrans.sizeDelta.x.ToString());
content.Append(' ');
content.Append(rectTrans.sizeDelta.y.ToString());
content.Append('*');//分隔不同的参照图
if (decorates[i].IsChangedTrans())
{
decorates[i].SaveTrans();
hadDecorateTransChanged = true;
}
}
}
if (hadTransChanged || hadDecorateTransChanged)
{
if (content[content.Length - 1] == '*')
content.Remove(content.Length - 1, 1);//删掉最后一个分隔符
File.WriteAllText(savePath, content.ToString());
return true;
}
//当真实界面的坐标和参照图的变换没变的话就不需要保存了
return false;
}

//打开界面时,从项目临时文件夹找到对应界面的参照图配置,然后生成参照图
public void ApplyConfig(string view_path)
{
string layout_path_md5 = UIEditorHelper.GenMD5String(view_path);
string confighFilePath = ConfigPath + "/" + layout_path_md5 + ".txt";
if (!File.Exists(confighFilePath))
return;
string content = File.ReadAllText(confighFilePath);
int pos_end_index = content.IndexOf(RealPosEndStr);
if (pos_end_index == -1)
{
Debug.Log("cannot find real layout pos config on ApplyConfig : " + view_path);
return;
}
string real_layout_pos_str = content.Substring(RealPosStartStr.Length, pos_end_index - RealPosStartStr.Length);
string[] pos_cfg = real_layout_pos_str.Split(' ');
if (pos_cfg.Length == 2)
{
RectTransform real_layout = UIEditorHelper.GetRealLayout(gameObject) as RectTransform;//先拿到真实的界面prefab
if (real_layout == null)
{
Debug.Log("cannot find real layout on ApplyConfig : " + view_path);
return;
}
real_layout.localPosition = new Vector3(float.Parse(pos_cfg[0]), float.Parse(pos_cfg[1]), real_layout.localPosition.z);
}
else
{
Debug.Log("cannot find real layout pos xy config on ApplyConfig : " + view_path);
return;
}
content = content.Substring(pos_end_index + RealPosEndStr.Length);
if (content == "")
return;//有些界面没参考图也是正常的,直接返回
string[] decorate_cfgs = content.Split('*');
for (int i = 0; i < decorate_cfgs.Length; i++)
{
string[] cfgs = decorate_cfgs[i].Split('#');
if (cfgs.Length == 3)
{
Decorate decor = UIEditorHelper.CreateEmptyDecorate(transform);
decor.SprPath = cfgs[0];
RectTransform rectTrans = decor.GetComponent<RectTransform>();
if (rectTrans != null)
{
//IFormatter formatter = new BinaryFormatter();//使用序列化工具的话就可以保存多点信息,但实现复杂了,暂用简单的吧
string[] pos = cfgs[1].Split(' ');
if (pos.Length == 2)
rectTrans.localPosition = new Vector2(float.Parse(pos[0]), float.Parse(pos[1]));

string[] size = cfgs[2].Split(' ');
if (size.Length == 2)
rectTrans.sizeDelta = new Vector2(float.Parse(size[0]), float.Parse(size[1]));
}
}
else
{
Debug.Log("warning : detect a wrong decorate config file!");
return;
}
}
}
}
}
}
#endif
Loading

0 comments on commit fd91bdf

Please sign in to comment.