Tolua Learning Note resource loading, data reading __lua

Source: Internet
Author: User
Tags lua

Not familiar with the List.lua can refer to http://blog.csdn.net/Jason_520/article/details/54238020
UI Listener Event http://blog.csdn.net/jason_520/article/details/54233497

The effect is probably this:

Run the game, click the Display panel button, display another panel, and read the resources to display the corresponding content.

Attach the LUA code:

--uitranscript.lua

uitranscript = 
{
    Gameobject = "0",
    data = 0,
};
Uitranscript.__index = Uitranscript

function uitranscript:new (gameobject, data) local
    o = {};
    Setmetatable (o,self);
    O.gameobject = Gameobject;
    O.data = data;
    return o;
End
--transcriptinfo.lua transcriptinfo = {id = "0", AreaName = "0", ScriptName = "0",
Scripticon = "0", scripttable = "0", Scriptscene = "0"}; Transcriptinfo.__index = Transcriptinfo function transcriptinfo:new (id,areaname,scriptname,scripticon,scripttable,
    scriptscene) Local o = {};
    Setmetatable (o,self);
    O.id = ID;
    O.areaname = AreaName;
    O.scriptname = ScriptName;
    O.scripticon = Scripticon;
    o.scripttable = scripttable;
    O.scriptscene = Scriptscene;
return o; End 
--selecttranscriptpanel.lua require "Logic/uitranscript" require "common/define" require "Unityengine/vector2"
Require "Controller/selecttranscriptctrl" local gameobject;

Local transform;
Selecttranscriptpanel = {};
Local this = Selecttranscriptpanel;

Local selecttranscriptctrl=selecttranscriptctrl.new (); function Selecttranscriptpanel.awake (obj) this.
Createpanel (); End Function Selecttranscriptpanel.start (obj) End Function Selecttranscriptpanel.createpanel () resmgr:loadprefab (' s Electtranscript ', {' _selecttranscriptpanel '},this.
Onloadfinish);

    End Function Selecttranscriptpanel.onloadfinish (OBJS) Local go=gameobject.instantiate (Objs[0]);
    Gameobject = go;   

    Transform = Go.transform;
    Local Parent=gameobject.find ("Canvas");
    Go.transform:SetParent (Parent.transform);
    Go.transform:GetComponent ("Recttransform"). Anchoredposition = Vector2.new (0,0);
    Go.transform:GetComponent ("Recttransform"). Sizedelta = Vector2.new (0,0); Go.transform:GetComponent("Recttransform"). Localscale = Vector3.new (1,1,1);

    Util.Log ("Finish"); This.

Initpanel ();
    End Function Selecttranscriptpanel.initpanel ()--this.btnclose = Transform:findchild ("Close"). Gameobject; --At this point the key defaults to increment from 1 selecttranscriptpanel.sprites = {uitranscript:new (transform:findchild ("Panel/transcript") ). gameobject,0), Uitranscript:new (Transform:findchild ("Panel/transcript2"). gameobject,0), UITranScript:new (Transform:findchild ("Panel/transcript3"). gameobject,0), Uitranscript:new (Transform:findchild ("panel/ TranScript4 "). gameobject,0), Uitranscript:new (Transform:findchild (" Panel/transcript5 "). gameobject,0), UITr Anscript:new (Transform:findchild ("Panel/transcript6"). gameobject,0), Selecttranscriptctrl.oncreate (GameObject)

; End Function Selecttranscriptpanel.ondestroy () end
--selecttranscriptctrl.lua Local list = require "list" require "Logic/transcriptinfo" require "Common/define" Transcrip Tinfolist = List:new ();
--Create a list of Selecttranscriptctrl = {};

Local this = Selecttranscriptctrl;
Local gameobject;
Local transform;

Local luabehaviour;
function Selecttranscriptctrl.new () return this;
    End Function Selecttranscriptctrl.awake () End Function selecttranscriptctrl.oncreate (obj) gameobject = obj;
    Transform = Obj.transform;
    --luabehaviour = gameobject:getcomponent (' Luabehaviour ');
    Local Close=gameobject.find ("close"); Eventtriggerlistener.get (Close). Onpointerclick=eventtriggerlistener.get (Close). Onpointerclick+this.
    Close; --luabehaviour:addclick (Selecttranscriptpanel.btnclose, this. close);--Register Shutdown button event for Index,value in Ipairs (selecttranscriptpanel.sprites) do--luabehaviour:addclick (value . Gameobject, this. ImageClick); --Register button click event Eventtriggerlistener.get (value.gameobject). Onpointerclick=eventtriggerlisteNer. Get (Value.gameobject). Onpointerclick+this.
    ImageClick; End Resmgr:loadtextasset (' data ', {' area '}, this.
GetInfo);--read into Area.txt file end local Nowareaid = 0; function Selecttranscriptctrl.getinfo (OBJS)--this side of the pit trampled a lot of. Later found not to add--_gt (typeof (StringSplitOptions)) to Customsetting.cs, this thing local str = SYSTEM.STRING.NEW (objs[0]:tostring ()) Local strarray = Str:split (' \ r \ n ', System.StringSplitOptions.RemoveEmptyEntries)--breaks the string as a separator, and splits the strings--local
    Strarray = Luahelper.stringsplit (str, ' \ r \ n ');
        For i = 2, strarray.length-1 does local temp = System.String.New (Strarray[i]);
        Local strArray2 = Temp:split (', ', System.StringSplitOptions.RemoveEmptyEntries);

        --local strArray2 = luahelper.stringsplit (temp, ', ');
        Local L = transcriptinfo:new (strarray2[0],strarray2[1],strarray2[2],strarray2[3],strarray2[4],strarray2[5]);
        Transcriptinfolist:push (L);--adding nowareaid=nowareaid+1;
    Selecttranscriptctrl.refresh (Nowareaid);End of local nowimageindex;
