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();

            //Game.Scene.GetComponent().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 {

		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.UIPackageName, FUILogin.UIResName);
		}
		public static async Task Await_Create_Login() {
			return await FGUIHelper.Add(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

 

你可能感兴趣的:(ET框架)