Unit 高效开发系列 - 第三章 代码自动化创建UI预制体

这篇具有很好参考价值的文章主要介绍了Unit 高效开发系列 - 第三章 代码自动化创建UI预制体。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Unit 高效开发系列 - 第三章 代码自动化创建UI预制体

前言

Uinty高效开发系列主要包含一系列的Unity开发实例,主要来源于笔者的一个3D游戏项目和一个手机APP项目,适合利用Unity开发游戏或手机APP场景。

一、代码自动化创建UI预制体实现-方式1

方案1采用基于父窗口的偏移方式确定UI对象位置,比较适合简单UI和固定大小的UI。

 private static void AddItemInfoSubItem(GameObject Pobj)
 {
     GameObject pNewObj;
     Pobj.AddComponent(Type.GetType("SetItemInfo"));
     pNewObj = CreateUIBase.AddNewObj(Pobj, "ItemInfoBg", new Vector3(0, ItemInfoSize.y/2- InforHeight/2, 0), new Vector2(ItemInfoSize.x, InforHeight), CreateUIBase.CompTypeEnum.CT_Img, "ItemInfoBgL" + g_level);       
     GameObject pNewObj2 = CreateUIBase.AddNewObj(pNewObj, "ItemBg", new Vector3(-(ItemInfoSize.x/2) + (InforHeight*0.8f*0.6f) , 0, 0), new Vector2(InforHeight*0.8f, InforHeight * 0.8f), CreateUIBase.CompTypeEnum.CT_Img, "ItemBgL" + g_level);
     CreateUIBase.AddNewObj(pNewObj2, "ItemImg", new Vector3(0, 0, 0), new Vector2(InforHeight * 0.8f, InforHeight * 0.8f), CreateUIBase.CompTypeEnum.CT_Img, "");
     CreateUIBase.AddNewObj(pNewObj, "ItemName" , new Vector3(-((ItemInfoSize.x  - InforHeight) / 2) + (InforHeight * 1.5f), 0, 0), new Vector2(InforHeight*2f, InforHeight * 0.8f), CreateUIBase.CompTypeEnum.CT_Text, "ItemNameL" + g_level, "物品名称");
     CreateUIBase.AddNewObj(pNewObj, "SubInfo", new Vector3(-((ItemInfoSize.x - InforHeight) / 2) + (InforHeight * 1.5f), (-InforHeight * 0.8f + InforHeight * 0.3f)*0.5f, 0), new Vector2(InforHeight * 2f, InforHeight * 0.3f), CreateUIBase.CompTypeEnum.CT_Text, "SubInfo" , "SubInfo");
     CreateUIBase.AddNewObj(pNewObj, "ItemLevel", new Vector3((ItemInfoSize.x / 2) * 0.8f - 60, -InforHeight * 0.25f, 0),new Vector2(InforHeight * 2f, InforHeight * 0.4f), CreateUIBase.CompTypeEnum.CT_Text, "ItemLevelL" + g_level, "极");

     pNewObj = CreateUIBase.AddNewObj(Pobj, "ItemInfoDetailBg", new Vector3(0, -ItemInfoSize.y / 2 + (ItemInfoSize.y - InforHeight)/2, 0), new Vector2(ItemInfoSize.x, ItemInfoSize.y-InforHeight), CreateUIBase.CompTypeEnum.CT_Img, "ItemInfoDetailBg");
     float DetailH = ItemInfoSize.y - InforHeight;
     float ItemJianJieH = DetailH * 0.2f;
     CreateUIBase.AddNewObj(pNewObj, "ItemJianJie", new Vector3(-ItemInfoSize.x * 0.5f*0.5f, (DetailH - ItemJianJieH)*0.5f, 0), new Vector2(ItemInfoSize.x*0.5f, ItemJianJieH), CreateUIBase.CompTypeEnum.CT_Text, "ItemJianJie", "物品简介", CreateUIBase.PovitEnum.LU);
     CreateUIBase.AddNewObj(pNewObj, "ItemDetailInfo", new Vector3(0, 0, 0), new Vector2(ItemInfoSize.x-10, DetailH - ItemJianJieH*2), CreateUIBase.CompTypeEnum.CT_Text, "ItemDetailInfo", "物品详细介绍水电费水电费师傅的说法是否撒旦是的发送到对方是否都是放水电费水电费水电费水电费水电费速度法术单方水电费");
     CreateUIBase.AddNewObj(pNewObj, "HuoQuTuJing", new Vector3(ItemInfoSize.x * 0.5f * 0.5f-20, (-DetailH + ItemJianJieH) * 0.5f, 0), new Vector2(ItemInfoSize.x * 0.5f, ItemJianJieH), CreateUIBase.CompTypeEnum.CT_Text, "HuoQuTuJing", "获取途径");

 }

