ET学习日志之更改FairyGUI生成模板

https://gitee.com/weifen/dcet_learning/tree/master

注意,使用最新的2020版本FairyGUI

更改后创建UI的方法

namespace DCET
{
	[Event(EventIdType.InitSceneStart)]
	public class InitSceneStart_CreateLogin : AEvent
	{
        public override void Run()
        {
            //var fui = FUILogin.CreateInstance(Game.Scene);

            默认将会以Id为Name,也可以自定义Name,方便查询和管理
            //fui.Name = FUILogin.UIResName;
            //fui.MakeFullScreen();
            //fui.AddComponent<FUILoginComponent>();

            //Game.Scene.GetComponent<FUIComponent>().Add(fui, true);

              FGUIGenerate.Create_Login();

        }
    }
}

将会生成的热更代码

/** This is an automatically generated class by FairyGUI. Please do not modify it. **/

using FairyGUI;
using System.Threading.Tasks;

namespace DCET
 {

	[ObjectSystem]
	public class FUILoginAwakeSystem : AwakeSystem<FUILogin, GObject> {

		public override void Awake(FUILogin self, GObject go) {
			self.Awake(go);
		}
	}

	public partial class FUILogin : FUI {
		public const string UIPackageName = "Hotfix";
		public const string UIResName = "Login";
		public const string UIName = "Hotfix.Login";
		public GComponent self;

		public Controller c1;
		public GImage bg;
		public GTextInput user;
		public GTextInput password;
		public GButton login;
		public GButton register;
		public static string URL = "ui://2w4fpdl4nil";

		public void Awake(GObject go) {
			if (go == null) {
				return;
			}
			GObject = go;
			Name = UIName;
			self = (GComponent)go;
			self.Add(this);
			var com = go.asCom;
			if (com != null) {
				this.c1 = com.GetController("c1");
				this.bg = (GImage)com.GetChild("bg");
				this.user = (GTextInput)com.GetChild("user");
				this.password = (GTextInput)com.GetChild("password");
				this.login = (GButton)com.GetChild("login");
				this.register = (GButton)com.GetChild("register");
				OnInitialization();
			}
		}

		partial void OnInitialization();

		public override void Dispose() {
			base.Dispose();
			self.Remove();
		}
	}
}
namespace DCET {
	public partial class FGUIGenerate {
		public static async void Create_Login() {
			await FGUIHelper.Add<FUILogin,FUILoginComponent>(FUILogin.UIPackageName, FUILogin.UIResName);
		}
		public static async Task<FUILoginComponent> Await_Create_Login() {
			return await FGUIHelper.Add<FUILogin,FUILoginComponent>(FUILogin.UIPackageName, FUILogin.UIResName);
		}
	}
}

冷更代码-------->它就是原生FairyGUI模板生成的样子,在DCET里面 冷更层只有一个背景UI, 所有UI都放在热更层

/** This is an automatically generated class by FairyGUI. Please do not modify it. **/

using FairyGUI;
using FairyGUI.Utils;

namespace DCETRuntime {
	public partial class FUILoading : GComponent {
		public GImage bg;
		public GTextField loadingText;
		public FUILoadingProgressBar loadingBar;
		public static string URL = "ui://1n4czlednil";

		public static FUILoading CreateInstance() {
			return (FUILoading) UIPackage.CreateObject("Runtime", "Loading");
		}

		public override void ConstructFromXML(XML xml) {
			base.ConstructFromXML(xml);
			this.bg = (GImage)this.GetChild("bg");
			this.loadingText = (GTextField)this.GetChild("loadingText");
			this.loadingBar = (FUILoadingProgressBar)this.GetChild("loadingBar");
			Oninitialization();
		}
		partial void Oninitialization();
	}
}

 

 

模板生成 lua文件,该文件放在  项目文件plugins文件夹下面,不是编辑器下面。。

function onPublish(handler)
    --fprint("Handling gen code in plugin")
    if not handler.genCode then return end
    handler.genCode = false --prevent default output

    if handler.pkg.name == "Runtime" then
        genRuntimeCode(handler)
    else
        genHotfixCode(handler) --do it myself
    end
