Exemplo n.º 1
0
        static void BuildBlockPrefabs()
        {
            Blockly.Dispose();
            Blockly.Init(false);
            Workspace workspace = new Workspace();

            var blocks = BlockFactory.Instance.GetAllBlockDefinitions().Keys;

//            var blocks = BlockFactory.Instance.GetAllBlockDefinitions().Keys.Where(s => s.Equals("lists_create_with"));

            if (!Directory.Exists(BlockResMgr.Get().BlockViewPrefabPath))
            {
                Directory.CreateDirectory(BlockResMgr.Get().BlockViewPrefabPath);
            }

            BlockResMgr.Get().ClearBlockViewPrefabs();
            BlockResMgr.Get().LoadBlockTutorials();
            try
            {
                int index = 0;
                int count = blocks.Count();
                foreach (string name in blocks)
                {
                    EditorUtility.DisplayProgressBar(null, "Building block prefab: " + name, index / (float)count);

                    Block      block = workspace.NewBlock(name);
                    GameObject obj   = BlockViewBuilder.BuildBlockView(block);

                    string     path   = BlockResMgr.Get().BlockViewPrefabPath + obj.name + ".prefab";
                    GameObject prefab = PrefabUtility.CreatePrefab(path, obj, ReplacePrefabOptions.Default);
                    BlockResMgr.Get().CreateTutorialTrigger(name, prefab);
                    BlockResMgr.Get().AddBlockViewPrefab(prefab);

                    GameObject.DestroyImmediate(obj);

                    index++;
                }
            }
            finally
            {
                AssetDatabase.SaveAssets();
                Resources.UnloadUnusedAssets();

                EditorUtility.ClearProgressBar();
            }
        }
Exemplo n.º 2
0
        public static BlockView CreateView(Block block)
        {
            BlockView blockView;

            GameObject blockPrefab = BlockResMgr.Get().LoadBlockViewPrefab(block.Type);

            if (blockPrefab != null)
            {
                // has been builded beforehand
                GameObject blockObj = GameObject.Instantiate(blockPrefab);
                blockObj.name = blockPrefab.name;

                blockView = blockObj.GetComponent <BlockView>();
                blockView.BindModel(block);

                // rebuild inputs for mutation blocks
                if (block.Mutator != null)
                {
                    BlockViewBuilder.BuildInputViews(block, blockView);
                }

                blockView.BuildLayout();
            }
            else
            {
                blockPrefab = BlockViewBuilder.BuildBlockView(block);

                blockView = blockPrefab.GetComponent <BlockView>();
                blockView.BindModel(block);

                // BlockViewBuilder.BuildBlockView will do both "BuildInputViews" and "BuildLayout"
            }

            blockView.ChangeBgColor(BlocklyUI.WorkspaceView.Toolbox.GetColorOfBlockView(blockView));

            return(blockView);
        }