Example #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();
            }
        }
Example #2
0
        /// <summary>
        /// Called when the underlying block has been updated.
        /// </summary>
        protected void OnBlockUpdated(Block.UpdateState updateState)
        {
            switch (updateState)
            {
            case Block.UpdateState.Inputs:
            {
                //rebuild block view's input views
                BlockViewBuilder.BuildInputViews(mBlock, this);

                //reupdate layout
                BuildLayout();

                //call this once to update the connection DB
                this.OnXYUpdated();

                //call this again to change new input views
                this.ChangeBgColor(m_BgImages[0].color);

                break;
            }
            }
        }
        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);
        }