end
function  genRuntimeCode( handler )
    local settings = handler.project:GetSettings("Publish").codeGeneration
    local codePkgName = handler:ToFilename(handler.pkg.name); --convert chinese to pinyin, remove special chars etc.
    local exportCodePath = handler.exportCodePath..'/'..codePkgName
    local namespaceName = codePkgName
    --local ns = 'fgui'
    local ns = ''
    
    if settings.packageName~=nil and settings.packageName~='' then
        namespaceName = settings.packageName..'.'..namespaceName;
    end

    --CollectClasses(stripeMemeber, stripeClass, fguiNamespace)
    local classes = handler:CollectClasses(settings.ignoreNoname, settings.ignoreNoname, ns)
    handler:SetupCodeFolder(exportCodePath, "ts") --check if target folder exists, and delete old files

    local getMemberByName = settings.getMemberByName

    local classCnt = classes.Count
    local writer = CodeWriter.new({ blockFromNewLine=false, usingTabs = true  })
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        local members = classInfo.members
        local references = classInfo.references
        writer:reset()

        --[[
        local refCount = references.Count
        if refCount>0 then
            for j=0,refCount-1 do
                local ref = references[j]
                writer:writeln('import %s from "./%s";', ref, ref)
            end
            writer:writeln()
        end
        ]]--

        writer:writeln('using FairyGUI;')
        writer:writeln('using FairyGUI.Utils;')
        writer:writeln()
        writer:writeln('namespace DCETRuntime')
        writer:startBlock()
        writer:writeln('public partial class %s : %s', classInfo.className, classInfo.superClassName)
        writer:startBlock()

        local memberCnt = members.Count
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            writer:writeln('public %s %s;', memberInfo.type,memberInfo.varName)
        end
        writer:writeln('public static string URL = "ui://%s%s";', handler.pkg.id, classInfo.classId)
        writer:writeln()

        writer:writeln('public static %s CreateInstance()', classInfo.className)
        writer:startBlock()
        writer:writeln('return (%s) UIPackage.CreateObject("%s", "%s");', classInfo.className, handler.pkg.name, classInfo.resName)
        writer:endBlock()
        writer:writeln()

        writer:writeln('public override void ConstructFromXML(XML xml)')
        writer:startBlock()
        writer:writeln('base.ConstructFromXML(xml);')
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            if memberInfo.group==0 then
                if getMemberByName then
                    writer:writeln('this.%s = (%s)this.GetChild("%s");', memberInfo.varName, memberInfo.type, memberInfo.name)
                else
                    writer:writeln('this.%s = (%s)this.GetChildAt(%s);', memberInfo.varName, memberInfo.type, memberInfo.index)
                end
            elseif memberInfo.group==1 then
                if getMemberByName then
                    writer:writeln('this.%s = this.GetController("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = this.GetControllerAt(%s);', memberInfo.varName, memberInfo.index)
                end
            else
                if getMemberByName then
                    writer:writeln('this.%s = this.GetTransition("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = this.GetTransitionAt(%s);', memberInfo.varName, memberInfo.index)
                end
            end
        end
        writer:writeln('Oninitialization();')
        writer:endBlock()
        writer:writeln('partial void Oninitialization();')

        writer:endBlock() --class

        writer:endBlock()--命名空间
        writer:save(exportCodePath..'/'..classInfo.className..'.cs')
    end

    writer:reset()

    local binderName = codePkgName..'Binder'
    --[[
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        writer:writeln('import %s from "./%s";', classInfo.className, classInfo.className)
    end
    ]]--

    writer:writeln('using FairyGUI;')
    writer:writeln('namespace DCETRuntime')
    writer:startBlock()
    writer:writeln()
    writer:writeln('public class %s', binderName)
    writer:startBlock()

    writer:writeln('public static void BindAll()')
    writer:startBlock()
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        writer:writeln('UIObjectFactory.SetPackageItemExtension(%s.URL,typeof( %s));', classInfo.className, classInfo.className)
    end
    writer:endBlock() --bindall

    writer:endBlock() --class
    writer:endBlock()--namespace
    writer:save(exportCodePath..'/'..binderName..'.cs')
end

