Canvas、Text、Image

4/3/2024 Unity3d

https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Text.html (opens new window)

https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Image.html (opens new window)

functions

functions parameter describe
transform:GetText(string) Name of child to be found.

return: UnityEngine.UI.Text
Get the UnityEngine.UI.Text of the child object through the path
transform:SetTxt(string,string) 1.Name of child to be found.

2.value
Get the UnityEngine.UI.Text of the child object through the path,

and then set the string value, if the found object does not

have this component, the setting will fail
transform:SetTxt(GameObject,string) 1. UnityEngine.UI.Text

2.value
Setup will fail if GameObject does not contain UnityEngine.UI.Text
transform:GetImage(string) 1.Name of child to be found.

return: UnityEngine.UI.Image
Get the UnityEngine.UI.Text of the child object through the path
transform:SetImage(string,sprite) 1.Name of child to be found.

2.UnityEngine.Sprite
transform:SetImage(Image,sprite) 1.UnityEngine.UI.Image

2.UnityEngine.Sprite
transform:SetImage(GameObject,sprite) 1.UnityEngine.GameObject

2.UnityEngine.Sprite
Setup will fail if GameObject does not contain UnityEngine.UI.Image
transform:UIroot() return:UnityEngine.Transform ui root node. it's a canvas

example1

transform:SetTxt("Content/titleTxt/txtContent","hello");

--set Image
function testSprite()
    loadAsyncSprite("UI/UIicon/HeroCharacter/Gritster",onLoadFinished)
end

function onLoadFinished(obj)
    if IsExist(obj) then
        print(obj)
        transform:SetImage("Canvas/Image",obj)
    end
end
1
2
3
4
5
6
7
8
9
10
11
12
13

loadUI

load a UI prefab, and dynamically bind the lua script, SetTxt

function Start()
    loadAsync("UI/UIPrefab/UITaskLinePanel",onLoadFinished)
end

function onLoadFinished(prefab)
    if IsExist(prefab) then
        local go = newObject(prefab) -- or GameObject.Instantiate(prefab)
        go.name = prefab.name
        local tf = go.transform
        local rectTf = go:GetComponent(typeof(UnityEngine.RectTransform))
        tf:SetParent(transform:UIroot()) --设置当前加载UI 的父级 canvas下
        tf.localScale = Vector3.one
        tf.localEulerAngles = Vector3.zero
        rectTf.sizeDelta = Vector2.zero
        rectTf.anchorMin = Vector2.zero
        rectTf.anchorMax = Vector2.one
        rectTf.anchoredPosition = Vector2.zero
        rectTf.anchoredPosition3D = Vector3.zero
        dynamicLua(go)
    end
end

--Dynamic bind Lua by name   
function dynamicLua(go)
        local plane=  bindScriptByname(go,'uiPlane')
        local data ={}
        data.titleTxt = 'hello'
        data.contentTxt = 'welcome!'
        data.btnYesTxt = 'thanks'
        data.btnNoTxt = 'Ha ha'
        data.callYes = thanks
        data.callNo = haha
        plane.show(data)    
end

function thanks()
    print('1')
end

function haha()
    print('2')
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

uiPlane

function Start()
    transform:AddBtnEvent("Content/btnContent/btnYes",onClickYes);
    transform:AddBtnEvent("Content/btnContent/btnNo",onClickNo);
end

local exedata
function show(data)
    exedata =data
    gameObject:SetActive(true)
    transform:SetTxt('Content/titleTxt/txtContent',data.titleTxt)
    transform:SetTxt('Content/contentTxt',data.contentTxt)
    transform:SetTxt('Content/btnContent/btnYes/txtYes',data.btnYesTxt)
    transform:SetTxt('Content/btnContent/btnNo/txtNo',data.btnNoTxt)
end


function onClickYes()
    if exedata.callYes then
        exedata.callYes()
    end
    hide()
end

function onClickNo()
    if exedata.callYes then
        exedata.callNo()
    end
    hide()
end

function hide()
    gameObject:SetActive(false)
    exedata =nil
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

Last Updated: Thu, 11 Apr 2024 06:54:22 GMT