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等:
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对象。
四、资源下载
-
获取示例工程资源
链接:https://pan.baidu.com/s/1fuNGZXH-GHwr8_TxXUcKnA?pwd=hfh5
提取码:pt5n -
获取解压密码
链接:http://jiajiatong.store/target.html?id=13 -
上一章
链接:Unit 高效开发系列 - 第二章 代码自动化创建粒子特效预制体文章来源:https://www.toymoban.com/news/detail-771606.html -
下一章
链接:Unit 高效开发系列 - 第四章 无限循环滑动ScrollView封装文章来源地址https://www.toymoban.com/news/detail-771606.html
到了这里,关于Unit 高效开发系列 - 第三章 代码自动化创建UI预制体的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!