Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

感觉lua里取ui类别componen的t操作有些复杂 #45

Open
lizijie opened this issue Mar 26, 2019 · 1 comment
Open

感觉lua里取ui类别componen的t操作有些复杂 #45

lizijie opened this issue Mar 26, 2019 · 1 comment

Comments

@lizijie
Copy link
Contributor

lizijie commented Mar 26, 2019

有一个动态创建的节点(listItem)已在inspector面板挂有UIImage,脚本里要设置sprite。
我以为这要就可以了

local img =  UIUtil.FindImage(trans, "Image")
img:SetSpriteName("xxxx")

但实际情况,img是UnityEngin.UI.UIImage,并不是UIImage.lua里的那个UIImage。按目前框架一般按如下操作

local img_trans = UIUtil.FindTrans(trans, "Image")</br>
local img = self:GetComponent(UIImage, img_trans .gameObject)
if img == nil then
    img = self:AddComponent(UIImage, img_trans .gameObject)
end
img:SetSpriteName("xxxx")

或许可以再简单些,但是这里的节点是动态创建,传取self:GetComponent的节点路径对view来说不能预先定义

local img_path = "Image"
 local img = self:GetComponent(UIImage, img_path)
if img == nil then
    img = self:AddComponent(UIImage, img_path)
end
img:SetSpriteName("xxxx")

另外,有一个地方可能对于无感知的使用者产生误解。
已在inspector挂了UIImage,为何让使用者在lua里还要AddComponent?是否可以将AddComponent隐藏在框架/接口内部?

@aoktian
Copy link

aoktian commented Feb 2, 2023

自己多封装几个方法
--- 加载精灵 function ResourcesManager:LoadSprite(path, callback, ...) self:LoadAsync(path, T_Sprite, function(sprite, ...) if not callback then return end callback(not IsNull(sprite) and sprite or nil, ...) end, ...) end

`ResourcesManager():LoadSprite(conf.path, function(sprite)
local unity_uiimage = UIUtil.FindImage(self.Bg)
unity_uiimage.sprite = sprite

self.bg_ready = true

end)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants