Esempio n. 1
0
        public void generateTest()
        {
            GameObject gameObject = new GameObject("AutoUI");
            Animator   animator   = gameObject.AddComponent <Animator>();
            GameObject child      = new GameObject("Child");

            child.transform.parent = gameObject.transform;
            AutoCompoGenerator generator = new AutoCompoGenerator()
            {
                specifiedTypeName = "AutoUIView",
                objFieldDict      = new Dictionary <Object, AutoBindFieldInfo>()
                {
                    { animator, new AutoBindFieldInfo(animator.GetInstanceID(), "./", typeof(Animator), null, "_animator") },
                    { child, new AutoBindFieldInfo(child.GetInstanceID(), "./Child", typeof(GameObject), null, "_child") }
                },
            };
            var unit = generator.genScript4GO(gameObject, new AutoCompoGenSetting()
            {
                baseTypes       = new string[] { nameof(MonoBehaviour) },
                fieldAttributes = new string[] { nameof(SerializeField) },
                Namespace       = "UI",
            });

            CodeNamespace nameSpace = unit.Namespaces[0];

            Assert.True(nameSpace.Imports.OfType <CodeNamespaceImport>().Any(n => n.Namespace == "UnityEngine"));
            Assert.True(nameSpace.Imports.OfType <CodeNamespaceImport>().Any(n => n.Namespace == "System"));
            Assert.AreEqual("UI", nameSpace.Name);
            CodeTypeDeclaration type = nameSpace.Types[0];

            Assert.AreEqual("AutoUIView", type.Name);
            CodeMemberField          field     = type.Members.OfType <CodeMemberField>().First(f => f.Name == "_animator");
            CodeAttributeDeclaration autoCompo = field.CustomAttributes.OfType <CodeAttributeDeclaration>().First(d => d.Name == nameof(AutoCompoAttribute));

            Assert.AreEqual(animator.GetInstanceID(), (autoCompo.Arguments[0].Value as CodePrimitiveExpression).Value);
            Assert.AreEqual("./", (autoCompo.Arguments[1].Value as CodePrimitiveExpression).Value);
            CodeAttributeDeclaration serFie = field.CustomAttributes.OfType <CodeAttributeDeclaration>().First(d => d.Name == nameof(SerializeField));

            Assert.NotNull(serFie);
            Assert.AreEqual(nameof(Animator), field.Type.BaseType);
            field     = type.Members.OfType <CodeMemberField>().First(f => f.Name == "_child");
            autoCompo = field.CustomAttributes.OfType <CodeAttributeDeclaration>().First(d => d.Name == nameof(AutoCompoAttribute));
            Assert.AreEqual(child.GetInstanceID(), (autoCompo.Arguments[0].Value as CodePrimitiveExpression).Value);
            Assert.AreEqual("./Child", (autoCompo.Arguments[1].Value as CodePrimitiveExpression).Value);
            serFie = field.CustomAttributes.OfType <CodeAttributeDeclaration>().First(d => d.Name == nameof(SerializeField));
            Assert.NotNull(serFie);
            Assert.AreEqual(nameof(GameObject), field.Type.BaseType);
        }
Esempio n. 2
0
        public void genButtonTest()
        {
            GameObject         gameObject = new GameObject("Button");
            Button             button     = gameObject.AddComponent <Button>();
            AutoCompoGenerator generator  = new AutoCompoGenerator()
            {
                specifiedTypeName = "CommonButton",
                objFieldDict      = new Dictionary <Object, AutoBindFieldInfo>()
                {
                    { button, new AutoBindFieldInfo(button.GetInstanceID(), "./", typeof(Button), null, "_button") }
                }
            };
            var unit = generator.genScript4GO(gameObject, new AutoCompoGenSetting()
            {
                Namespace       = "UI",
                baseTypes       = new string[] { nameof(MonoBehaviour) },
                fieldAttributes = new string[] { nameof(SerializeField) }
            });

            //字段
            CodeTypeDeclaration type  = unit.Namespaces[0].Types[0];
            CodeMemberField     field = type.Members.OfType <CodeMemberField>().First(f => f.Name == "_button");

            Assert.NotNull(field);
            //属性
            CodeMemberProperty prop = type.Members.OfType <CodeMemberProperty>().First(f => f.Name == "button");

            Assert.NotNull(prop);
            Assert.True(prop.HasGet);
            Assert.False(prop.HasSet);
            //初始化
            CodeMemberMethod    init   = type.Members.OfType <CodeMemberMethod>().First(m => m.Name == "init");
            CodeAssignStatement assign = init.Statements.OfType <CodeAssignStatement>().First();

            Assert.NotNull(assign);
            //事件
            CodeMemberEvent Event = type.Members.OfType <CodeMemberEvent>().First(m => m.Name == "onButtonClick");

            Assert.AreEqual(nameof(Action), Event.Type.BaseType);
        }
