FairyGUI 代码生成插件

 

 

生成的代码

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

using FairyGUI;

namespace ETModel
 {

	[ObjectSystem]
	public class UI_CanteraComponentAwakeSystem : AwakeSystem {

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

	public partial class UI_CanteraComponent : FUI {
		public const string UIPackageName = "ComponentPackage";
		public const string UIResName = "CanteraComponent";
		public const string UIName = "ComponentPackage.CanteraComponent";
		public GComponent self;

		public GButton m_closeBtn;
		public GButton m_settingBtn;
		public GLoader m_icon;
		public GTextField m_describe;
		public GList m_powerStarList;
		public GList m_timeStarList;
		public GButton m_leftBtn;
		public GButton m_rightBtn;
		public GProgressBar m_power;
		public GProgressBar m_time;

		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.m_closeBtn = (GButton)com.GetChildAt(4);
				this.m_settingBtn = (GButton)com.GetChildAt(5);
				this.m_icon = (GLoader)com.GetChildAt(6);
				this.m_describe = (GTextField)com.GetChildAt(7);
				this.m_powerStarList = (GList)com.GetChildAt(10);
				this.m_timeStarList = (GList)com.GetChildAt(11);
				this.m_leftBtn = (GButton)com.GetChildAt(12);
				this.m_rightBtn = (GButton)com.GetChildAt(13);
				this.m_power = (GProgressBar)com.GetChildAt(16);
				this.m_time = (GProgressBar)com.GetChildAt(17);
				OnInitialization();
			}
		}

		partial void OnInitialization();

		public override void Dispose() {
			base.Dispose();
			self.Remove();
			self = null;
			m_closeBtn = null;
			m_settingBtn = null;
			m_icon = null;
			m_describe = null;
			m_powerStarList = null;
			m_timeStarList = null;
			m_leftBtn = null;
			m_rightBtn = null;
			m_power = null;
			m_time = null;
		}
	}
}

 

--region LuaCodeWriter
local LuaCodeWriter = fclass()

function LuaCodeWriter:ctor(config)
    config = config or {}
    self.blockStart = config.blockStart or '{'
    self.blockEnd = config.blockEnd or '}'
    self.blockFromNewLine = config.blockFromNewLine
    if self.blockFromNewLine == nil then
        self.blockFromNewLine = true
    end
    if config.usingTabs then
        self.indentStr = '\t'
    else
        self.indentStr = '    '
    end
    self.usingTabs = config.usingTabs
    self.endOfLine = config.endOfLine or '\n'
    self.lines = {}
    self.indent = 0

    self:writeMark()
end

function LuaCodeWriter:writeMark()
    table.insert(self.lines, '--- This is an automatically generated class by FairyGUI. Please do not modify it. ---')
    table.insert(self.lines, '')
end

function LuaCodeWriter:writeln(format, ...)
    if not format then
        table.insert(self.lines, '')
        return
    end

    local str = ''
    for i = 0, self.indent - 1 do
        str = str .. self.indentStr
    end
    str = str .. string.format(format, ...)
    table.insert(self.lines, str)

    return self
end