二、代码自动化创建UI预制体实现-方式2

方案2采用分层定高。计算分段高度,在两个SplitSpanObj之间,或者开头到第一个SplitSpanObj,或者SplitSpanObj到结尾定义为一个分段,要求在一个分段中的各组件的宽高比总和必须相同,总高度按可以通过宽度确定高度的组件(例如按钮)的最大高度来确定,适合制作比较复杂的UI界面和需要动态适配窗口大小的UI。

1.封装UICodePro类

UICodePro类用于创建基本的UI对象,包括Button、DropDown、Scrollbar、Slider、Toggle、Text、ButtonGroup、Panel、FixWithBg、VariablePanel、SearchInputField、TMPInputField等:
Unit 高效开发系列 - 第三章 代码自动化创建UI预制体,Unity,游戏开发,C#,自动化,ui,游戏程序,unity,c#

2.定义UI对象g_ItemInfoDic,通过UI对象创建UI预制体

 public static Dictionary<ItemTypeEnum, ItemInfo> g_ItemInfoDic = new Dictionary<ItemTypeEnum, ItemInfo>()
 {

    {
         ItemTypeEnum.IT_SingleBtn,
         new ItemInfo(ItemTypeEnum.IT_SingleBtn,new List<object>()
         {
             new BgObj("All/ItemBG"),
              new FixWithBgObj("IT_SingleBtn","All/TiJiao","BtnClickPro:SubmitBtnClick"),
              new HiddenTextObj("info",""),
         })
     },

     {
         ItemTypeEnum.IT_SingleColText,
         new ItemInfo(ItemTypeEnum.IT_SingleColText,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip","Tip:",new Vector3(0.5f,1.7f,7.8f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TextObj("Info","",new Vector3(2.2f,7.3f,0.5f),new Vector3(1.0f,8.0f,1.0f),6.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

       {
         ItemTypeEnum.IT_SingleColTextWithTip,
         new ItemInfo(ItemTypeEnum.IT_SingleColTextWithTip,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip","Tip:",new Vector3(0.5f,1.7f,7.8f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TextObj("Info","",new Vector3(2.2f,7.3f,0.5f),new Vector3(1.0f,8.0f,1.0f),6.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },
       {
         ItemTypeEnum.IT_SingleColTextWithOutTip,
         new ItemInfo(ItemTypeEnum.IT_SingleColTextWithOutTip,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
             new TextObj("TextInfo","",new Vector3(0.5f,9f,0.5f),new Vector3(1f,8f,1f),10f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },
     {
         ItemTypeEnum.IT_TowColText,
         new ItemInfo(ItemTypeEnum.IT_TowColText,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2f,7.5f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TextObj("Info1","",new Vector3(2f,2.5f,5.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleCenter),
              new TextObj("Tip2","Tip2:",new Vector3(5.5f,1.5f,3f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TextObj("Info2","",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleCenter),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },


      {
         ItemTypeEnum.IT_SingleColInput,
         new ItemInfo(ItemTypeEnum.IT_SingleColInput,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip","Tip:",new Vector3(0.5f,2f,7.5f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input","",new Vector3(3f,6.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),6.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

      {
         ItemTypeEnum.IT_SingleColDropDown,
         new ItemInfo(ItemTypeEnum.IT_SingleColDropDown,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip","Tip:",new Vector3(0.5f,1.5f,8.0f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown",new Vector3(2f,7.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),6.5f*1.6f,32, new List<string>() { },new Color(1,0.9f,1)),
             // new TMPInputFieldObj("Input","",new Vector3(2f,7.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),6.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },


      {
         ItemTypeEnum.IT_TowColInput,
         new ItemInfo(ItemTypeEnum.IT_TowColInput,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2f,7.5f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input1","",new Vector3(2f,2.5f,5.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1, 0.9f, 1), "BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new TextObj("Tip2","Tip2:",new Vector3(5.5f,1.5f,3f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input2","",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

       {
         ItemTypeEnum.IT_TowColSearchInput,
         new ItemInfo(ItemTypeEnum.IT_TowColSearchInput,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2f,7.5f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SearchInputFieldObj("Input1","",new Vector3(2f,2.5f,5.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField","BtnClickPro:InputValueChange",""),
              new TextObj("Tip2","Tip2:",new Vector3(5.5f,1.5f,3f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SearchInputFieldObj("Input2","",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField","BtnClickPro:InputValueChange",""),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },


       {
         ItemTypeEnum.IT_ThreeColInput,
         new ItemInfo(ItemTypeEnum.IT_ThreeColInput,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2.5f,7f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input1","",new Vector3(0.5f,2.5f,7f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new TextObj("Tip2","Tip2:",new Vector3(3.75f,2.5f,3.75f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input2","",new Vector3(3.75f,2.5f,3.75f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new TextObj("Tip3","Tip3:",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new TMPInputFieldObj("Input3","",new Vector3(7f,2.5f,0.5f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField",new Color(1,0.9f,1),"BtnClickPro:InputValueChange","",TMP_InputField.ContentType.Standard),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

       {
         ItemTypeEnum.IT_ThreeColSearchInput,
         new ItemInfo(ItemTypeEnum.IT_ThreeColSearchInput,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2.5f,7f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SearchInputFieldObj("Input1","",new Vector3(0.5f,2.5f,7f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField","BtnClickPro:InputValueChange",""),
              new TextObj("Tip2","Tip2:",new Vector3(3.75f,2.5f,3.75f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SearchInputFieldObj("Input2","",new Vector3(3.75f,2.5f,3.75f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField","BtnClickPro:InputValueChange",""),
              new TextObj("Tip3","Tip3:",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new SearchInputFieldObj("Input3","",new Vector3(7f,2.5f,0.5f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,"Arial",32,Color.blue, TextAnchor.MiddleCenter,"SearchInputField/InputField","BtnClickPro:InputValueChange",""),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },
        {
         ItemTypeEnum.IT_ThreeColToggle,
         new ItemInfo(ItemTypeEnum.IT_ThreeColToggle,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),                 
              new ToggleObj("Tip1","Tip1",new Vector3(0.5f,2.5f,7f),new Vector3(1.0f,8.0f,1.0f),"BtnClickPro:ToggleChange",2.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f),TextAnchor.MiddleLeft),
              new ToggleObj("Tip2","Tip2",new Vector3(3.75f,2.5f,3.75f),new Vector3(1.0f,8.0f,1.0f),"BtnClickPro:ToggleChange",2.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f),TextAnchor.MiddleLeft),
              new ToggleObj("Tip3","Tip3",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),"BtnClickPro:ToggleChange",2.5f*1.6f,"Arial",32,new Color(0.4f,0.0f,0.5f),TextAnchor.MiddleLeft),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },
       {
         ItemTypeEnum.IT_TowColDropDown,
         new ItemInfo(ItemTypeEnum.IT_TowColDropDown,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,1.5f,8f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown1",new Vector3(2f,2.5f,5.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,40, new List<string>() { },new Color(1,0.9f,1), "BtnClickPro:DropDownValueChange"),
              new TextObj("Tip2","Tip2:",new Vector3(5.5f,1.5f,3f),new Vector3(1.0f,8.0f,1.0f),1.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown2",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,8.0f,1.0f),2.5f*1.6f,40, new List<string>() { },new Color(1,0.9f,1), "BtnClickPro:DropDownValueChange"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

        {
         ItemTypeEnum.IT_ThreeColDropDown,
         new ItemInfo(ItemTypeEnum.IT_ThreeColDropDown,new List<object>()
         {
              new BgObj("All/ItemBG"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
              new TextObj("Tip1","Tip1:",new Vector3(0.5f,2.5f,7f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown1",new Vector3(0.5f,2.5f,7f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f,32, new List<string>() { "A", "B" },new Color(1,0.9f,1), "BtnClickPro:DropDownValueChange"),
              new TextObj("Tip2","Tip2:",new Vector3(3.75f,2.5f,3.75f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown2",new Vector3(3.75f,2.5f,3.75f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f, 32, new List<string>() { "A", "B" },new Color(1,0.9f,1), "BtnClickPro:DropDownValueChange"),
              new TextObj("Tip3","Tip3:",new Vector3(7f,2.5f,0.5f),new Vector3(1.0f,3.5f,5.5f),2.5f*1.5f,"Arial",32,new Color(0.4f,0.0f,0.5f), TextAnchor.MiddleLeft),
              new DropDownObj("DropDown3",new Vector3(7f,2.5f,0.5f),new Vector3(5.5f,3.5f,1.0f),2.5f*1.6f, 32, new List<string>() { "A", "B" },new Color(1,0.9f,1), "BtnClickPro:DropDownValueChange"),
              new SplitSpanObj(30, PixOrScaEnum.Pixel),
         })
     },

 };

3.创建或重置UI对象

通过CreateOrResetItemType函数创建或重置UI对象,通过bReset 参数设置创建UI对象还是重置UI界面大小。由于不同设备的分辨率不一致,或者父窗口的大小发生改变,UI组件也需要动态适配,因此需要进行resize。

public enum ItemTypeEnum
{
    IT_Begin = 0,
    IT_SingleColText,

    IT_SingleBtn,
    IT_SingleColTextWithTip,
    IT_SingleColTextWithOutTip,
    IT_TowColText,

    IT_SingleColInput,
    IT_SingleColDropDown,
    IT_TowColInput,
    IT_ThreeColInput,

    //IT_SingleColSearchInput,
    IT_TowColSearchInput,
    IT_ThreeColSearchInput,

    IT_TowColDropDown,
    IT_ThreeColDropDown,

    IT_ThreeColToggle,     

    IT_BUTT
}

public struct ItemInfo
{
    public ItemTypeEnum type;       
    public List<object> objs;
    public ItemInfo(ItemTypeEnum _type,  List<object> _objs)
    {
        type = _type;          
        objs = _objs;
    }
}
    public static GameObject CreateOrResetItemType(ItemTypeEnum itemType, GameObject pObj, float baseSize, bool bReset = false)
    {
        GameObject go;
        RectTransform rt;
        if (bReset == false)
        {
            go = new GameObject(itemType.ToString());
            go.transform.SetParent(pObj.transform, false);
            rt = go.AddComponent<RectTransform>();

            rt.sizeDelta = Vector2.zero;
            rt.localPosition = Vector2.zero;
            rt.localScale = Vector2.one;
            rt.anchorMin = new Vector2(0, 1);
            rt.anchorMax = new Vector2(0, 1);
            rt.anchoredPosition = Vector3.zero;
            //Image img = go.AddComponent<Image>();           
            //go.SetActive(false);
        }
        else
        {
            go = pObj.transform.Find(itemType.ToString()).gameObject;
            rt = go.GetComponent<RectTransform>();
            go.SetActive(true);
        }

        rt.localScale = new Vector3(1f, 1f, 1f);
        
        float totalSize = 0;

        Debug.Log("itemType:" + itemType.ToString());
        ItemInfo itemInfo = g_ItemInfoDic[itemType];

        List<Vector3> splitHeights = CaleSplitHeight(itemType, baseSize);

        float maxSize = 0;
        bool bStart = true;
        int objNum = 0;
        int curSplit = 0;
        foreach (object obj in itemInfo.objs)
        {
            if (obj.GetType() == typeof(HiddenTextObj))
            {
                HiddenTextObj xObj = (HiddenTextObj)obj;
                Text IndexText;
                if (bReset)
                {
                    IndexText = go.transform.Find(xObj.objName).GetComponent<Text>();
                }
                else
                {
                    GameObject sgo = new GameObject(xObj.objName);
                    sgo.transform.SetParent(go.transform);
                    IndexText = sgo.AddComponent<Text>();
                    sgo.SetActive(false);
                }
                IndexText.text = xObj.text;
                continue;
            }
            if (obj.GetType() == typeof(SplitSpanObj))
            {
                SplitSpanObj xObj = (SplitSpanObj)obj;

                if (xObj.pixOrSca == PixOrScaEnum.Pixel)
                {
                    totalSize += xObj.span;
                }
                else
                {
                    totalSize += baseSize * xObj.span;
                }
                bStart = true;
            }
            else if (obj.GetType() == typeof(BgObj))
            {
                BgObj xObj = (BgObj)obj;
                Image bg = null;
                if (bReset)
                {
                    bg = go.GetComponent<Image>();
                }
                else
                {
                    bg = go.AddComponent<Image>();
                    bg.sprite = GlobalObj.GetSprite(xObj.sprite);
                    if (xObj.scriptFunc != "")
                    {
                        Button btn;
                        if (bReset)
                        {
                            btn = go.GetComponent<Button>();
                        }
                        else
                        {
                            btn = go.AddComponent<Button>();
                        }

                        string[] scriptFunc = xObj.scriptFunc.Split(':');
                        if (scriptFunc.Count() == 2)
                        {
                            go.AddComponent(Type.GetType(scriptFunc[0]));
#if UNITY_EDITOR
                            var myScriptInstance = FindObjectOfType(Type.GetType(scriptFunc[0]));
                            var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance, scriptFunc[1], new Type[] { typeof(GameObject) });
                            UnityAction<GameObject> action = Delegate.CreateDelegate(typeof(UnityAction<GameObject>), myScriptInstance, targetinfo, false) as UnityAction<GameObject>;
                            UnityEditor.Events.UnityEventTools.AddObjectPersistentListener<GameObject>(btn.onClick, action, go);
#endif
                        }
                    }
                }
            }
            else
            {
                if (bStart)
                {
                    if (objNum > 0)
                    {
                        totalSize += splitHeights[curSplit].z;
                        curSplit++;
                    }
                }
                bStart = false;
                objNum++;
                CreateGameObj(obj, go, splitHeights[curSplit], bReset);
            }
        }
        totalSize += splitHeights[curSplit].z;

        rt.sizeDelta = new Vector2(baseSize, totalSize);    
        
        return go;
    }

4.DropDown、SearchInput 对象初始化示例:

下拉选项和带搜索的输入对象在使用时需要初始化,简单的初始化示例如下:

Transform IT_SingleColDropDown = commUIObjsTs.transform.Find("IT_SingleColDropDown");
if (IT_SingleColDropDown != null)
{
    Dropdown dropdown = IT_SingleColDropDown.Find("DropDown").GetComponent<Dropdown>();
    dropdown.options.Clear();
    dropdown.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });
}

Transform IT_TowColDropDown = commUIObjsTs.transform.Find("IT_TowColDropDown");
if (IT_TowColDropDown != null)
{
    Dropdown DropDown1 = IT_TowColDropDown.Find("DropDown1").GetComponent<Dropdown>();
    DropDown1.options.Clear();
    DropDown1.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });

    Dropdown DropDown2 = IT_TowColDropDown.Find("DropDown2").GetComponent<Dropdown>();
    DropDown2.options.Clear();
    DropDown2.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });
}

Transform IT_ThreeColDropDown = commUIObjsTs.transform.Find("IT_ThreeColDropDown");
if (IT_ThreeColDropDown != null)
{
    Dropdown DropDown1 = IT_ThreeColDropDown.Find("DropDown1").GetComponent<Dropdown>();
    DropDown1.options.Clear();
    DropDown1.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });

    Dropdown DropDown2 = IT_ThreeColDropDown.Find("DropDown2").GetComponent<Dropdown>();
    DropDown2.options.Clear();
    DropDown2.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });

    Dropdown DropDown3 = IT_ThreeColDropDown.Find("DropDown3").GetComponent<Dropdown>();
    DropDown3.options.Clear();
    DropDown3.AddOptions(new List<Dropdown.OptionData> { new Dropdown.OptionData("A"), new Dropdown.OptionData("B"), new Dropdown.OptionData("C") });
}


Transform IT_TowColSearchInput = commUIObjsTs.transform.Find("IT_TowColSearchInput");
if (IT_TowColSearchInput != null)
{
    SearchInputField Input1 = IT_TowColSearchInput.Find("Input1").GetComponent<SearchInputField>();
    Input1.options.Clear();
    Input1.AddOptions(new List<string> { "a", "aa", "aaa" });

    SearchInputField Input2 = IT_TowColSearchInput.Find("Input2").GetComponent<SearchInputField>();
    Input2.options.Clear();
    Input2.AddOptions(new List<string> { "a", "aa", "aaa" });
}

Transform IT_ThreeColSearchInput = commUIObjsTs.transform.Find("IT_ThreeColSearchInput");
if (IT_ThreeColSearchInput != null)
{
    SearchInputField Input1 = IT_ThreeColSearchInput.Find("Input1").GetComponent<SearchInputField>();
    Input1.options.Clear();
    Input1.AddOptions(new List<string> { "a", "aa", "aaa" });

    SearchInputField Input2 = IT_ThreeColSearchInput.Find("Input2").GetComponent<SearchInputField>();
    Input2.options.Clear();
    Input2.AddOptions(new List<string> { "a", "aa", "aaa" });

    SearchInputField Input3 = IT_ThreeColSearchInput.Find("Input3").GetComponent<SearchInputField>();
    Input3.options.Clear();
    Input3.AddOptions(new List<string> { "a", "aa", "aaa" });
}

三、示例工程编译及运行

  • 解压CreateUIPrefab.zip文件,使用unity 2019.2.2f1版本打开CreateUIPrefab文件夹,点击“Tool” -> CreateUI生成UI对象。
  • Unit 高效开发系列 - 第三章 代码自动化创建UI预制体,Unity,游戏开发,C#,自动化,ui,游戏程序,unity,c#

四、资源下载

  • 获取示例工程资源
    链接:https://pan.baidu.com/s/1fuNGZXH-GHwr8_TxXUcKnA?pwd=hfh5
    提取码:pt5n

  • 获取解压密码
    链接:http://jiajiatong.store/target.html?id=13

  • 上一章
    链接:Unit 高效开发系列 - 第二章 代码自动化创建粒子特效预制体

  • 下一章
    链接:Unit 高效开发系列 - 第四章 无限循环滑动ScrollView封装文章来源地址https://www.toymoban.com/news/detail-771606.html

到了这里,关于Unit 高效开发系列 - 第三章 代码自动化创建UI预制体的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 移动开发技术_微信小程序_第三章测试

    作答记录 1 【单选题】下列关于小程序生命周期函数说法不正确的是( )。(5.0分) A、 分为应用生命周期函数和页面生命周期函数两种 B、 在onLoad生命周期函数中可以获得地址中的参数 C、 onLaunch生命周期函数是小程序初始化完成时触发 D、 onLaunch 生命周期函数会被重复执

    2024年02月09日
    浏览(38)
  • 数字图像处理第三章 学习笔记附部分例子代码(C++ & opencv)

    本系列博客参考书为, 数字图像处理第三版-冈萨雷斯 第三版教材中图片下载地址: book images downloads vs2019配置opencv可以查看:VS2019 Opencv4.5.4配置教程 后续剧情: 数字图像处理 第四章 频率域滤波 学习笔记 数字图像处理 第六章 彩色图像处理 学习笔记 数字图像处理 第七章 小

    2024年02月03日
    浏览(82)
  • 使用 GPT4 和 ChatGPT 开发应用:前言到第三章

    原文:Developing Apps with GPT-4 and ChatGPT 译者:飞龙 协议:CC BY-NC-SA 4.0 在发布仅仅五天后,ChatGPT 就吸引了惊人的一百万用户,这在科技行业及其他领域引起了轰动。作为一个副作用,OpenAI API 用于人工智能文本生成的接口突然曝光,尽管它已经可用了三年。ChatGPT 界面展示了这

    2024年01月20日
    浏览(73)
  • 第三章 LInux多线程开发 3.1-3.5线程创建 终止 分离

    一般情况下,main函数所在的线程我们称之为 主线程(main线程) ,其余创建的线程称之为子线程。 程序中默认只有一个进程,fork()函数调用,2进行 程序中默认只有一个线程,pthread_create()函数调用,2个线程。 #include pthread.h int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

    2024年02月09日
    浏览(46)
  • AIGC系列文章目录 第三章 AIGC 简单易用免费的AI图像生成器: Stable Diffusion

    目前亲测体验的AI图像生成器有NovelAI、MJ和Stable Diffusion。其中, 支持免费、无限生成、超高专业级画质 的只有 Stable Diffusion 。 Stable Diffusion 由 Stable Diffusion XL 提供支持,是一款最先进的工具,可以将您的想象力变为现实。 只需点击几下和简单的文本输入,您就可以创建令人

    2024年02月03日
    浏览(68)
  • 第三章:AI大模型的开发环境搭建3.3 开发环境配置3.3.1 本地环境配置

    在本章中,我们将详细介绍如何为AI大模型的开发搭建本地环境。我们将从背景介绍开始,然后讲解核心概念与联系,接着深入了解核心算法原理、具体操作步骤以及数学模型公式。在此基础上,我们将提供具体的最佳实践,包括代码实例和详细解释说明。最后,我们将探讨

    2024年02月19日
    浏览(50)
  • 【正点原子Linux连载】第三章 RV1126开发环境搭建 摘自【正点原子】ATK-DLRV1126系统开发手册

    1)实验平台:正点原子RV1126 Linux开发板 2)平台购买地址:https://detail.tmall.com/item.htm?id=692176265749 3)全套实验源码+手册+视频下载地址: http://www.openedv.com/thread-340252-1-1.html 3.1 rv1126的环境配置 在上章节里面我们已经安装好Ubuntu,此时的Ubuntu还是不能做开发的,因为还有很多环

    2024年02月04日
    浏览(52)
  • Linux第三章

    无论是Windows、MacOS、Linux均采用多用户的管理模式进行权限管理。在Linux系统中,拥有最大权限的账户名为:root(超级管理员) root用户拥有最大的系统操作权限,而普通用户在许多地方的权限是受限的(普通用户的权限,一般在其HOME目录内是不受限的,一旦出了HOME目录,大

    2023年04月26日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包