u3d 工具写法:实现根据预设模板和配置,生成预设物体

该代码段展示了一个在Unity中通过加载XML配置文件动态生成预设模板的实现。首先,从Resources目录下加载预设模板,然后读取XML文件中的docTemp节点,根据配置信息(如图片URL、是否可输入、输入字段数)创建并设置新的GameObject,最后保存为预设文件。过程中涉及了文件读取、XML解析、对象实例化和预设保存等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	[MenuItem("Tools/根据预设生成物品")]
    public static void CreateDocTemplate()
    {
    	//获取事先做好的预设
        GameObject templatePrefab = AssetDatabase.LoadAssetAtPath("Assets/Resources/Prefabs/DocTemplate/ResPrefab/Template_doc.prefab", typeof(GameObject)) as GameObject;
		
		//读取配置表并且遍历
		XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load("Assets/Resources/StaticXML/docTemp.xml");
        XmlNodeList nodeList = xmlDoc.SelectNodes("//docTemp");
        int index = 0;
        foreach (XmlElement xe in nodeList)
        {
            index++;
            string imageUrlStr = xe.GetAttribute("docUrl");
            bool canInput = int.Parse(xe.GetAttribute("canInput")) == 1;
            int inputFieldCount = int.Parse(xe.GetAttribute("inputFieldCount"));
            if (!canInput || inputFieldCount > 0)
            {
                CreatTemplate(index, imageUrlStr, inputFieldCount);
            }
        }
        //保存
        AssetDatabase.SaveAssets();
    }
	//生成的预设
    private static void CreatTemplate(int index , string imageUrlStr , int inputFieldCount)
    {
    	//生成的预设储存的路径
        string savePath = templatePrefabPath + "/doc_" + index + ".prefab";
        if (!File.Exists(savePath))//是否有这个预设,已存在则不生成
        {
        	//创建一个 GameObject
            GameObject templateObj = PrefabUtility.InstantiatePrefab(templatePrefab) as GameObject;
            //以下为对新的 GameObjece 进行设置,根据不同需求设计
            string[] urlArr = imageUrlStr.Split(',');
            Transform iamgeRoot = templateObj.transform.Find("Scroll View/Viewport/Content");
            for (int i = 0; i < urlArr.Length; i++)
            {
                GameObject docImageGo = new GameObject("Sprite_doc");
                docImageGo.transform.SetParent(iamgeRoot);
                Image image = docImageGo.AddComponent<Image>();
                Sprite sp = (Sprite)AssetDatabase.LoadAssetAtPath("Assets/_Atlas/DocTemplate/" + urlArr[i] + ".jpg", typeof(UnityEngine.Sprite));
                if (sp == null)
                {
                    Debug.LogError("图片不存在:" + "Assets/_Atlas/DocTemplate/" + urlArr[i] + ".jpg");
                    GameObject.DestroyImmediate(templateObj);
                    return;
                }
                image.sprite = sp;
                image.GetComponent<RectTransform>().sizeDelta = new Vector2(sp.texture.width, sp.texture.height);
            }
            //保存预设
            PrefabUtility.SaveAsPrefabAsset(templateObj, savePath);
            GameObject.DestroyImmediate(templateObj);
            Debug.Log("成功生成文书模板:doc_" + index);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值