Local spriteindex = 1;

    function Selecttranscriptctrl.refresh (areaid) Nowimageindex = 1;  

    Local now = nil;  
        For i = 1,transcriptinfolist.length,1 do-traversal list now = Transcriptinfolist:next (now); 

        Local v = now.value;
            if (v.id = = ToString (areaid)) then print (Spriteindex);
            Selecttranscriptpanel.sprites[spriteindex].data = v;
            Spriteindex = Spriteindex + 1; Resmgr:loadsprite (' Selecttranscript_asset ', {V.scripticon}, this.  Imageinit); --Load Sprite End Function Selecttranscriptctrl.imageinit (OBJS)--Set sprite--print (Nowimageindex

    );
    Local go = Selecttranscriptpanel.sprites[nowimageindex].gameobject; Go:getcomponent ("Image"). Sprite = Objs[0]; --To add _gt (typeof Image) to the Customsetting.cs, _gt (typeof (Sprite)), Go.transform:FindChild ("Text"): Getcomponent ('
    Text '). Text = Selecttranscriptpanel.sprites[nowimageindex].data.scriptname; NowimageiNdex = Nowimageindex + 1;
    End Function Selecttranscriptctrl.imageclick (GO) print (' Lua click trigger! from Gameobject: ' ... go.name);  For Index,value in Ipairs (selecttranscriptpanel.sprites) do if (Value.gameobject = go) then local V

            = Value.data; if (v ~= Nil and v ~= 0) then print (v.id. V.areaname.. V.scriptname.. V.scripticon.. V.scripttable..
            V.scriptscene);

    End-end-End Function Selecttranscriptctrl.open (areaid,selecttranscriptpanel)--nowareaid = Areaid; --if (Transcriptinfolist.length > 0) then Selecttranscriptctrl.refresh (Nowareaid); End--print (Nowareaid ... ""). transcriptinfolist.length)--if Gameobject = = Nil then Panelmgr:createpanel (' Selecttranscript ', this. 
    OnCreate);
    if Gameobject = = Nil then selecttranscriptpanel.awake (gameobject);
    else gameobject:setactive (true);
End-End Function Selecttranscriptctrl.close (GO) gameobject:setactive (false); End
--selectareapanel.lua--this class is first put ...
Local transform;

Local gameobject;
Selectareapanel = {};

Local this = Selectareapanel;
    --Start Event-function selectareapanel.awake (obj) gameobject = obj;

    Transform = Obj.transform; This.
    Initpanel (); Print ("Awake lua--->>".
Gameobject.name);
    End--initializes the panel-function selectareapanel.initpanel () this.btn = Transform:findchild ("button"). Gameobject;
    THIS.BTN2 = Transform:findchild ("Button2"). Gameobject;
this.img = Transform:findchild ("Image"). Gameobject;
End Function Selectareapanel.ondestroy () print ("OnDestroy---->>>"); End 
--selectareactrl.lua require "Controller/selecttranscriptctrl" Require View/selectareapanel "require" view/ Selecttranscriptpanel "require" Unityengine/vector3 "require" 3RD/PBLUA/LOGIN_PB "require" 3rd/pbc/protobuf "local Sproto = require "3rd/sproto/sproto" local core = require "sproto.core" local Print_r = require "3rd/sproto/print_r" Sele
Ctareactrl = {};

Local this = Selectareactrl;
Local panel;
Local prompt;
Local transform;

Local gameobject;
---function selectareactrl.new () return this;
    End Function Selectareactrl.awake () print ("Selectareactrl.awake--->>"); --panelmgr:createpanel (' _selectareapanel ', this.
    OnCreate); Resmgr:loadprefab ("Selectarea", {"_selectareapanel"},this).
