美文网首页
lua 自动获取ui组件并生成代码工具

lua 自动获取ui组件并生成代码工具

作者: Walk_In_Jar | 来源:发表于2022-07-15 15:27 被阅读0次

    gameobject上挂载

    using UnityEngine;
    public class UIInterface : MonoBehaviour
    {
        public Object[] compChilds;
    }
    
    

    写wraper

        [LuaCallCSharp]
        public static class UIInterfaceWraper
        {
            //由于lua特性,lua获取到的Object类型,可以直接调用其具体类型的属性方法
            //但是,如果要调用C#方法,必须使用具体类型
            //比如这个,需通过wraper方法
            public static void ForceRebuildLayoutImmediate(Object obj)
            {
                LayoutRebuilder.ForceRebuildLayoutImmediate(obj as RectTransform);
            }
            public static Object GetUnityEngineObject(UIInterface face,int index)
            {
               return face.pChilds[index];
            }
        }
    

    lua中这样获取

    local getUEObject =function (index,face)
        return face.compChilds[index]
    end
    
    local face = self.container:GetComponent("UIInterface")
    self.obj1 = getUEObject(0, face)
    self.scrollRect = getUEObject(1, face)--获取Object类型,可直接在lua'中当作对应类型处理
    self.content = getUEObject(2, face)
    
    CS.UIInterfaceWraper.ForceRebuildLayoutImmediate(self.content)
    

    ui组件自动获取(editor代码)

        public static void AddComp()
        {
            GameObject window = Selection.activeGameObject;
            if (window == null)
                return;
    
            UIInterface face = window.GetComponent<UIInterface>();
            if (face == null)
            {
                face = window.AddComponent<UIInterface>();
            }
            AllList = new List<Object>();
    
            AddToList(face.transform);
    
            face.pChilds = new Object[AllList.Count];
            for (int i = 0; i < AllList.Count; i++)
            {
                face.pChilds[i] = AllList[i];
            }
            EditorUtility.SetDirty(window);
            //AssetDatabase.SaveAssets();
        }
        static void AddToList(Transform transform)
        {
            foreach (Transform item in transform)
            {
                var obj = GetObj(item.gameObject);
                if (obj != null)
                {
                    AllList.Add(obj);
                }
    
                if (item.childCount > 0 && item.GetComponent<UIInterface>() == null)
                    AddToList(item);
            }
        }
    public static UnityEngine.Object GetObj(GameObject tgo)
        {
            int length = tgo.gameObject.name.Length;
            if (length <= 3)
                return null;
    
            string n = tgo.name.Remove(3);
            UnityEngine.Object obj = null;
            switch (n)
            {
                case "txt":
                    obj = tgo.GetComponent<TextMeshProUGUI>();
                    break;
                case "btn":
                    obj = tgo.GetComponent<Button>();
                    break;
                case "obj":
                    obj = tgo;
                    break;
                case "tra":
                    obj = tgo.transform;
                    break;
                case "rct":
                    obj = tgo.transform as RectTransform; 
                    break;
                case "ins":
                    obj = tgo.GetComponent<UIInterface>();
                    break;
                case "sdr":
                    obj = tgo.GetComponent<Slider>();
                    break;
                case "tog":
                    obj = tgo.GetComponent<Toggle>();
                    break;
                case "inp":
                    obj = tgo.GetComponent<InputField>();
                    break;
                case "slr":
                    obj = tgo.GetComponent<ScrollRect>();
                    break;
                case "cvg":
                    obj = tgo.GetComponent<CanvasGroup>();
                    break;
                default:
                    // Log.Error("未能处理物体:", tgo.name);
                    break;
            }
            return obj;
        }
    

    ui代码生成 (editor代码)

        private static string upperFirst(string s)
        {
            return Regex.Replace(s, @"\b[a-z]\w+", delegate (Match match)
            {
                string v = match.ToString();
                return char.ToUpper(v[0]) + v.Substring(1);
            });
        }
        private static string GenCode()
    {
             GameObject window = Selection.activeGameObject;
            if (window == null)
                return;
            UIInterface face = window.GetComponent<UIInterface>();
            var list = face.pChilds;
            string line = "--UI Members " + "\n";
            int index = 0;
            foreach (var obj in list)
            {
                string Goanme = string.Empty;
                GameObject go = null;
                if (obj is GameObject)
                {
                    go = obj as GameObject;
                    Goanme = go.name;
                }
                else if (obj is Component com)
                {
                    Goanme = com.transform.gameObject.name;
                }
                string start = Goanme.Remove(3);
                string name = "self.m" + upperFirst(Goanme).Replace(" ","");
                if (!string.IsNullOrEmpty(Goanme))
                {
                    line += name+ "=Utils.GetUnityEngineObject(" +index+",face)\n";
                }
                index++;
            }
           Debug.Log(line);
    }
    

    效果

    --UI Members 
    self.mSlrScrollView=Utils.GetUnityEngineObject(0,face)
    self.mSdrSlider=Utils.GetUnityEngineObject(1,face)
    self.mBtnButton=Utils.GetUnityEngineObject(2,face)
    

    相关文章

      网友评论

          本文标题:lua 自动获取ui组件并生成代码工具

          本文链接:https://www.haomeiwen.com/subject/ogpfirtx.html