function LuaCodeWriter:startBlock()
    if self.blockFromNewLine or #self.lines == 0 then
        self:writeln(self.blockStart)
    else
        local str = self.lines[#self.lines]
        self.lines[#self.lines] = str .. ' ' .. self.blockStart
    end
    self.indent = self.indent + 1

    return self
end

function LuaCodeWriter:endBlock()
    self.indent = self.indent - 1
    self:writeln(self.blockEnd)

    return self
end

function LuaCodeWriter:incIndent()
    self.indent = self.indent + 1

    return self
end

function LuaCodeWriter:decIndent()
    self.indent = self.indent - 1

    return self
end

function LuaCodeWriter:reset()
    if #self.lines > 0 then
        self.lines = {}
    end
    self.indent = 0

    self:writeMark()
end

function LuaCodeWriter:tostring()
    return table.concat(self.lines, self.endOfLine)
end

function LuaCodeWriter:save(filePath)
    local str = table.concat(self.lines, self.endOfLine)

    CS.System.IO.File.WriteAllText(filePath, str)
end

--endregion

local projectCustomPropertiesDic;

local customPropKeys = {
    key_gen_lua = { name = "key_gen_lua", default_value = "true" },
    key_lua_file_extension_name = { name = "key_lua_file_extension_name", default_value = "lua" },
    key_lua_path_root = { name = "key_lua_path_root", default_value = "UIGenCode/" },
    key_wrapper_namespace = { name = "key_wrapper_namespace", default_value = "CS.FairyGUI" },
}

local function get_project_custom_property_value(key_name)
    if (projectCustomPropertiesDic and projectCustomPropertiesDic:ContainsKey(key_name)) then
        return projectCustomPropertiesDic:get_Item(key_name);
    else
        return customPropKeys[key_name].default_value;
    end
end

function onPublish(handler)
    if not handler.genCode or handler.publishDescOnly then
        return
    end

    projectCustomPropertiesDic = App.project:GetSettings("CustomProperties").elements;

    local gen_lua = get_project_custom_property_value(customPropKeys.key_gen_lua.name);
    if (gen_lua == "true") then
        handler.genCode = false --prevent default output
        App.consoleView:Clear();
        fprint("Handling gen lua code in plugin.")
        genCode(handler)
    end
end

-- this is copied from Editor-Install-Path/Resources/Data/StreamingAssets/Scripts
function genCode(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  })

    --FUIPackageStart--
    writer:writeln('namespace ETModel')
    writer:startBlock()
    writer:writeln('public static partial class FUIPackage')
    writer:startBlock()

    writer:writeln('public const string %s = "%s";',codePkgName,codePkgName)

    -- 生成所有的
    local itemCount = handler.items.Count
    for i=0,itemCount-1 do
        --fprint(handler.items[i].path)
        local path = string.format('%s%s%s',codePkgName,handler.items[i].path,handler.items[i].name)
        --writer:writeln('public const string %s = "ui://%s";//%s',string.gsub(path,'/','_'),string.format('%s/%s',codePkgName,handler.items[i].name),string.format('ui://%s%s',handler.pkg.id,handler.items[i].id))
        writer:writeln('public const string %s = "ui://%s";',string.gsub(path,'/','_'),string.format('%s%s',handler.pkg.id,handler.items[i].id))
    end

    writer:endBlock()--class
    writer:endBlock()--namespace
    local binderPackageName = 'Package'..codePkgName
    writer:save(exportCodePath..'/'..binderPackageName..'.cs')
    --FUIPackageEnd--
    
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        local members = classInfo.members
        local references = classInfo.references
        writer:reset()

        --local refCount = references.Count
        writer:writeln('using FairyGUI;')
        writer:writeln('using ETModel;')
        writer:writeln()
        writer:writeln('namespace ETHotfix')
        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]
            local typeName = memberInfo.type
            -- 判断是不是我们自定义类型
            if string.find(typeName,'fgui')==nil then
                writer:writeln('public %s %s;', typeName,memberInfo.varName)
            else
                writer:writeln('public %s %s;', string.gsub(memberInfo.type,'fgui.',''),memberInfo.varName)
            end
        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]
            local typeName = memberInfo.type
            if memberInfo.group==0 then
                 -- 判断是不是我们自定义类型
                if getMemberByName then
                    if string.find(typeName,'fgui')==nil then
                        writer:writeln('this.%s = ComponentFactory.Create<%s, GObject>( com.GetChildAt(%s));', memberInfo.varName, typeName, memberInfo.name)
                    else
                        writer:writeln('this.%s = (%s)com.GetChild("%s");', memberInfo.varName, string.gsub(memberInfo.type,'fgui.',''), memberInfo.name)
                    end
                else
                    if string.find(typeName,'fgui')==nil then
                        writer:writeln('this.%s = ComponentFactory.Create<%s, GObject>( com.GetChildAt(%s));', memberInfo.varName, typeName, memberInfo.index)
                    else
                        writer:writeln('this.%s = (%s)com.GetChildAt(%s);', memberInfo.varName, string.gsub(memberInfo.type,'fgui.',''), memberInfo.index)
                    end
                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:writeln('self = null;')

        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            local typeName = memberInfo.type
            if memberInfo.group==0 then
                 -- 判断是不是我们自定义类型
                 if string.find(typeName,'fgui')==nil then
                    writer:writeln('%s.Dispose();', memberInfo.varName)
                end
                    writer:writeln('%s = null;', memberInfo.varName)
            else
                if getMemberByName then
                    writer:writeln('%s = null;', memberInfo.varName)
                else
                    writer:writeln('%s = null;', memberInfo.varName)
                end
            end
        end

        writer:endBlock()
        writer:endBlock() --class
        writer:endBlock()

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

end

 

你可能感兴趣的:(unity3d,fairygui,插件)