Example #1
0
 private void Awake()
 {
     //加载地图信息
     m_mapData = new Dictionary <Vector3, BlockInfo>();
     for (int i = 0; i < transform.childCount; i++)
     {
         Transform child = transform.GetChild(i);
         for (int j = 0; j < child.childCount; j++)
         {
             BlockInfoComponent block       = child.GetChild(j).GetComponent <BlockInfoComponent>();
             BasicEntity        basicEntity = child.GetChild(j).GetComponent <BasicEntity>();
             BlockInfo          blockInfo   = new BlockInfo(block.m_logicPosition, basicEntity);
             if (!m_mapData.ContainsKey(block.m_logicPosition))
             {
                 m_mapData.Add(block.m_logicPosition, blockInfo);
             }
         }
     }
 }
    //实例化方块
    void Draw()
    {
        string id = brush.m_blockID.ToString();

        //获取笔刷位置
        float posX = brush.transform.position.x;
        float posY = brush.transform.position.y;
        float poxZ = brush.transform.position.z;

        Debug.Log("voxelMap.name: " + voxelMap.name);

        if (voxelMap.displayUpperGizmo)
        {
            GameObject block = GameObject.Find(voxelMap.name + "/voxelUpperBlocks/block_" + id);

            //如果找不到现有的实例,则新建一个方块
            if (block == null)
            {
                block = new GameObject("block_" + id);
                block.transform.SetParent(voxelMap.voxelUpperBlocks.transform);
                block.transform.position = new Vector3(posX, posY, poxZ);
                block.AddComponent <MeshFilter> ();
                block.AddComponent <MeshRenderer> ();
                block.AddComponent <BasicEntity> ();
                block.AddComponent <BlockInfoComponent> ();
                block.AddComponent <PropertyComponent> ();
                //block.AddComponent<AbilityComponent> ();
            }
            block.tag = brush.m_tag;
            //传入方块的网格数据
            BlockInfoComponent blockInfoComponent = block.GetComponent <BlockInfoComponent> ();
            PropertyComponent  propertyComponent  = block.GetComponent <PropertyComponent> ();
            //AbilityComponent abilityComponent = block.GetComponent<AbilityComponent> ();

            block.GetComponent <MeshFilter> ().sharedMesh       = brush.m_meshFilter.sharedMesh;
            block.GetComponent <MeshRenderer> ().sharedMaterial = brush.m_meshRender.sharedMaterial;
            //
            blockInfoComponent.m_blockType     = voxelMap.GetBlockBrush.BlockType;
            blockInfoComponent.m_blockName     = voxelMap.GetBlockBrush.m_name;
            blockInfoComponent.m_logicPosition = brush.m_LogicPosition;
            blockInfoComponent.m_componentType = ComponentType.BlockInfo;
            blockInfoComponent.m_entity        = block.GetComponent <BasicEntity> ();
            propertyComponent.m_componentType  = ComponentType.Property;
            propertyComponent.m_entity         = block.GetComponent <BasicEntity> ();
            //abilityComponent.m_componentType = ComponentType.Ability;
            //abilityComponent.m_entity = block.GetComponent<BasicEntity> ();
        }
        else
        {
            GameObject block = GameObject.Find(voxelMap.name + "/voxelLowerBlocks/block_" + id);

            //如果找不到现有的实例,则新建一个方块
            if (block == null)
            {
                block = new GameObject("block_" + id);
                block.transform.SetParent(voxelMap.voxelLowerBlocks.transform);
                block.transform.position = new Vector3(posX, posY, poxZ);
                block.AddComponent <MeshFilter> ();
                block.AddComponent <MeshRenderer> ();
                block.AddComponent <BasicEntity> ();
                block.AddComponent <BlockInfoComponent> ();
                block.AddComponent <PropertyComponent> ();
                //block.AddComponent<AbilityComponent> ();
            }
            block.tag = brush.m_tag;
            //传入方块的网格数据
            BlockInfoComponent blockInfoComponent = block.GetComponent <BlockInfoComponent> ();
            PropertyComponent  propertyComponent  = block.GetComponent <PropertyComponent> ();
            //AbilityComponent abilityComponent = block.GetComponent<AbilityComponent> ();
            block.GetComponent <MeshFilter> ().sharedMesh       = brush.m_meshFilter.sharedMesh;
            block.GetComponent <MeshRenderer> ().sharedMaterial = brush.m_meshRender.sharedMaterial;
            //
            blockInfoComponent.m_blockType     = voxelMap.GetBlockBrush.BlockType;
            blockInfoComponent.m_blockName     = voxelMap.GetBlockBrush.m_name;
            blockInfoComponent.m_logicPosition = brush.m_LogicPosition;
            blockInfoComponent.m_componentType = ComponentType.BlockInfo;
            blockInfoComponent.m_entity        = block.GetComponent <BasicEntity> ();
            propertyComponent.m_componentType  = ComponentType.Property;
            propertyComponent.m_entity         = block.GetComponent <BasicEntity> ();
            //abilityComponent.m_componentType = ComponentType.Ability;
            //abilityComponent.m_entity = block.GetComponent<BasicEntity> ();
        }


        //Debug.Log (voxelMap.name + "/voxelLowerBlocks");
        //GameObject voxelLowerBlocks = GameObject.Find (voxelMap.name + "/voxelLowerBlocks");
        //voxelLowerBlocks.GetComponent<VoxelBlocks> ().mapData.Add (new Vector3 (1, 1, 1), BlockType.Door);
    }