FairyGUI生成插件,具体使用方法查阅FairyGUI官方文档:FairyGUI 代码生成插件

使用书写方式:

using ETModel;
using FairyGUI;namespace ETHotfix
{/// <summary>/// UI类型/// </summary>public partial class UIIdType{public const string Login = "Login";}/// <summary>/// 必须具有UIFactory特性以及继承IUIFactory/// </summary>[UIFactory(UIIdType.Login)]class UILoginEvent : IUIFactory{public FUI Create(string type){//创建UIGObject gObject = UIPackage.CreateObject(UI_Login.UIPackageName, UI_Login.UIResName);var result = ComponentFactory.Create<UI_Login, GObject>(gObject);result.AddComponent<LoginComponent>();result.MakeFullScreen();return result;}public void Remove(string type){//释放UI}}public class LoginComponent:Component{/// <summary>/// 持有UI字段方便修改/// </summary>public UI_Login ui;}[ObjectSystem]public class LoginComponentAwakeSystem : AwakeSystem<LoginComponent>{public override void Awake(LoginComponent self){self.ui = self.GetParent<UI_Login>();self.ui.m_account.text = "666";self.ui.m_login.onClick.Add(()=> {});//初始化UI界面等}}[Event(EventIdType.GameStartEvent)]public class GameStartEvent : AEvent{public override void Run(){//创建登录界面Game.Scene.GetComponent<FUIComponent>().Create(UIIdType.Login);}}
}

添加FairyGUI库到Assets/ThirdParty

把 Assets/Hotfix/Init.cs 的 Game.Scene.AddComponent<UIComponent>(); 注释掉,并添加

             Game.Scene.AddComponent<FUIComponent>();Game.Scene.AddComponent<FUIPackageComponent>();

把 Assets/Hotfix/Module/UI 文件夹删除

把 Assets/Hotfix/Module/Demo 删除

Assets/Hotfix/Base/Helper/AssetBundleHelper.cs 修改为

using System.Collections.Generic;namespace ETHotfix
{public static class AssetBundleHelper{public static readonly Dictionary<int, string> IntToStringDict = new Dictionary<int, string>();public static readonly Dictionary<string, string> StringToABDict = new Dictionary<string, string>();public static readonly Dictionary<string, string> BundleNameToLowerDict = new Dictionary<string, string>(){{ "StreamingAssets", "StreamingAssets" }};public static string IntToString(int value){string result;if (IntToStringDict.TryGetValue(value, out result)){return result;}result = value.ToString();IntToStringDict[value] = result;return result;}public static string StringToAB(string value){string result;if (StringToABDict.TryGetValue(value, out result)){return result;}result = value + ".unity3d";StringToABDict[value] = result;return result;}public static string IntToAB(int value){return StringToAB(IntToString(value));}public static string BundleNameToLower(string value){string result;if (BundleNameToLowerDict.TryGetValue(value, out result)){return result;}result = value.ToLower();BundleNameToLowerDict[value] = result;return result;}public static string GetBundleNameById(string id, EntityType entityType = EntityType.None){string subString = id.Substring(0, 3);switch (subString[0]){case '1':case '2':case '3':return "3000";case '4':case '5':case '6':case '8':return subString;case '9':return "900";}return subString;}}
}

ResourcesComponent.cs 添加

  public AssetBundle GetAssetBundle(string abName){ABInfo abInfo;if (!bundles.TryGetValue(AssetBundleHelper.BundleNameToLower(abName), out abInfo)){throw new Exception($"not found bundle: {abName}");}return abInfo.AssetBundle;}

下面引用自DCET