OnCreate);
    End--Start event--function Selectareactrl.oncreate (OBJS) gameobject = objs[0];

    Transform = Objs[0].transform;   
    Local go=gameobject.instantiate (objs[0]);
    Parent=gameobject.find ("Canvas");
    Go.transform:SetParent (Parent.transform); Go.transform:gEtcomponent ("Recttransform"). Anchoredposition = Vector2.new (0,0);
    Go.transform:GetComponent ("Recttransform"). Sizedelta = Vector2.new (0,0);

    Go.transform:GetComponent ("Recttransform"). Localscale = Vector3.new (1,1,1);
    --selectareapanel.awake (Objs[0]);
    --prompt = transform:getcomponent (' Luabehaviour '); --prompt:addclick (SELECTAREAPANEL.BTN, this.
    OnClick); --prompt:addclick (SELECTAREAPANEL.BTN2, this.
    OnClick); --resmgr:loadprefab (' Prompt ', {' Promptitem '}, this.

    Initpanel);
    Btn=gameobject.find ("button") Listener=eventtriggerlistener.get (BTN);

    Listener.onpointerclick=listener.onpointerclick+selectareactrl.onclick;
    --btn2=gameobject.find ("Button2")--listener=eventtriggerlistener.get (BTN2);

    --listener.onpointerclick=listener.onpointerclick+selectareactrl.onclick;
    Img=gameobject.find ("Image") Listener = Eventtriggerlistener.get (IMG);
    Listener.onpointerclick = Listener.onpointerclick + selectareactrl.a; Listener.ondrag = LiStener.ondrag + selectareactrl.b; End Function Selectareactrl.a (g) Print ("Point to me ...")
"); End Function selectareactrl.b (g)--uicamera=parent.
    Find ("Uicamera");

    Img.transform.position=unityengine.input.mouseposition; Print ("So shy ...")
"); 
    End--Initialize panel--function Selectareactrl.initpanel (OBJS) Local count = 100;
    Local parent = promptpanel.gridparent;
        For i = 1, count does local go = NewObject (objs[0]); Go.name = ' Item '.
        ToString (i);
        Go.transform:SetParent (parent);
        Go.transform.localScale = Vector3.one;
        Go.transform.localPosition = Vector3.zero; Prompt:addclick (go, this.)

        Onitemclick);
        Local label = Go.transform:FindChild (' Text ');
    Label:getcomponent (' text '). Text = ToString (i);
End---scroll Item click-function Selectareactrl.onitemclick (GO) log (go.name); End--click event--function Selectareactrl.onclick (go) if go = Btn then Selecttranscriptctrl.open ("1", Selecttran
    Scriptpanel); End End

Make the next two presets

In Luaframework/examples, create the folder data and create the Area.txt, which reads as follows:

Area ID, area name, replica name, replica icon, copy table, replica scene
id,areaname,scriptname,scripticon,scripttable,scriptscene
1, area One, Green, C1 , Data/battle_001,battle
2, area One, Sky City, C2,data/battle_002,battle
3, area One, King's Ruins, C3,data/battle_003,battle
4, area two, mechanical ox, D1,data/battle_004,battle
5, area two, snow-capped mountains, D2,data/battle_005,battle
6, area Two, Tian Jie, d3,data/battle_006, Battle

Add two methods to ResourceManager.cs:

public void Loadsprite (String abname, string[] assetnames, luafunction func)
{
    loadasset<sprite> (abname , Assetnames, NULL, func);

public void Loadtextasset (String abname, string[] assetnames, luafunction func)
{
    Loadasset<textasset > (abname, Assetnames, NULL, func);
}

Add the code in Packager.cs that needs to be punched into the AB package:

Addbuildmap ("Selectarea" + appconst.extname, "*.prefab", "Assets/luaframework/examples/builds/selectarea");
Addbuildmap ("Selecttranscript" + appconst.extname, "*.prefab", "assets/luaframework/examples/builds/ Selecttranscript ");

Addbuildmap ("Selecttranscript_asset" + appconst.extname, "*.png", "assets/luaframework/examples/textures/ Selecttranscript ");

Addbuildmap ("Data" + appconst.extname, "*.txt", "Assets/luaframework/examples/data");

Here also to mention, remember CustomSetting.cs added (in fact, to remind myself, forget all day):

_gt (typeof (Image)),
_gt (typeof (Sprite)), _gt (typeof (Button)), _gt (typeof (
Textasset))
, _GT ( typeof (StringSplitOptions)),

Make this change in GameManager.cs.

Presumably the whole idea is:
* 1 Unity Start Selectareactrl.lua, call the Awake function, create the "_selectareapanel" panel, and register some UI button events.
* 2 Click the Display panel button, call the Selecttranscriptctrl.open method, this method calls the Selecttranscriptpanel.awake method, create the _selecttranscriptpanel panel, and initialization of some data.
* 3 Selecttranscriptctrl.getinfo Loads the contents of the Area.txt file and is displayed on the UI.

Implementation content is very simple, but the process in LUA to achieve, feel good chaos, but continue to work hard ...
Finally attach the project download address: Https://github.com/xiaoxiaosen520/LuaFramework_UGUI.git

The author is also just contact Luaframework, Bowen just as learning notes to share with you, hope to progress together with you.

Make a little progress every day.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.