Esempio n. 3
0
        public void genListCtrlTest()
        {
            GameObject gameObject = new GameObject("Content");
            GameObject item       = new GameObject("Item");

            item.transform.SetParent(gameObject.transform);
            AutoCompoGenerator generator = new AutoCompoGenerator()
            {
                specifiedTypeName = "ItemList",
                objFieldDict      = new Dictionary <Object, AutoBindFieldInfo>()
                {
                    { item, new AutoBindFieldInfo(item.GetInstanceID(), "./Item", typeof(GameObject), null, null) }
                },
                controllerType = AutoCompoGenerator.CTRL_TYPE_LIST,
                listOrigin     = item,
            };
            var unit = generator.genScript4GO(gameObject, new AutoCompoGenSetting()
            {
                Namespace       = "UI",
                baseTypes       = new string[] { nameof(MonoBehaviour) },
                fieldAttributes = new string[] { nameof(SerializeField) }
            });
            CodeTypeDeclaration type      = unit.Namespaces[0].Types[0];
            CodeTypeDeclaration poolType  = type.Members.OfType <CodeTypeDeclaration>().First(t => t.Name == "ItemPool");
            CodeMemberField     poolField = type.Members.OfType <CodeMemberField>().First(f => f.Name == "_itemPool");

            Assert.AreEqual(poolType.Name, poolField.Type.BaseType);
            CodeMemberField itemCreatePartial = type.Members.OfType <CodeMemberField>().First(f => f.Name.Contains("onItemCreate"));

            Assert.NotNull(itemCreatePartial);
            CodeMemberField itemRemovePartial = type.Members.OfType <CodeMemberField>().First(f => f.Name.Contains("onItemRemove"));

            Assert.NotNull(itemRemovePartial);
            CodeMemberMethod initPoolMethod = type.Members.OfType <CodeMemberMethod>().First(m => m.Name == "initPool");

            Assert.NotNull(initPoolMethod);
        }
Esempio n. 4
0
        public void genButtonCtrlTest()
        {
            GameObject         gameObject = new GameObject("Button");
            Button             button     = gameObject.AddComponent <Button>();
            AutoCompoGenerator generator  = new AutoCompoGenerator()
            {
                specifiedTypeName = "CommonButton",
                objFieldDict      = new Dictionary <Object, AutoBindFieldInfo>()
                {
                    { button, new AutoBindFieldInfo(button.GetInstanceID(), "./", typeof(Button), null, null) }
                },
                controllerType = AutoCompoGenerator.CTRL_TYPE_BUTTON,
                buttonMain     = button
            };
            var unit = generator.genScript4GO(gameObject, new AutoCompoGenSetting()
            {
                Namespace       = "UI",
                baseTypes       = new string[] { nameof(MonoBehaviour) },
                fieldAttributes = new string[] { nameof(SerializeField) }
            });
            CodeTypeDeclaration type  = unit.Namespaces[0].Types[0];
            CodeMemberField     field = type.Members.OfType <CodeMemberField>().First(f => f.Name == "_asButton");
            //主按钮标记
            CodeAttributeDeclaration autoCompo = field.CustomAttributes.OfType <CodeAttributeDeclaration>().First(a => a.Name == typeof(AutoCompoAttribute).Name);

            Assert.AreEqual(button.GetInstanceID(), (autoCompo.Arguments[0].Value as CodePrimitiveExpression).Value);
            Assert.AreEqual("./", (autoCompo.Arguments[1].Value as CodePrimitiveExpression).Value);
            Assert.AreEqual("mainButton", (autoCompo.Arguments[2].Value as CodePrimitiveExpression).Value);
            //事件
            CodeMemberEvent Event = type.Members.OfType <CodeMemberEvent>().First(e => e.Name == "onClick");

            Assert.AreEqual("CommonButton", Event.Type.TypeArguments[0].BaseType);
            //回调函数
            CodeMemberMethod callback = type.Members.OfType <CodeMemberMethod>().First(m => m.Name == "clickCallback");

            Assert.NotNull(callback);
        }