-- this is copied from Editor-Install-Path/Resources/Data/StreamingAssets/Scripts
function genHotfixCode(handler) 
    local settings = handler.project:GetSettings("Publish").codeGeneration
    local codePkgName = handler:ToFilename(handler.pkg.name); --convert chinese to pinyin, remove special chars etc.
    local exportCodePath = handler.exportCodePath..'/'..codePkgName
    local namespaceName = codePkgName
    local ns = 'fgui'
    
    if settings.packageName~=nil and settings.packageName~='' then
        namespaceName = settings.packageName..'.'..namespaceName;
    end

    --CollectClasses(stripeMemeber, stripeClass, fguiNamespace)
    local classes = handler:CollectClasses(settings.ignoreNoname, settings.ignoreNoname, ns)
    handler:SetupCodeFolder(exportCodePath, "ts") --check if target folder exists, and delete old files

    local getMemberByName = settings.getMemberByName

    local classCnt = classes.Count
    local writer = CodeWriter.new({ blockFromNewLine=false, usingTabs = true  })
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        local members = classInfo.members
        local references = classInfo.references
        writer:reset()

        local refCount = references.Count
        -- if refCount>0 then
        --     for j=0,refCount-1 do
        --         local ref = references[j]
        --         writer:writeln('import %s from "./%s";', ref, ref)
        --     end
        --     writer:writeln()
        -- end
        
        writer:writeln('using FairyGUI;')
        writer:writeln('using System.Threading.Tasks;')
        writer:writeln()
        writer:writeln('namespace DCET')
        writer:writeln()
        writer:startBlock()
        writer:writeln()
        writer:writeln('[ObjectSystem]')
        writer:writeln('public class %sAwakeSystem : AwakeSystem<%s, GObject>',classInfo.className,classInfo.className)
        writer:startBlock()
        writer:writeln()
        writer:writeln('public override void Awake(%s self, GObject go)',classInfo.className)
        writer:startBlock()
        writer:writeln('self.Awake(go);')
        writer:endBlock()
        writer:endBlock()
        writer:writeln()


        writer:writeln('public partial class %s : %s', classInfo.className, 'FUI')
        writer:startBlock()
        writer:writeln('public const string UIPackageName = "%s";', handler.pkg.name)
        writer:writeln('public const string UIResName = "%s";', classInfo.resName)
        writer:writeln('public const string UIName = "%s.%s";', handler.pkg.name,classInfo.resName)
        writer:writeln('public GComponent self;', classInfo.resName)
        writer:writeln()

        local memberCnt = members.Count
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            writer:writeln('public %s %s;', GetType(string.gsub(memberInfo.type,'fgui.','')),memberInfo.varName)
        end
        writer:writeln('public static string URL = "ui://%s%s";', handler.pkg.id, classInfo.classId)
        writer:writeln()

        writer:writeln('public void Awake(GObject go)')
        writer:startBlock()
        writer:writeln('if (go == null)')
        writer:startBlock()
        writer:writeln('return;')
        writer:endBlock()
        writer:writeln('GObject = go;')
        writer:writeln('Name = UIName;')
        writer:writeln('self = (GComponent)go;')
        writer:writeln('self.Add(this);')
        writer:writeln('var com = go.asCom;')
        writer:writeln('if (com != null)')
        writer:startBlock()
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            if memberInfo.group==0 then
                if getMemberByName then
                    writer:writeln('this.%s = (%s)com.GetChild("%s");', memberInfo.varName, GetType(string.gsub(memberInfo.type,'fgui.','')), memberInfo.name)
                else
                    writer:writeln('this.%s = (%s)com.GetChildAt(%s);', memberInfo.varName, GetType(string.gsub(memberInfo.type,'fgui.','')), memberInfo.index)
                end
            elseif memberInfo.group==1 then
                if getMemberByName then
                    writer:writeln('this.%s = com.GetController("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = com.GetControllerAt(%s);', memberInfo.varName, memberInfo.index)
                end
            else
                if getMemberByName then
                    writer:writeln('this.%s = com.GetTransition("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = com.GetTransitionAt(%s);', memberInfo.varName, memberInfo.index)
                end
            end
        end
        writer:writeln('OnInitialization();')
        writer:endBlock()
        writer:endBlock()
        writer:writeln()
        writer:writeln('partial void OnInitialization();')
        writer:writeln()
        writer:writeln('public override void Dispose()')
        writer:startBlock()
        writer:writeln('base.Dispose();')
        writer:writeln('self.Remove();')
        writer:endBlock()
        writer:endBlock() --class
        writer:endBlock()

        writer:writeln('namespace DCET')
        writer:startBlock()
            writer:writeln('public partial class FGUIGenerate')
            writer:startBlock()

                writer:writeln('public static async void Create_%s()',classInfo.resName)
                writer:startBlock()
                    writer:writeln('await FGUIHelper.Add<%s,%sComponent>(%s.UIPackageName, %s.UIResName);',classInfo.className,classInfo.className,classInfo.className,classInfo.className)
                writer:endBlock()

                writer:writeln('public static async Task<%sComponent> Await_Create_%s()',classInfo.className,classInfo.resName)
                writer:startBlock()
                    writer:writeln('return await FGUIHelper.Add<%s,%sComponent>(%s.UIPackageName, %s.UIResName);',classInfo.className,classInfo.className,classInfo.className,classInfo.className)
                writer:endBlock()

            writer:endBlock()
        writer:endBlock()

        writer:save(exportCodePath..'/'..classInfo.className..'.cs')
    end

end