using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using ETModel;
namespace ETHotfix
{[ObjectSystem]public class FUIAwakeSystem : AwakeSystem<FUI, GObject>{public override void Awake(FUI self, GObject gObject){self.GObject = gObject;}}public class FUI : Entity{public GObject GObject;public string Name{get{if(GObject == null){return string.Empty;}return GObject.name;}set{if (GObject == null){return;}GObject.name = value;}}public bool Visible{get{if (GObject == null){return false;}return GObject.visible;}set{if (GObject == null){return;}GObject.visible = value;}}public bool IsComponent{get{return GObject is GComponent;}}public bool IsRoot{get{return GObject is GRoot;}}public bool IsEmpty{get{return GObject == null;}}private Dictionary<string, FUI> fuiChildren = new Dictionary<string, FUI>();protected bool isFromFGUIPool = false;public override void Dispose(){if (IsDisposed){return;}base.Dispose();// 从父亲中删除自己GetParent<FUI>()?.RemoveNoDispose(Name);// 删除所有的孩子foreach (FUI ui in fuiChildren.Values.ToArray()){ui.Dispose();}fuiChildren.Clear();// 删除自己的UIif (!IsRoot && !isFromFGUIPool){GObject.Dispose();}GObject = null;isFromFGUIPool = false;}public void Add(FUI ui, bool asChildGObject){if(ui == null || ui.IsEmpty){throw new Exception($"ui can not be empty");}if (string.IsNullOrWhiteSpace(ui.Name)){throw new Exception($"ui.Name can not be empty");}if (fuiChildren.ContainsKey(ui.Name)){throw new Exception($"ui.Name({ui.Name}) already exist");}fuiChildren.Add(ui.Name, ui);if (IsComponent && asChildGObject){GObject.asCom.AddChild(ui.GObject);}ui.Parent = this;}public void MakeFullScreen(){GObject?.asCom?.MakeFullScreen();}public void Remove(string name){if (IsDisposed){return;}FUI ui;if (fuiChildren.TryGetValue(name, out ui)){fuiChildren.Remove(name);if (ui != null){if (IsComponent){GObject.asCom.RemoveChild(ui.GObject, false);}ui.Dispose();}}}/// <summary>/// 一般情况不要使用此方法,如需使用,需要自行管理返回值的FUI的释放。/// </summary>public FUI RemoveNoDispose(string name){if (IsDisposed){return null;}FUI ui;if (fuiChildren.TryGetValue(name, out ui)){fuiChildren.Remove(name);if (ui != null){if(IsComponent){GObject.asCom.RemoveChild(ui.GObject, false);}ui.Parent = null;}}return ui;}public void RemoveChildren(){foreach (var child in fuiChildren.Values.ToArray()){child.Dispose();}fuiChildren.Clear();}public FUI Get(string name){FUI child;if (fuiChildren.TryGetValue(name, out child)){return child;}return null;}public FUI[] GetAll(){return fuiChildren.Values.ToArray();}}
}
using FairyGUI;
using System;
using System.Collections.Generic;
using ETModel;
namespace ETHotfix
{[ObjectSystem]public class FUIComponentLoadSystem : LoadSystem<FUIComponent>{public override void Load(FUIComponent self){self.Load();}}[ObjectSystem]public class FUIComponentAwakeSystem : AwakeSystem<FUIComponent>{public override void Awake(FUIComponent self){self.Root = ComponentFactory.Create<FUI, GObject>( GRoot.inst);self.Awake();}}/// <summary>/// 管理所有顶层UI, 顶层UI都是GRoot的孩子/// </summary>public class FUIComponent: Component{public FUI Root;private readonly Dictionary<string, IUIFactory> UiTypes = new Dictionary<string, IUIFactory>();public override void Dispose(){if (IsDisposed){return;}base.Dispose();UiTypes.Clear();Root?.Dispose();Root = null;}public void Awake(){this.Load();}public void Load(){UiTypes.Clear();List<Type> types = Game.EventSystem.GetTypes();foreach (Type type in types){object[] attrs = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);if (attrs.Length == 0){continue;}UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;if (UiTypes.ContainsKey(attribute.Type)){Log.Debug($"已经存在同类UI Factory: {attribute.Type}");throw new Exception($"已经存在同类UI Factory: {attribute.Type}");}object o = Activator.CreateInstance(type);IUIFactory factory = o as IUIFactory;if (factory == null){Log.Error(string.Format("{0} 没有继承 IUIFactory", o.GetType().FullName));continue;}this.UiTypes.Add(attribute.Type, factory);}}public FUI Create(string type){try{FUI ui = UiTypes[type].Create(type);ui.Name = type;Add(ui, true);return ui;}catch (Exception e){throw new Exception($"{type} UI 错误: {e}");}}public void Add(FUI ui, bool asChildGObject){Root?.Add(ui, asChildGObject);}public void Remove(string name){UiTypes[name].Remove(name);Root?.Remove(name);}public FUI Get(string name){return Root?.Get(name);}public FUI[] GetAll(){return Root?.GetAll();}public void Clear(){var childrens = GetAll();if(childrens != null){foreach (var fui in childrens){Remove(fui.Name);}}}}
}
using FairyGUI;
using System;namespace ETHotfix
{public class FUIPackage : IDisposable{private UIPackage uiPackage;public FUIPackage(string resPath){uiPackage = UIPackage.AddPackage(resPath);}public void Dispose(){if(uiPackage != null){UIPackage.RemovePackage(uiPackage.name);}}}
}
using FairyGUI;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using ETModel;namespace ETHotfix
{/// <summary>/// 管理所有UI Package/// </summary>public class FUIPackageComponent : Entity{public const string FUI_PACKAGE_DIR = "Assets/Bundles/FUI";private readonly Dictionary<string, UIPackage> packages = new Dictionary<string, UIPackage>();public void AddPackage(string type){if (Define.IsEditorMode){UIPackage uiPackage = UIPackage.AddPackage($"{FUI_PACKAGE_DIR}/{type}");packages.Add(type, uiPackage);}else{string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui");string uiBundleResName = AssetBundleHelper.StringToAB(type);ResourcesComponent resourcesComponent = ETModel. Game.Scene.GetComponent<ResourcesComponent>();resourcesComponent.LoadBundle(uiBundleDesName);resourcesComponent.LoadBundle(uiBundleResName);AssetBundle desAssetBundle = resourcesComponent.GetAssetBundle(uiBundleDesName);AssetBundle resAssetBundle = resourcesComponent.GetAssetBundle(uiBundleResName);UIPackage uiPackage = UIPackage.AddPackage(desAssetBundle, resAssetBundle);packages.Add(type, uiPackage);}}public async Task AddPackageAsync(string type){if (Define.IsEditorMode){await Task.CompletedTask;UIPackage uiPackage = UIPackage.AddPackage($"{FUI_PACKAGE_DIR}/{type}");packages.Add(type, uiPackage);}else{string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui");string uiBundleResName = AssetBundleHelper.StringToAB(type);ResourcesComponent resourcesComponent =ETModel. Game.Scene.GetComponent<ResourcesComponent>();await resourcesComponent.LoadBundleAsync(uiBundleDesName);await resourcesComponent.LoadBundleAsync(uiBundleResName);AssetBundle desAssetBundle = resourcesComponent.GetAssetBundle(uiBundleDesName);AssetBundle resAssetBundle = resourcesComponent.GetAssetBundle(uiBundleResName);UIPackage uiPackage = UIPackage.AddPackage(desAssetBundle, resAssetBundle);packages.Add(type, uiPackage);}}public void RemovePackage(string type){UIPackage package;if(packages.TryGetValue(type, out package)){var p = UIPackage.GetByName(package.name);if (p != null){UIPackage.RemovePackage(package.name);}packages.Remove(package.name);}if (!Define.IsEditorMode){string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui");string uiBundleResName = AssetBundleHelper.StringToAB(type);ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(uiBundleDesName);ETModel.Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle(uiBundleResName);}}}
}
using System.Collections.Generic;namespace ETHotfix
{/// <summary>/// UI栈/// </summary>public class FUIStackComponent: Entity{private readonly Stack<FUI> uis = new Stack<FUI>();public int Count{get{return uis.Count;}}public void Push(FUI fui){uis.Peek().Visible = false;uis.Push(fui);}public void Pop(){FUI fui = uis.Pop();fui.Dispose();if (uis.Count > 0){uis.Peek().Visible = true;}}public void Clear(){while (uis.Count > 0){FUI fui = uis.Pop();fui.Dispose();}}}
}
using FairyGUI;
using UnityEngine;namespace ETHotfix
{public static class GComponentHelper{/// <summary>/// 获取组件包围盒/// </summary>/// <returns>x:最小值 y:最小值 w:宽 h:高</returns>public static Rect GetRect(GComponent gcom){if(gcom == null){return Rect.zero;}float ax = 0f;float ay = 0f;float aw = 0f;float ah = 0f;GObject[] childs = new GObject[4];GObject maxDisschild = null;float diss = 0f;int cnt = gcom.numChildren;if (gcom.numChildren > 0){ax = int.MaxValue;ay = int.MaxValue;float ar = int.MinValue;float ab = int.MinValue;float tmp;for (int i = 0; i < cnt; ++i){GObject child = gcom.GetChildAt(i);tmp = child.xMin;if (tmp < ax){ax = tmp;childs[0] = child;}tmp = child.yMin;if (tmp < ay){ay = tmp;childs[1] = child;}tmp = child.xMin + child.actualWidth;if (tmp > ar){ar = tmp;childs[2] = child;}tmp = child.yMin + child.actualWidth;if (tmp > ab){ab = tmp;childs[3] = child;}                   }for (int i = 0; i < childs.Length; ++i){if (diss <= childRadiusDiss(childs[i])){diss = childRadiusDiss(childs[i]);maxDisschild = childs[i];}}var ratio = Mathf.Max(maxDisschild.actualWidth, maxDisschild.height) / Mathf.Min(maxDisschild.actualWidth, maxDisschild.actualHeight);diss = diss * ratio;aw = ar - ax + diss;ah = ab - ay + diss;ax = ax - diss * 0.5f;ay = ay - diss * 0.5f;return new Rect(ax, ay, aw, ah);}return new Rect(gcom.x, gcom.y, gcom.actualWidth, gcom.actualHeight);}private static float childRadiusDiss(GObject child){var radius = Mathf.Sqrt(child.actualWidth * child.actualWidth + child.actualHeight * child.actualHeight) * 0.5f;var diss = radius * Mathf.Abs(Mathf.Sin(child.rotation));return diss;}}
}
using FairyGUI;
using System.Collections.Generic;namespace ETHotfix
{public static class GObjectHelper{private static Dictionary<GObject, FUI> keyValuePairs = new Dictionary<GObject, FUI>();public static T Get<T>(this GObject self) where T : FUI{if (self != null && keyValuePairs.ContainsKey(self)){return keyValuePairs[self] as T;}return default(T);}public static void Add(this GObject self, FUI fui){if (self != null && fui != null){keyValuePairs[self] = fui;}}public static FUI Remove(this GObject self){if (self != null && keyValuePairs.ContainsKey(self)){var result = keyValuePairs[self];keyValuePairs.Remove(self);return result;}return null;}}
}
using UnityEngine;namespace ETHotfix
{public interface IUIFactory{FUI Create(string type);void Remove(string type);}
}

2021/4/26GList

 public class GListComponent : Entity{public GList list;System.Action<FUI> onRenderer;private readonly Dictionary<GObject, FUI> dict = new Dictionary<GObject, FUI>();public void Awake(){var fui = this.GetParent<FUI>();this.list = fui.GObject.asList;this.list.itemRenderer = OnitemRenderer;}public void SetItemNumber(int newNumber){list.numItems = newNumber;}public FUI AddItem(GObject item){if (dict.ContainsKey(item))return dict[item];list.AddChild(item);var ui = EntityFactory.CreateWithParent<FUI, GObject>(this, item);dict.Add(item, ui);return ui;}public void RemoveItem(GObject item){if (!dict.ContainsKey(item))return;var ui = dict[item];dict.Remove(item);ui.Dispose();}public void SetVirtual(){list.SetVirtual();}public void ItemRenderer(System.Action<FUI> renderer){onRenderer = renderer;}void OnitemRenderer(int index, GObject item){if (!dict.TryGetValue(item, out FUI ui)){ui = EntityFactory.CreateWithParent<FUI, GObject>(this, item);dict.Add(item, ui);}onRenderer?.Invoke(ui);}public override void Dispose(){if (this.IsDisposed)return;foreach(var item in dict.Values){item.Dispose();}dict.Clear();base.Dispose();}}[ObjectSystem]public class GListComponentAwakeSystem : AwakeSystem<GListComponent>{public override void Awake(GListComponent self){self.Awake();}}

GListComponent参考使用方式

using FairyGUI;namespace ET
{[UI(UIType.Login, typeof(UI_LoginComponent))]public class LoginConfig : IUIConfig{void IUIConfig.OnCreate(FUI ui){var uiComponent = (ui as UI_LoginComponent);var loginComponent = ui.AddComponent<LoginComponent>();//在实际使用中,list对象可能不仅仅只需要添加一个组件//所以我创建一个fui对list进行管理var listfui = EntityFactory.CreateWithParent<FUI, GObject>(ui, uiComponent.m_list);loginComponent.list = listfui.AddComponent<GListComponent>();}void IUIConfig.OnRemove(FUI ui){}}
}

ET5.0 UGUI替换为FairyGUI相关推荐

  1. R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:使用na_if()函数将0值替换为NA值、负收入替换为NA值

    R语言dplyr包的mutate函数将列添加到dataframe中或者修改现有的数据列:使用na_if()函数将0值替换为NA值.负收入替换为NA值 目录

  2. 给定n个整数(0-100),其中0可以替换成任意其他数字,如数组{98,99,100,1,2,3} 视为连续,要求判断这n个整数是否连续?

    该题目为某IT公司的笔试题目 题目: 给定n个整数(0-100),其中0可以替换成任意其他数字,如数组{98,99,100,1,2,3} 视为连续,要求判断这n个整数是否连续?若是连续数组,返回tru ...

  3. ET5.0学习-1启动官方Demo

    目录 环境配置 第一步 服务端启动 第二部 客户端启动 环境配置 window系统:Windows 10 专业版 Unity:2019.3.0f 下载地址:https://download.unity ...

  4. SteamVR2.0 UGUI射线交互模拟

    SteamVR2.0 UGUI射线交互模拟 文章目录 SteamVR2.0 UGUI射线交互模拟 前言 一.开始前要做的准备 1.在项目开始前应当把环境给搭建好,在Unity项目中把SteamVR导入 ...

  5. 【Unity】实现镜头虚化模糊效果(Blur):camera模糊,UGUI模糊,FairyGUI模糊效果

    前言 起因是在一个FairyGUI项目里,有个项目需求要求弹出框的背景是虚化模糊效果,包括背景后的UI模糊和场景模糊,于是有了这一次unity实现模糊效果的测试记录. 先贴一下测试使用的unity工程 ...

  6. NET Core 3.0 AutoFac替换内置DI的新姿势

    .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原来在NET Core 2.1时候,AutoFac返回一个 IServ ...

  7. vue3.0树形表格插件vue-table-with-tree-grid(vue.2.0)替换 vxe-table(vue3.0)

    由于看黑马,视频,那个vue-table-with-tree-grid在vue3 不能用,所以用这个替换下 vxe-table官网 https://vxetable.cn/#/table/start/ ...

  8. 一个有趣的实验:用0.1f 替换 0,性能提升 7 倍!

    点击关注上方"视学算法",设为"置顶或星标",第一时间送达技术干货. 本文来源:http://cenalulu.github.io/linux/about-de ...

  9. pytorch 和 tensorflow2.0 方法替换

    Embedding初始化 pytorch: Embedding() tf2.0: random.normal() # 验证均值和方差 def confirm(weight):mean = np.sum ...

最新文章

  1. 一个古老而优雅的电子线路
  2. 多终端编程需要注意的那些事
  3. java json的使用,java中json的使用
  4. DFS与BFS的总结
  5. CEO 赠书 | 对失败的学习,决定了你成功的速度
  6. BugKuCTF 杂项 隐写
  7. IDEA开发vue.js卡死问题
  8. ZooKeeper安装,部署
  9. python3软件怎么使用_python3怎么使用pip
  10. HTML5 API详解(8):worker多线程教你如何避免页面卡死
  11. (转) 淘淘商城系列——Redis集群的搭建
  12. 如何避免可怕的中年危机?看完这篇彻底明白了
  13. 3.4.1 - Numeric Types
  14. 数据结构:循环链表实现约瑟夫环
  15. 在VS2010中使用Git管理源代码
  16. 电赛练习之旋转倒立摆
  17. Orcad Capture CIS 绘制原理图库
  18. QT 信号toggled triggered区别
  19. ❤️数据可视化❤️:基于Echarts + GeoJson实现的地图视觉映射散点(气泡)组件【7】 - 海南省
  20. webdav服务器文件大小限制,WebDAV服务器

热门文章

  1. PID算法控制平衡小车直立
  2. 身价10亿的打工皇帝-唐骏
  3. 话筒性能测试软件,动圈式话筒性能的检测
  4. C语言猜字游戏---翁凯
  5. 获取XBOX360体感大冒险照片
  6. p2psearcher绿色版使用方法
  7. python带你制作一个gequ下载器,海量gequ免费听
  8. 【技术分享】单片机模拟NS手柄 半自动完成太鼓达人曲目
  9. android adb没反应,Android adb无法发现设备处理方法
  10. Airpods Pro闪红灯是什么意思?