function GetType(type)
    if type ~= 'GList' 
    and type ~= 'GImage' 
    and type ~= 'GTextField'  
    and type ~= 'GMovieClip'  
    and type ~= 'GTextInput'  
    and type ~= 'GGraph'  
    and type ~= 'GLoader'  
    and type ~= 'GRichTextField'  
    and type ~= 'GGroup'
    and type ~= 'GComponent'
    and type ~= 'ScrollPane'
    and type ~= 'Controller'
    and type ~= 'GLabel'
    and type ~= 'GButton'
    and type ~= 'GComboBox'
    and type ~= 'GProgressBar'
    and type ~= 'GSlider'
    and type ~= 'GScrollBar'
    and type ~= 'GTree'
    and type ~= 'Window'
    and type ~= 'PopupMenu'
    and type ~= 'Transition' then
        return 'GComponent'
    end
    return type
end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
-------------------- FairyGUI Copyright © 2014-2020 FairyGUI.com Version 3.4.0 support@fairygui.com -------------------- FairyGUI is a flexible UI framework for Unity, working with the professional FREE Game UI Editor: FairyGUI Editor. Download the editor from here: http://en.fairygui.com/ -------------------- Get Started -------------------- Run demos in Assets/FairyGUI/Examples/Scenes. The UI project is in Examples-UIProject.zip, unzip it anywhere. Use FairyGUI Editor to open it. Using FairyGUI in Unity: * Place a UIPanel in scene by using editor menu GameObject/FairyGUI/UIPanel. * Or using UIPackage.CreateObject to create UI component dynamically, and then use GRoot.inst.AddChild to show it on screen. ----------------- Version History ----------------- 3.4.0 - NEW: Add multi-display support. - NEW: Add API DynamicFont(name, font). - IMPROVED: Compatibility with 2018.3. - FIXED: Incorrect letter spacing on mobile platform. - FIXED: Same transition hook may be called twice. - FIXED: Exception raised when texture was disposed before object was disposed. 3.3.0 - NEW: Add textfield maxwidth feature. - NEW: Add API to query package dependencies. - IMPROVED: Graphics module refactor. Now it is more convenient to create various shapes(pie, lines, polygon etc) and do mesh deform. Memory usage on building mesh is also dropped. Also supports automatically setup uv for arbitrary quad to avoid seam between 2 triangles. All shaders are updated, don't forget to replace shaders in your project. - IMPROVED: Text-Brighter mechanism is removed, so FairyGUI-Text-Brighter.shader is removed. - IMPROVED: Add support for shrinking multi-line text. - IMPROVED: Improve Lua support. 3.2.0 - NEW: Add DisplayObjectInfo component. Define script symbol FAIRYGUI_TEST to enable it. - FIXED: A virtual list scrolling bug. - FIXED: A BlendMode bug. 3.1.0 - NEW: Draw extra 8 directions instead of 4 directions to archive text outline effect. Toggle option is UIConfig.enhancedTextOutlineEffect. - IMPROVED: Eexecution efficiency optimizations. - IMPROVED: GoWrapper now supports multiple materials. - FIXED: Correct cleanup action for RemovePackage. 3.0.0 From this version, we change package data format to binary. Editor version 3.9.0 with 'binary format' option checked in publish dialog is required to generating this kind of format. Old XML format is not supported anymore. - NEW: Add UIPackage.UnloadAssets and UIPackage.ReloadAssets to allow unload package resources without removing the package. - NEW: Add TransitionActionType.Text and TransitionActionType.Icon. 2.4.0 - NEW: GTween is introduced, DOTween is no longer used inside FairyGUI. - NEW: Transitions now support playing partially and pausing. - IMPROVED: Change the way of registering bitmap font. - FIXED: A GButton pivot issue. - FIXED: Correct text align behavior. 2.3.0 - NEW: Allow loader to load component. - NEW: Add text template feature. - FIXED: Exclude invisible object in FairyBatching. 2.2.0 - NEW: Modify shaders to fit linear color space. - IMPROVED: Improve relation system to handle conditions that anchor is set. - IMPROVED: Eliminate GC in transition. - FIXED: Fixed a bug of unloading bundle in UIPackage. - FIXED: Fixed issue that some blend mode(multiply, screen) works improperly. 2.1.0 - NEW: Add GGraph.DrawRoundRect. - NEW: Add FillType.ScaleNoBorder. - FIXED: Repair potential problems of virtual list. - FIXED: Fixed a bug in handling shared materials. 2.0.0 - NEW: RTL Text rendering support. Define scripting symbols RTL_TEXT_SUPPORT to enabled it. - NEW: Support for setting GObject.data in editor. - NEW: Support for setting selectedIcon of list item in editor. - IMPROVED: Add UIConfig.depthSupportForPaitingMode. - IMPROVED: Set sorting order of popup automatically. - FIXED: Fixed a text layout bug when line spacing is negative. - FIXED: Reset ScrollPane.draggingPane when an active scrollPane is being disposed. - FIXED: Fixed a bug of skew rendering.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值